Linux 主动/被动模式下FTP服务器的适当iptables规则

Linux 主动/被动模式下FTP服务器的适当iptables规则,linux,ftp,iptables,centos6,proftpd,Linux,Ftp,Iptables,Centos6,Proftpd,我在CentOS6上安装了ProFTPD服务器。 如果我使用ftp localhost,我可以正确连接,但是如果我从外部尝试,我会得到消息“no route to host”。但有一个到主机的路由,因为我是通过SSH连接的 我尝试添加以下iptable规则: iptables -A INPUT -p tcp -m tcp --dport 21 -m conntrack --ctstate ESTABLISHED -j ACCEPT -m comment --comment "Allow ftp

我在CentOS6上安装了ProFTPD服务器。 如果我使用ftp localhost,我可以正确连接,但是如果我从外部尝试,我会得到消息“no route to host”。但有一个到主机的路由,因为我是通过SSH连接的

我尝试添加以下iptable规则:

iptables -A INPUT  -p tcp -m tcp --dport 21 -m conntrack --ctstate ESTABLISHED -j ACCEPT -m comment --comment "Allow ftp connections on port 21"
iptables -A OUTPUT -p tcp -m tcp --dport 21 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT -m comment --comment "Allow ftp connections on port 21"

iptables -A INPUT  -p tcp -m tcp --dport 20 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT -m comment --comment "Allow ftp connections on port 20"
iptables -A OUTPUT -p tcp -m tcp --dport 20 -m conntrack --ctstate ESTABLISHED -j ACCEPT -m comment --comment "Allow ftp connections on port 20"

iptables -A INPUT  -p tcp -m tcp --sport 1024: --dport 1024: -m conntrack --ctstate ESTABLISHED -j ACCEPT -m comment --comment "Allow passive inbound connections"
iptables -A OUTPUT -p tcp -m tcp --sport 1024: --dport 1024: -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT -m comment --comment "Allow passive inbound connections"
并重新启动proftpd和iptables服务。
如何解决此问题?

要允许FTP,服务器上需要以下规则:

  • 允许客户端启动到端口21的控制连接,如下所示:

    iptables -A INPUT  -p tcp -m tcp --dport 21 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT -m comment --comment "Allow ftp connections on port 21"
    iptables -A OUTPUT -p tcp -m tcp --sport 21 -m conntrack --ctstate ESTABLISHED -j ACCEPT -m comment --comment "Allow ftp connections on port 21"
    
    iptables -A OUTPUT -p tcp -m tcp --sport 20 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT -m comment --comment "Allow ftp connections on port 20"
    iptables -A INPUT  -p tcp -m tcp --dport 20 -m conntrack --ctstate ESTABLISHED -j ACCEPT -m comment --comment "Allow ftp connections on port 20"
    
  • 对于活动模式,允许服务器从端口20启动数据连接,如下所示:

    iptables -A INPUT  -p tcp -m tcp --dport 21 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT -m comment --comment "Allow ftp connections on port 21"
    iptables -A OUTPUT -p tcp -m tcp --sport 21 -m conntrack --ctstate ESTABLISHED -j ACCEPT -m comment --comment "Allow ftp connections on port 21"
    
    iptables -A OUTPUT -p tcp -m tcp --sport 20 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT -m comment --comment "Allow ftp connections on port 20"
    iptables -A INPUT  -p tcp -m tcp --dport 20 -m conntrack --ctstate ESTABLISHED -j ACCEPT -m comment --comment "Allow ftp connections on port 20"
    
  • 对于被动模式,允许客户端在非特权端口上启动数据连接:

    iptables -A INPUT -p tcp -m tcp --sport 1024: --dport 1024: -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT -m comment --comment "Allow passive inbound connections"
    iptables -A OUTPUT -p tcp -m tcp --sport 1024: --dport 1024: -m conntrack --ctstate ESTABLISHED -j ACCEPT -m comment --comment "Allow passive inbound connections"
    
  • 普通的
    conntrack
    模块应在主动模式下建立
    相关的
    数据连接时正确跟踪,但是,在被动模式下建立此类连接时,您可能需要加载
    nf\u conntrack\u ftp
    模块以正确跟踪:

    • 检查它是否加载了
      lsmod | grep nf_conntrack_ftp
    • 使用
      modprobe nf\u conntrack\u ftp加载它
    或者,您可以将
    相关的
    状态替换为
    新的
    状态,该状态不太安全,但肯定会完成任务


    提供上述规则基本原理的简明摘要。

    为了允许FTP,您需要在服务器上使用以下规则:

  • 允许客户端启动到端口21的控制连接,如下所示:

    iptables -A INPUT  -p tcp -m tcp --dport 21 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT -m comment --comment "Allow ftp connections on port 21"
    iptables -A OUTPUT -p tcp -m tcp --sport 21 -m conntrack --ctstate ESTABLISHED -j ACCEPT -m comment --comment "Allow ftp connections on port 21"
    
    iptables -A OUTPUT -p tcp -m tcp --sport 20 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT -m comment --comment "Allow ftp connections on port 20"
    iptables -A INPUT  -p tcp -m tcp --dport 20 -m conntrack --ctstate ESTABLISHED -j ACCEPT -m comment --comment "Allow ftp connections on port 20"
    
  • 对于活动模式,允许服务器从端口20启动数据连接,如下所示:

    iptables -A INPUT  -p tcp -m tcp --dport 21 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT -m comment --comment "Allow ftp connections on port 21"
    iptables -A OUTPUT -p tcp -m tcp --sport 21 -m conntrack --ctstate ESTABLISHED -j ACCEPT -m comment --comment "Allow ftp connections on port 21"
    
    iptables -A OUTPUT -p tcp -m tcp --sport 20 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT -m comment --comment "Allow ftp connections on port 20"
    iptables -A INPUT  -p tcp -m tcp --dport 20 -m conntrack --ctstate ESTABLISHED -j ACCEPT -m comment --comment "Allow ftp connections on port 20"
    
  • 对于被动模式,允许客户端在非特权端口上启动数据连接:

    iptables -A INPUT -p tcp -m tcp --sport 1024: --dport 1024: -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT -m comment --comment "Allow passive inbound connections"
    iptables -A OUTPUT -p tcp -m tcp --sport 1024: --dport 1024: -m conntrack --ctstate ESTABLISHED -j ACCEPT -m comment --comment "Allow passive inbound connections"
    
  • 普通的
    conntrack
    模块应在主动模式下建立
    相关的
    数据连接时正确跟踪,但是,在被动模式下建立此类连接时,您可能需要加载
    nf\u conntrack\u ftp
    模块以正确跟踪:

    • 检查它是否加载了
      lsmod | grep nf_conntrack_ftp
    • 使用
      modprobe nf\u conntrack\u ftp加载它
    或者,您可以将
    相关的
    状态替换为
    新的
    状态,该状态不太安全,但肯定会完成任务

    提供上述规则的基本原理的简明摘要