Iptables在“-A INPUT -j REJECT –reject-with icmp-host-prohibited”之后追加规则默认值

我使用iptables在CentOS 6.4上为HTTP打开端口80我通常使用vim来编辑/ etc / sysconfig / iptables但是这次我使用/ sbin / iptables命令。

# /sbin/iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT # service iptables save # service iptables restart 

当我列出规则,我可以看到这样的http:

 ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh REJECT all -- anywhere anywhere reject-with icmp-host-prohibited ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:http 

但我无法从其他机器连接到Web服务器我检查了iptables文件,我看到这样的内容:

 *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [88:9264] -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT 

我不得不手动把行:

 -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT 

之前

 -A INPUT -j REJECT --reject-with icmp-host-prohibited 

然后当我重新启动iptables服务。 有效!

那么如何以正确的方式添加新的规则呢? 谢谢!

iptables的-A命令只是“追加”一个规则。 所以如果你现在的规则是这样的:

 ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh REJECT all -- anywhere anywhere reject-with icmp-host-prohibited 

你运行:

 # /sbin/iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT 

那么当然这会在REJECT规则之后结束(因为你告诉它把规则附加到现有的rulset)。 你有几个select:

  1. 你可以直接编辑/etc/sysconfig/iptables ,插入你想要的规则,然后运行service iptables restart
  2. 您可以使用lokkit工具来修改防火墙。 例如, lokkit -p 80:tcp 。 这将自动更新/etc/sysconfig/iptables以及主动防火墙。
  3. 您可以使用-I <num>标志将iptables插入列表中的指定位置。 --line-numbers标志可以用来确定<num>应该是什么。 这样做后,你需要运行service iptables save

如果你真的想用append命令来做这种事情,那么你需要首先执行一些设置。 创build一个新的链(称为,也许, allow_services ):

 iptables -N allow_services 

并在适当的地方添加一条规则到你的INPUT链,跳到这个新的链:

 iptables -I INPUT 5 -j allow_services 

从这一点开始,您可以简单地将新服务附加到allow_services链:

 iptables -A allow_services -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT 

假设你在最后的REJECT之前放置你的跳跃规则( -j选项),这将会做你似乎在问的东西。