First, introduce the principles of ftp in active and passive modes: What is the role of ftp? If you build an ftp server on the intranet for testing, you do not need to pay attention to it. it is very easy. there is generally no firewall between hosts on the intranet, so there is no active or passive mode, however, in the production environment, the ftp server and the client are generally not
First, introduce the principles of ftp in active and passive modes: What is the role of ftp? If you build an ftp server on the intranet for testing, you do not need to pay attention to it. it is very easy. there is generally no firewall between hosts on the intranet, so there is no active or passive mode, however, in the production environment, there is generally more than one firewall between the ftp server and the client, so it is necessary to figure out this principle.
FTP is short for File TransferProtocol (File transfer protocol), used to transfer files between two computers. The FTP protocol is much more complex than HTTP. The reason for the complexity is that the FTP protocol requires two TCP connections. one is the command link used to transmit commands between the FTP client and the server, and the other is the data link used to upload or download data.
The FTP protocol can work in two ways: PORT and PASV. the Chinese meaning is active and passive.
The active connection process is that the client sends a connection request to the ftp port of the server (21 by default). The server accepts the connection and establishes a command link. When data needs to be transmitted, the client uses the PORT command on the command link to tell the server: "I opened PORT XXXX and you came to connect to me ". The server sends a connection request from Port 20 to Port XXXX of the client and establishes a data link to transmit data.
PASV (passive) connection process: the client sends a connection request to the FTP port of the server (21 by default). The server accepts the connection and establishes a command link. When data needs to be transmitted, the server uses the PASV command on the command link to tell the client: "I opened Port XXXX, and you came to connect to me ". Therefore, the client sends a connection request to Port XXXX of the server and establishes a data link to transmit data. To sum up, the active mode is as follows: the server knocks on the client, and the client opens the door. the passive mode is as follows: the client knocks on the server, and the server opens the door. Therefore, if you use a proxy to access the internet, the active mode cannot be used, because the server knocks on the door of the proxy server over the Internet, rather than the door of the client. sometimes, the client does not easily open the door because of firewall blocking, unless the client opens a high-end port greater than 1024
Let's talk about the establishment of ftp in the production environment: based on the introduction of the above active and passive modes, let's talk about the establishment of ftp in the production environment: first, ftp in the production environment should adopt the passive mode, because the passive mode is always when the client requests a connection from the server, the active mode requires the server to connect to the client port and the client port, if the client is a public ip address and there is no firewall, the problem is that the client is generally converted to a nat address, when the server is connected to the client, it is not the client, but the device that provides the nat service. Therefore, you cannot use the active mode. If the passive mode is used, when the client connects to port 21 on the server side and needs to transmit data, the server uses the PASV command on the command link to tell the client: "I opened Port XXXX and you came to connect to me ". Therefore, the client sends a connection request to Port XXXX of the server and establishes a data link to transmit data. However, in the production environment, do not forget to have Firewall devices in front of the server, and the default policy is disable. Generally, only a few ports are enabled, and other ports are turned off, when the client connects to the port provided by the server, it will be rejected by the firewall. to avoid this situation, you need to restrict the ports opened in passive mode in the ftp server configuration file, so that you can restrict risks, such as vsftp settings. edit the vsftpd configuration file/etc/vsftpd. add pasv_max_port = 3000 pasv_min_port = 3020 # these two lines mean that the server does not randomly open ports greater than 1024 when ports are opened in passive mode, instead, open the ports specified in this range for firewall changes: in addition to opening Port 21, remember to open ports 3000--3020 on the firewall.Iptables:Iptables-IINPUT-p tcp -- dport 3000: 3020-j ACCEPT: if a firewall device is used, enable access to ports 3000-3020 of the ip address of the host that provides ftp service on the device during the fire prevention period.