How to use and common configuration of FTP connection under Linux

Introduction to FTP Protocol

FTP is the English abbreviation of File Transfer Protocol (File Transfer Protocol), while the Chinese abbreviation is "Text Transfer Protocol". Used for bi-directional transmission of control files on the Internet. At the same time, it is also an application (Application). There are different FTP applications based on different operating systems, and all of these applications follow the same protocol to transfer files. In the use of FTP, users often encounter two concepts: "download" (Download) and "upload" (Upload). "Downloading" a file means copying a file from a remote host to your own computer; "uploading" a file means copying a file from your own computer to a remote host. In Internet language, users can upload (download) files to (from) remote hosts through client programs.

FTP uses the user interface of the Internet standard file transfer protocol FTP to provide users with a set of applications for managing file transfers between computers.

FTP is designed based on the client-server (C/S) model, and two connections are established between the client and the FTP server.

The development of any FTP-based client software must follow the working principle of FTP. The unique advantage of FTP and the biggest difference from other client-server programs is that it uses two TCP connections between two communicating hosts. One is a data connection, used for data transmission; the other is a control connection, used to transmit control information (command and response), this idea of ​​separately transmitting commands and data greatly improves the efficiency of FTP, while other client server applications Generally there is only one TCP connection. Figure 1 shows the basic model of FTP. The client has three components: the user interface, the client control process, and the client data transfer process. The server has two components: the server control process and the server data transfer process. In the entire interactive FTP session, the control connection is always connected, and the data connection is opened and then closed every time a file is transferred.

transfer method

There are two ways of FTP transmission: ASCII and binary.

  • The ASCII transfer method
    assumes that the file the user is copying contains simple ASCII text. If the remote machine is not running UNIX, ftp will usually automatically adjust the contents of the file when the file is transferred so that it can interpret the file as stored on another computer. The format of the text file.
    But there is often such a situation, the files that the user is transferring contain not text files, they may be programs, databases, word processing files or compressed files. Before copying any non-text files, use the binary command to tell ftp to copy them verbatim.
  • Binary transfer mode
    In binary transfer, the bit order of the file is preserved so that the original and the copied are in a bit-by-bit one-to-one correspondence. Even files containing bit sequences on the destination machine are meaningless. For example, macintosh transmits executable files to Windows system in binary mode, and this file cannot be executed on the other system.
    If binary files are transferred in ASCII mode, they will still be translated even if they are not needed. This can corrupt data. (ASCII methods generally assume that the first significant bit of each character is meaningless, because ASCII character combinations do not use it. If transferring binary files, all bits are significant.)

support mode

The FTP client initiates an FTP session and establishes a corresponding connection with the FTP server. During the FTP session, two connections, the control information process and the data process, need to be established. The control connection cannot complete the task of transferring data, and can only be used to transmit control information such as internal commands and command responses executed by FTP; the data connection is the connection between the server and the client to transfer files, which is full-duplex and allows simultaneous Two-way data transfer. When the data transfer is completed, the data connection will be withdrawn, and then return to the FTP session state until the control connection is withdrawn and the session is exited.

FTP supports two modes: Standard (PORT mode, active mode), Passive (PASV, passive mode).

  • In Port mode,
    the FTP client first establishes a connection with the TCP port 21 of the server to send commands. When the client needs to receive data, it sends a PORT command on this channel. The PORT command contains what port the client uses to receive data. When transmitting data, the server connects to the designated port of the client through its own TCP port 20 to send data. The FTP server must establish a new connection with the client to transfer data.

  • Passive mode
    The establishment of a control channel is similar to the Standard mode, but the Pasv command is sent after the connection is established. After the server receives the Pasv command, it opens a temporary port (the port number is greater than 1023 and less than 65535) and notifies the client of the request to transmit data on this port. The client connects to this port of the FTP server, and then the FTP server will transmit data through this port.
    Many firewalls are not allowed to accept external connections when they are set, so many FTP servers behind the firewall or on the intranet do not support PASV mode, because the client cannot pass through the firewall to open the high-end port of the FTP server; The client of the network cannot use the PORT mode to log in to the FTP server, because the TCP 20 from the server cannot establish a new connection with the client of the internal network, resulting in failure to work.

FTP Client Considerations

Depending on whether the FTP client has a public IP, whether a firewall is installed, and how the connected FTP server is set up, the following items need to be noted:

  • If the FTP client does not have a public IP, only the PASV method can be used to connect to the FTP server;
  • If the FTP client has a public IP and a firewall is installed, when using PORT to connect to the FTP server, high-end ports above 1024 must be opened on the firewall to ensure that when connecting to the data link, the connection request from the server to the client will not blocked by firewall

FTP protocol connection steps

You can read this article written by the author, which is very detailed:
https://blog.csdn.net/qq_37460687/article/details/125921156

FTP Common Commands

Upload and download. Port 21 is used for connection and port 20 is used for data transmission.

During FTP file transfer, the client first connects to port 21 of the FTP server for user authentication. After successful authentication, when transferring files, the server will open a port 20 to transfer data files.

FTP common commands under Linux

Connect to FTP server under Linux

  • connect to FTP
# 格式:ftp [hostname | ip-address]
ftp 192.168.1.122
  • disconnect ftp connection
quit

FTP directory operation under Linux

  • view current directory
pwd
  • Enter the specified directory
    # 格式:cd remote-directory
    cd
  • Create a directory
mkdir 目录名
  • delete directory
