不能在CentOS上修改iptables

我的问题:如何添加一个自定义的iptables规则来接受某个端口上的连接?

我试图打开我的服务器上的端口3500,但失败。 我开始使用这个命令:(从http://wiki.centos.org/HowTos/Network/IPTables )

iptables -A INPUT -p tcp --dport 3500 -j ACCEPT 

但后来我运行iptables -L我仍然没有看到新的规则被列出:(我的假设它应该包括在输出3500)

 Chain INPUT (policy ACCEPT) target prot opt source destination RH-Firewall-1-INPUT all -- anywhere anywhere ACCEPT tcp -- anywhere anywhere tcp dpt:ssh ACCEPT tcp -- anywhere anywhere tcp dpt:rtmp-port Chain FORWARD (policy ACCEPT) target prot opt source destination RH-Firewall-1-INPUT all -- anywhere anywhere Chain OUTPUT (policy ACCEPT) target prot opt source destination Chain RH-Firewall-1-INPUT (2 references) target prot opt source destination ACCEPT all -- anywhere anywhere ACCEPT icmp -- anywhere anywhere icmp any ACCEPT esp -- anywhere anywhere ACCEPT ah -- anywhere anywhere ACCEPT udp -- anywhere 224.0.0.251 udp dpt:mdns ACCEPT udp -- anywhere anywhere udp dpt:ipp ACCEPT tcp -- anywhere anywhere tcp dpt:ipp ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED ACCEPT udp -- anywhere anywhere state NEW udp dpt:snmp ACCEPT udp -- anywhere anywhere state NEW udp dpt:snmptrap ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:http ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ftp REJECT all -- anywhere anywhere reject-with icmp-host-prohibited 

编辑所以我尝试插入ACCEPT规则到input链,我的iptables现在看起来像这样:

 Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:3500 RH-Firewall-1-INPUT all -- 0.0.0.0/0 0.0.0.0/0 

但是它不允许我从外部连接到3500。 (我仍然可以从内部telnet)。 当我试图telnet my_host 3500,我得到这个: telnet: Unable to connect to remote host: Connection refused

编辑2 :我的netstat -an | grep "LISTEN " netstat -an | grep "LISTEN "输出:

tcp 0 0 127.0.0.1:3500 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:973 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 ::: 22 ::: * LISTEN

编辑3 :我跟着lain劝告,也有我的服务绑定到0.0.0.0:3500而不是127.0.0.1:3500,它的工作原理。

您的规则已列出,根据IANA 端口/服务名称, rtmp-port是3500 端口 。 要获得端口号列表而不是其服务名称,请使用-n开关

 iptables -L -n 

您还使用-A开关将您的规则添加到INPUT链。 在数据包被发送到RH-Firewall-1-INPUT链后,这个数据包被添加,最后一个规则是全局REJECT,所以发往端口3500的数据包将在被INPUT链testing之前被拒绝。

您有几个可能的解决scheme – 使用-I开关将您的规则插入到INPUT链或RH-Firewall-1-INPUT

 iptables -I INPUT -p tcp --dport 3500 -j ACCEPT 

要么

 iptables -I RH-Firewall-1-INPUT -p tcp --dport 3500 -j ACCEPT 

你也许应该清理你的规则,你可以使用

 iptables -D INPUT -p tcp --dport 3500 -j ACCEPT 

(多次)在添加新规则之前删除端口3500的现有规则。