赞
踩
在前面做端口匹配的时候都是通过--dport
参数来指定一个端口或者连续的一组端口,如果我们想同时对多个不连续的端口添加防火墙规则,那么--dport
参数就无法实现了。
基于这种情况,防火墙提供了-m
参数指定扩展模块,通过multiport
参数就可以同时指定多个不连续的端口号,一条防火墙规则最大支持同时添加15个不同的端口号。
案例:仅允许192.168.20.21访问本机的80、443、20、21、22端口,其余所有主机都不允许访问本机的80、443、20、21、端口。
1)编写防火墙规则
在INPUT链的filter表中添加对应的规则。
1.允许192.168.20.21访问本机的80、443、20、21、22
[root@jxl-1 ~]# iptables -t filter -I INPUT -s 192.168.20.21 -d 192.168.20.20 -p tcp -m multiport --dports 20,21,22,80,443 -j ACCEPT
#还有另一种写法
[root@jxl-1 ~]# iptables -t filter -I INPUT -s 192.168.20.21 -d 192.168.20.20 -p tcp -m multiport --dports 20:22,80,443 -j ACCEPT
2.拒绝所有主机访问本机的TCP端口
[root@jxl-1 ~]# iptables -t filter -A INPUT -p tcp -m multiport --dports 20,21,80,443 -j DROP
2)查看添加的规则
允许192.168.20.21主机访问本机的20,21,22,80,443端口,其余所有主机都不允许访问本机的20,21,80,443端口。
[root@jxl-1 ~]# iptables -L -n -v --line-number
Chain INPUT (policy ACCEPT 653 packets, 601K bytes)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT tcp -- * * 192.168.20.21 192.168.20.20 multiport dports 20,21,22,80,443
2 0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 multiport dports 20,21,80,443
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 676 packets, 993K bytes)
num pkts bytes target prot opt in out source destination
[root@jxl-1 ~]#
3)测试效果
随便选择一个端口,以80为例,可以看到192.168.20.21可以正常使用,而192.168.20.22则无法使用。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。