CentOS7使用ipset快速屏蔽指定国家的IP访问

#创建一个名为cnblocks的规则
ipset -N cnblocks hash:net
#下载国家IP段到当前目录
wget -P . http://www.ipdeny.com/ipblocks/data/countries/cn.zone
#将IP段添加到cnblocks规则中(firewalld)
for i in $(cat /root/cn.zone ); firewall-cmd --permanent --ipset=cnblocks --add-entry=$i; done
CentOS7中自带的是firewalld,添加到规则中时用上面这条命令
如果你换成了iptables,那就用下面这条命令
#将IP段添加到cnblocks规则中(iptables)
for i in $(cat /root/cn.zone ); do ipset -A cnblocks $i; done

开始屏蔽

	
#firewalld
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source ipset=cnblocks drop'
firewall-cmd --reload
 
#iptables
iptables -I INPUT -p tcp -m set --match-set cnblocks src -j DROP
service iptables save

解除屏蔽

#firewalld
firewall-cmd --permanent --remove-rich-rule='rule family=ipv4 source ipset=cnblocks drop'
firewall-cmd --permanent --delete-ipset=cnblocks
firewall-cmd --reload
 
#iptables
iptables -D INPUT -p tcp -m set --match-set cnblocks src -j DROP
ipset destroy cnblocks
service iptables save

清空之前的规则

CentOS 7还需要关闭firewall防火墙:
systemctl stop firewalld.service
systemctl disable firewalld.service

#防止设置不生效,建议清空下之前的防火墙规则
iptables -P INPUT ACCEPT
iptables -F
#关闭指定端口,比如80/443
iptables -A INPUT -p tcp --dport 80 -j DROP
iptables -A INPUT -p tcp --dport 443 -j DROP
#查看
iptables -L INPUT
或
iptables -L
或
iptables-save(此命令将保存规则,下次开机自动执行)

重启iptables服务:
service iptables restart

执行完毕之后/etc/syscofig/iptables文件就有了

然后我们在执行一些chkconfig,看看 六个状态下的命令开启设置,因为这个是涉及到重启以后是否你修改的命令~~

那chkconfig iptables off(设置自动启动为关闭)

chkconfig iptables on(设置自动启动为启动)

chkconfig --del iptables(移除开启自启动)

chkconfig --add iptables(增加开启自启动)

版权声明:
作者:wanghaha
链接:http://www.aiii.vip/479.html
来源:我的生活分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>