为什么我的iptablesconfiguration不接受来自指定源iP的连接?

从机器192.168.1.2,我有这个iptables的configuration:

$ hostname -I 192.168.1.2 $ iptables -L -n -v Chain INPUT (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT tcp -- eth0 * 192.168.1.3 0.0.0.0/0 tcp dpt:6379 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT all -- docker0 * 0.0.0.0/0 0.0.0.0/0 943 118K ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 20 988 ACCEPT tcp -- eth0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 Chain FORWARD (policy DROP 5 packets, 300 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 160 packets, 18263 bytes) pkts bytes target prot opt in out source destination 

当我尝试通过端口6379从192.168.1.3连接到上述机器时,它只是挂起,直到我杀了它:

 $ hostname -I 192.168.1.3 $ nc -z 192.168.1.2 6379 ^C 

我在做什么错了,那不允许我通过端口6379从192.168.1.3连接到192.168.1.2?

在尝试连接之后不久,您可能会添加“接受”规则,并被策略取消。 如果是这种情况,可能已经为该stream创build了连接跟踪条目,其中包括过滤决定。 连接跟踪表充当netfilter的一种caching; 它不会评估所有数据包的所有规则。 如果有匹配的连接跟踪条目,则会查询该条目。

总是要确保删除您尝试匹配新规则的conntrack条目,例如使用conntrack(8)工具。 就你而言,你可以做一些类似的事情

 conntrack -D -s 192.168.1.3 

我重置iptables,然后改变我的脚本中的这一行,从这个特定的iptables规则:

 iptables -A INPUT -s 192.168.1.3 -i eth0 -p tcp -m tcp --dport 6379 -j ACCEPT 

对此:

 iptables -A INPUT -i eth0 -p tcp --dport 6379 -s 192.168.1.3 -j ACCEPT 

现在它神奇地工作,即使iptables -L -n -v看起来与以前一样:

 $ iptables -L -n -v Chain INPUT (policy DROP 6 packets, 360 bytes) pkts bytes target prot opt in out source destination 1 60 ACCEPT tcp -- eth0 * 192.168.1.3 0.0.0.0/0 tcp dpt:6379 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT all -- docker0 * 0.0.0.0/0 0.0.0.0/0 311 544K ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 1 60 ACCEPT tcp -- eth0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 255 packets, 28839 bytes) pkts bytes target prot opt in out source destination 

不能说这不是行为我以前没有见过iptables。

编辑:对于任何好奇的人,我实际上使用iptables-persistent ,这是我一直在使用的完整文件:

 *filter :INPUT DROP [0:0] :FORWARD DROP [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -i eth0 -p tcp --dport 6379 -s 192.168.1.3 -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -i docker0 -j ACCEPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -i eth0 -p tcp -m tcp --dport 22 -j ACCEPT COMMIT