Iptables is also used when you install the server. you don't have to worry about it. when you use it, you can find that iptables has many things to learn, such as opening up the ftp service. The default policy of the INPUT chain of the iptables filter table is set to DROP, and the rest of the chains are ACCEPT. This server is used as an ftp server and must be connected to another ftp server. That is to say, when you want to install the source port and the target server IptablesYou don't have to know. it's only available once. IptablesThere are still many things to learn, such as opening up the ftp service.
The default policy of the INPUT chain of the iptables filter table is set to DROP, and the rest of the chains are ACCEPT. This server is used as an ftp server and must be connected to another ftp server. That is to say, you must open both the source port and the target Port to 21:
# Iptables-a input-p tcp -- dport 21-jACCEPT
Is Port 21 enabled? No.
Let's talk about the ftp protocol first. The ftp protocol is a simple tcp protocol with poor confidentiality (plaintext). its working principle is that the client first connects to port 21 of the server, and then establishes a connection after three steps of handshake. It should be noted that this connection can only be used to transmit ftp commands. nothing can be passed through this connection, even if you use the "ls" command to view files.
After a command connection is established, the server needs to establish a data connection. Data connections are divided into active and passive modes ). By default, ftp is in passive mode. you can use the "pass" command to switch between active and passive ftp. The active mode is connected to the client through Port 20, while the passive mode is connected to the client through the port after port 1024. Because more than 1024 of ports are randomly allocated, in passive mode, we do not know what ports the server uses to connect to the client. That is to say, we do not know what port iptables should open.
At first, I used:
# Iptables-a input-p tcp -- dport 1024:-j ACCEPT
To allow the ftp to establish a passive connection. However, if this is the case, it means that all p2p connections are open and insecure.
Configure Active and Passive FTP mode using iptables
The FTP protocol can work in two ways: PORT and PASV. the Chinese meaning is active and passive.
Port mode: ftp server: tcp 21 client: dynamic
Pasv mode: ftp server: tcp 21 PORT (active) connection process: the client sends a connection request to the server's ftp port (default: 21), the server accepts the connection, create 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.
After the above analysis and view the relevant information, the following conclusions are obtained:
1. open ports 20 and 21:
# Iptables-a input-p tcp -- dport 21-j ACCEPT
# Iptables-a input-p tcp -- dport 20-j ACCEPT
2. add a rule:
# Iptables-a input-p tcp -- dport 1024:-m state -- stateESTABLISHED, RELATED-j ACCEPT
Allow passive access maintained by the connection.
3. if not, load the additional FTP module:
Modprobe ip_nat_ftp
Modprobe ip_conntrack
Modprobe ip_conntrack_ftp (I will load this part for my experiment)
There are two ways to enable loading upon startup: one is to compile and install iptables, and the other is to add these to the startup item and add it to/etc/rc. d/rc. the end of the local command is enough, so that the local command can be executed every time.
I don't know if there are any problems.
Supplement:
#########################
In the server
Iptables-a input-m state? State ESTABLISHED, RELATED-j ACCEPT
In the client
Iptables-a output-m state? State ESTABLISHED, RELATED-jACCEPT
#####################################
In fact, when I restarted iptables, the required modules disappeared. Through unremitting efforts, I finally found the Final Solution. edit/etc/sysconfig/iptables-config:
IPTABLES_MODULES = "ip_conntrack_ftp"
IPTABLES_MODULES = "ip_nat_ftp"
Add the modules to be loaded, no matter whether the system is restarted or iptables, but the ftp module is always loaded.
In this case, we hope to help you solve ftp problems in the future.
######################################## ##########
If the local machine is accessed through FTP
Service iptables stop
Modprobe ip_conntrack_irc // if the port is not 21, add the port number. If it is 21, you can also use modprobeip_nat_ftp to load other modules.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service