firewalld 使用ipset 拒绝国外ip
获取中国ip列表
方法1第三方:
wget https://www.isres.com/china_ip_list.txt
wget https://github.com/17mon/china_ip_list/blob/master/china_ip_list.txt
方法2apnic官方:
http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest
可添加有需要的内网段
echo 192.168.0.0/16 >> china_ip_list.txt
echo 10.10.0.0/16 >> china_ip_list.txt
新建、删除集合
firewall-cmd --permanent --new-ipset=china --type=hash:net
#–option=family=inet6指定该ipset使用IPV6地址
firewall-cmd --permanent --delete-ipset=xx
添加ip,从文件中
firewall-cmd --permanent --ipset=china --add-entries-from-file=china_ip_list.txt
手动添加删除ip
firewall-cmd --permanent --ipset=[ipset_name] --add-entry=[xx.xx.xx.xx]
firewall-cmd --permanent --ipset=[ipset_name] --remove-entry=[xx.xx.xx.xx]
设置富规则,允许国内ip访问所有端口、协议
firewall-cmd --permanent --add-rich-rule 'rule family="ipv4" source ipset="china" accept'
设置富规则,允许国内ip访问指定端口、tcp
firewall-cmd --permanent --add-rich-rule 'rule family="ipv4" source ipset="china" port protocol="tcp" port="18022" accept'
重载才会生效
firewall-cmd --reload
查看ipset名字
firewall-cmd --get-ipsets
查看指定ipset信息
firewall-cmd --info-ipset=china
#ip在一行输出
firewall-cmd --permanent --ipset=china --get-entries
#一行一个ip,方便查看
其他
iptables存在丢失问题,firewalld正常
查看ipset集合的文件位置
firewall-cmd --permanent --path-ipset=sshblock
#/etc/firewalld/ipsets/...
判断一个ip是否在集合中
如果集合是ip段,判断ip结果异常
firewall-cmd --permanent --ipset=china --query-entry