需求:禁止国外IP访问,仅中国大陆地区访问,以防止海外IP对网站的攻击
下载国内IP地址,用作白名单,文件名china_ip.txt
wget -q --timeout=60 -O- 'http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest' | awk -F\| '/CN\|ipv4/ { printf("%s/%d\n", $4, 32-log($5)/log(2)) }' > /root/china_ip.txt
检查防火墙状态
systemctl status firewalld
防火墙开启命令
systemctl start firewalld
防火墙关闭命令
systemctl stop firewalld
新建IP集合并把IP白名单文件加入集合
firewall-cmd --permanent --new-ipset=china_ip --type=hash:net
firewall-cmd --permanent --ipset=china_ip --add-entries-from-file=china_ip.txt
添加规则并重载配置
firewall-cmd --permanent --add-rich-rule 'rule family="ipv4" source ipset="china_ip" port port=80 protocol=tcp accept'
firewall-cmd --permanent --add-rich-rule 'rule family="ipv4" source ipset="china_ip" port port=8080 protocol=tcp accept'
firewall-cmd --permanent --add-rich-rule 'rule family="ipv4" source ipset="china_ip" port port=443 protocol=tcp accept'
firewall-cmd --reload
验证
在关闭防火墙的状态下此时国内国外都可以正常访问网站,然后在开启防火墙的情况下国外访问失败,国内访问不受影响。
评论 暂无