防火墙 – 无法在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 my_host 3500时,我得到这个:telnet:无法连接到远程主机:连接被拒绝

编辑2:我的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:*听
tcp 0 0 127.0.0.1:25 0.0.0.0:*听听
tcp 0 0 ::: 22 ::: * LISTEN

编辑3:我跟着lain建议,也让我的服务绑定到0.0.0.0:3500而不是127.0.0.1:3500它的工作原理.

列出了您的规则,根据IANA port/service名称,rtmp-port是端口3500.要获取端口号列表而不是服务名称,请使用-n开关
iptables -L -n

您还使用-A开关将规则添加到INPUT链.这是在数据包发送到RH-Firewall-1-INPUT链之后添加的,其最后一条规则是一揽子REJECT,因此发往端口3500的数据包将在它们在INPUT链中进行测试之前被拒绝.

您有几种可能的解决方案 – 使用-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的现有规则.

相关文章

Centos下搭建性能监控Spotlight
CentOS 6.3下Strongswan搭建IPSec VPN
在CentOS6.5上安装Skype与QQ
阿里云基于centos6.5主机VPN配置
CentOS 6.3下配置multipah
CentOS安装、配置APR和tomcat-native