设置通用的iptables规则?

这可能是一个非常愚蠢的问题,但是如何在不定义接口的情况下在多个接口上打开一个端口? 例如,我如何打开所有接口上的端口22?

在我的机器上,我有一些dynamic的接口,可能或不可用,所以我必须设置“通用”的规则。

此代码不适合我,但我不明白为什么:

# My default policy is to drop the input. # The other policies are required like that. $IPTABLES -P INPUT DROP $IPTABLES -P OUTPUT ACCEPT $IPTABLES -P FORWARD ACCEPT $IPTABLES -t nat -P POSTROUTING ACCEPT $IPTABLES -t nat -P PREROUTING ACCEPT $IPTABLES -t mangle -P OUTPUT ACCEPT $IPTABLES -t mangle -P PREROUTING ACCEPT #Open port 22 on all interfaces ? $IPTABLES -A INPUT -p tcp --dport 22 -j ACCEPT 

所以显然最后一行有什么问题,但我看不到呢?

编辑:iptables -nvL

 root@machine:/etc/rc.d# iptables -nvL Chain INPUT (policy DROP 22 packets, 1378 bytes) pkts bytes target prot opt in out source destination 18 1484 ACCEPT all -- * * 192.168.0.0/24 192.168.0.1 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 4 236 ACCEPT all -- eth0 * 0.0.0.0/0 0.0.0.0/0 0 0 DROP all -- eth1 * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT all -- 192.168.0.1 * 0.0.0.0/0 0.0.0.0/0 24 1362 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:53 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:53 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:80 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:443 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:443 Chain FORWARD (policy ACCEPT 490 packets, 194K bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 49 packets, 35544 bytes) pkts bytes target prot opt in out source destination 

根据你的iptables -nvL输出, 你有一个规则来删除eth1中的所有stream量 。 这可能是你的问题。

您要添加的规则使用-A INPUT ,因此它将规则附加到链的末尾。 鉴于你的脚本中没有其他的规则在你的-nvL输出中,我猜你还有一些其他规则正在其他地方被首先应用。

这里的解决scheme是将-A INPUT改为-I INPUT ,这将在DROP规则之前的开始处添加规则。