使用IPTABLES的IPRANGE模块来屏蔽一段IP

本文编写于3455天前,最后编辑于 3455天前,部分内容可能已经过时,请您自行斟酌确认。

默认情况下我们可以使用子网掩码来屏蔽一个ip段,例如下面就是屏蔽了192.168.0.0-192.168.0.255整个段的IP

iptables -A INPUT -s 192.168.0.0/24 -j DROP

如果我们需要屏蔽10-20这几个ip,上面的方式就不够灵活了。
我们可以使用iptables的iprange模块来达到上面的目的。
下面是通过man查看到的iptables iprange模块的帮助信息。

iprange
       This matches on a given arbitrary range of IP addresses.
 
       [!] --src-range from[-to]
              Match source IP in the specified range.  #源IP
 
       [!] --dst-range from[-to]
              Match destination IP in the specified range.  #目的IP

如果要拒绝10-20的访问这几个ip,我们可以这样使用:

iptables -A INPUT -m iprange --src-range 192.168.0.10-192.168.0.20 -j DROP

如果要拒绝访问10-20这几个ip,我们可以这样使用。

iptables -A OUTPUT -m iprange --dst-range 192.168.0.10-192.168.0.20 -j DROP

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注