我已经添加了一些基本的规则:
iptables -A INPUT -p tcp --dport 22 -j ACCEPT iptables -A INPUT -p tcp --dport 80 -j ACCEPT
然后,使用以下命令closures所有其他端口:
iptables -A INPUT -j DROP
它完美的工作,我testing它:
% telnet xxxx 81 Trying xxxx.. telnet: connect to address xxxx: Operation timed out telnet: Unable to connect to remote host
但是,当我列出规则,我看到policy ACCEPT :
Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT tcp -- anywhere anywhere tcp dpt:ssh ACCEPT tcp -- anywhere anywhere tcp dpt:http DROP all -- anywhere anywhere
我知道如何改变它,用iptables -P INPUT DROP ,然后它变成:
Chain INPUT (policy DROP) target prot opt source destination ACCEPT tcp -- anywhere anywhere tcp dpt:ssh ACCEPT tcp -- anywhere anywhere tcp dpt:http DROP all -- anywhere anywhere
但是我不明白区别,因为它和以前一样。 我读过这篇文章,build议将政策改为DROP,但为什么我应该这样做呢? 上面我的iptables有什么区别?
iptables DROP策略相当于链表末尾的iptables -A INPUT -j DROP (DROP规则)。 但是这个规则必须停留在链的末尾 ,任何规则之后都不会被任何包所触及。
如果使用DROP规则,就不能再使用iptables -A (append),只能使用iptables -I nr (insert,nr是最后一条规则的编号)并在最后一条规则之前插入规则。 为了使这个插入工作,你必须知道你的最后一条规则的规则编号,当然这个编号会改变,所以脚本变得更加困难。 您可以使用DROP策略为您节省一些困难。
最后,如果我从头开始知道不符合任何规则的数据包是如何被使用的话,它会使整体更具可读性。
还有其他的想法吗?