rmdir 目录名
  • View all directories and files in the current directory
 # 格式:ls [remote-directory] [local-file]
 # 说明:remote-directory 指定要查看其列表的目录。如果没有指定将使用远程计算机中的当前工作目录。 local-file指要存储列表的本地文件,如果没有将输出将显示在屏幕上。
ls
  • View the specified suffix file
dir .jpg

FTP file operation under Linux

  • delete single file
    # 格式:delete remote-file
    delete 文件名
  • delete multiple files
    # 格式:mdelete remote-files [ ...]
    mdelete 文件名
  • rename file
    # 格式:rename filename newfilename
    rename test atest
  • upload files
    # 格式:put local-file [remote-file]
    put /home/a.txt /mydata/b.txt
  • download a single file
    # 格式:get [remote-file] [local-file]
    get /mydata/a.txt /apps/b.txt
  • download multiple files
    # 进入指定目录
    cd /usr/your/

    # 格式:mget filename [filename ....]
    mget *.*

other commands

  • query all commands
# help[cmd]:显示FTP命令的帮助信息,cmd是命令名,如果不带参数,则显示所有FTP命令

FTP error code

code describe discuss
100 series The requested action has been initiated; another reply is expected before proceeding with a new command.
110 Reboot marked reply. The text is exact and not left to a particular implementation; it must read "MARK yyyy = mmmm", where yyyy is the user process data stream mark, and the equivalent mark for the mmmm server (note the space between the mark and the "=" ).
120 The service is ready in nn minutes.
125 Data connection is open; transfer started.
150 File status okay; about to open data connection. FTP uses two ports: 21 for sending commands and 20 for sending data. A status code of 150 means that the server will open a new connection on port 20 to send some data.
200 series The requested operation completed successfully.
200 Order okay.
202 The command is not implemented and is redundant on this site.
211 System status or system help reply.
212 directory status.
213 file status.
214 help message.
215 NAME system type. where NAME is the official system name from the list in the "Designated Numbers" document.
220 The service is ready for new users.
221 The service closed the control connection. Log out if appropriate.
225 Data connection is open; no transfer is in progress.
226 Close the data connection. The requested file operation was successful (for example, file transfer or file abort). This command opens a data connection on port 20 to perform operations, such as transferring files. The operation completed successfully and the data connection was closed.
227 Enter passive mode. (h1,h2,h3,h4,p1,p2)
230 User logged in, continue. This status code is displayed after the client has sent the correct password. It indicates that the user has successfully logged in.
250 Requested file operation okay, done.
257 "PATHNAME" has been created.
300 series The command has been accepted, but the requested operation is on hold pending receipt of further information.
331 The username is okay, a password is required. You will see this status code after the client sends a username, regardless of whether the supplied username is a valid account on the system.
332 Account login required. Provide login credentials
350 The requested file operation awaits further information.
400 series The command was not accepted and the requested operation did not occur, but the error condition is temporary and the operation can be requested again.
421 Error 421 Service Unavailable, closing control connection. Error 421 User Limit Reached Error 421 You are not authorized to connect Error 421 Maximum Connections Reached Error 421 Maximum Connections Exceeded This can be a reply to any command if the service knows it must shut down. Please try logging in later.
425 Unable to open data connection. Change from PASV to port mode, check your firewall settings, or try connecting via HTTP.
426 Connection closed; transfer aborted. The command opened a data connection to perform the operation, but the operation was canceled and the data connection was closed. Try logging back in; contact your hosting provider to check if your hosting account needs to be increased; try disabling the firewall on your PC and see if that fixes the issue. If not, check with your hosting provider or ISP.
450 The requested file operation was not performed. The file is not available (for example, the file is busy). Try again later.
451 The requested operation was aborted: Local error in processing. Make sure the command and parameters are entered correctly.
452 The requested operation was not performed. Insufficient storage space in the system. Ask the FTP administrator to increase the allocated storage space, or archive/delete remote files.
500 series 该命令未被接受,并且请求的操作未发生。
500 语法错误,命令无法识别,命令行太长。 尝试切换到被动模式。
501 参数或参数中的语法错误。 验证您的输入; 例如,确保没有错误的字符,空格等。
502 命令未实现。 服务器不支持此命令。
503 错误的命令序列。 验证命令序列。
504 未对该参数实现命令。 确保输入的参数正确。
530 用户未登录。 确保键入正确的用户名和密码组合。某些服务器在达到用户限制时使用此代码,而不是421
532 需要存储文件的帐户。 登录的用户没有在远程服务器上存储文件的权限。
550 请求的操作未执行。文件不可用,找不到,无法访问 验证您是否尝试连接到正确的服务器/位置。远程服务器的管理员必须为您提供通过FTP进行连接的权限。
552 请求的文件操作中止。超过存储分配。 需要更多的磁盘空间。存档远程服务器上不再需要的文件。
553 请求的操作未执行。不允许文件名。 更改文件名中的文件名或删除空格/特殊字符。
10,000系列 常见Winsock错误代码(Winsock错误代码的完整列表)
10054 连接由对等体复位。远程主机强制关闭连接。
10060 无法连接到远程服务器。 一般是超时错误。尝试从PASV切换到端口模式,或尝试增加超时值。
10061 无法连接到远程服务器。服务器主动拒绝连接。 尝试切换连接端口。
10066 目录不为空。 当其中有文件/文件夹时,服务器不会删除此目录。如果要删除目录,请首先归档或删除其中的文件。
10068 用户过多,服务器已满。 尝试在另一时间登录。

Guess you like

Origin blog.csdn.net/qq_37596943/article/details/127343591