疑难杂症:为何我始终没有办法禁止掉来自192.168.49.0/24的流量,以下两种方式都试了,下面是iptables规则
[root@centos-linux-02 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT icmp -- anywhere anywhere
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
DROP tcp -- 192.168.49.0/24 anywhere tcp dpt:ssh
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
[root@centos-linux-02 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT icmp -- anywhere anywhere
DROP tcp -- 192.168.49.0/24 anywhere tcp dpt:ssh
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
把drop这条放在最上面,drop tcp 改为 drop all 。试一下
可是我只想禁止192.168.49.0/24段的啊,你说的那样肯定可以禁止访问,可是都禁止掉了啊
原因找到了,编辑iptables之前,首先要iptables -F去清除默认规则表,不然会影响接下来写入的匹配。
可以这样写:
iptables -I INPUT -s 192.168.49.0/24 -p tcp --dport 22 -j DROP
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
或者:
iptables -I INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -s 192.168.49.0/24 -p tcp --dport 22 -j DROP