防火墙 – 无法在CentOS上修改iptables

前端之家收集整理的这篇文章主要介绍了防火墙 – 无法在CentOS上修改iptables前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的问题:如何添加自定义iptables规则以接受某个端口上的连接?

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

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

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

  1. Chain INPUT (policy ACCEPT)
  2. target prot opt source destination
  3. RH-Firewall-1-INPUT all -- anywhere anywhere
  4. ACCEPT tcp -- anywhere anywhere tcp dpt:ssh
  5. ACCEPT tcp -- anywhere anywhere tcp dpt:rtmp-port
  6.  
  7. Chain FORWARD (policy ACCEPT)
  8. target prot opt source destination
  9. RH-Firewall-1-INPUT all -- anywhere anywhere
  10.  
  11. Chain OUTPUT (policy ACCEPT)
  12. target prot opt source destination
  13.  
  14. Chain RH-Firewall-1-INPUT (2 references)
  15. target prot opt source destination
  16. ACCEPT all -- anywhere anywhere
  17. ACCEPT icmp -- anywhere anywhere icmp any
  18. ACCEPT esp -- anywhere anywhere
  19. ACCEPT ah -- anywhere anywhere
  20. ACCEPT udp -- anywhere 224.0.0.251 udp dpt:mdns
  21. ACCEPT udp -- anywhere anywhere udp dpt:ipp
  22. ACCEPT tcp -- anywhere anywhere tcp dpt:ipp
  23. ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
  24. ACCEPT udp -- anywhere anywhere state NEW udp dpt:snmp
  25. ACCEPT udp -- anywhere anywhere state NEW udp dpt:snmptrap
  26. ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
  27. ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:http
  28. ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ftp
  29. REJECT all -- anywhere anywhere reject-with icmp-host-prohibited

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

  1. Chain INPUT (policy ACCEPT)
  2. target prot opt source destination
  3. ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:3500
  4. 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开关
  1. iptables -L -n

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

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

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

要么

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

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

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

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

猜你在找的CentOS相关文章