https://onitroad.com 更多教程

在 iptables 中允许 FTP 端口 20/21

登录到 ftp 服务器并按照以下步骤操作。

  1. 编辑文件 /etc/sysconfig/iptables-config 并将“ip_conntrack_ftp”模块添加到“IPTABLES_MODULES=”部分。

条目应如下所示:

IPTABLES_MODULES="ip_conntrack_ftp"
  1. 编辑文件/etc/sysconfig/iptables并确保为端口20/21添加了iptables规则
# vi /etc/sysconfig/iptables
# Firewall configuration written by system-config-firewall
# bananaal customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m tcp --dport 21 -j ACCEPT                        ## 与FTP命令(端口21)相关的规则
-A INPUT -p tcp -m tcp --dport 20 -j ACCEPT                        ## 与FTP数据(端口20)相关的规则
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

注意:iptables 规则的顺序很重要。

  1. 重启iptables服务
# service iptables restart
  1. 运行下面的命令来检查是否加载了 ftp 模块。
# lsmod | grep -i ftp

示例输出:

# lsmod | grep -i ftp
nf_conntrack_ftp       12913  0
nf_conntrack           79357  3 nf_conntrack_ftp,nf_conntrack_ipv4,xt_state
  1. 运行下面的命令来检查是否启用了与 ftp 20 和 21 端口相关的 iptables 规则。
# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ftp
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ftp-data
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ssh
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
  1. 验证是否可以从客户端ftp 成功到ftp 服务器。
CentOS/RHEL:如何配置 iptable 规则以允许 FTP 的20/21端口

iptables 实用程序控制 Linux 内核中的网络包过滤代码。
iptables 功能用于在 Linux 内核中设置、维护和检查 IP 包过滤规则表。
可以定义几个不同的表。
每个表都包含许多内置链,也可能包含用户定义的链。

在 FTP 服务器上,默认情况下 iptables 规则未设置为允许端口 20/21 进行 FTP 连接。
尝试打开 ftp 连接会导致以下错误:

# ftp 192.168.10.10
ftp: connect: No route to host
ftp>
日期:2020-09-17 00:12:12 来源:oir作者:oir