firewalld防火墙禁止海外IP访问实例
环境:centos7.9 firewalld v0.6.3 ipset v7.1
- 禁止海外IP访问有两种方式:(本例以白名单方式)
- 黑名单(允许所有IP,禁止海外IP)
- 白名单(禁止所有IP,允许国内IP)
1、准备国内IP地址文件
- IP地址收集整理:
- http://ip.bczs.net/country/CN
- https://qiniu.wsfnk.com/china_ip.txt
- https://github.com/17mon/china_ip_list/blob/master/china_ip_list.txt
- https://www.isres.com/china_ip_list.txt
- https://raw.githubusercontent.com/17mon/china_ip_list/master/china_ip_list.txt
[root@localhost ~]# curl -O https://www.isres.com/china_ip_list.txt 或 [root@localhost ~]# wget https://www.isres.com/china_ip_list.txt
2、查看 firewalld 所支持的 IP 集设置类型(选项可略过,需要以root身份执行)
[root@localhost ~]# firewall-cmd --get-ipset-types hash:ip hash:ip,mark hash:ip,port hash:ip,port,ip hash:ip,port,net hash:mac hash:net hash:net,iface hash:net,net hash:net,port hash:net,port,net
3、创建ipset的IP集合(需要以root身份,且在permanent环境中)
[root@localhost ~]# firewall-cmd --permanent --new-ipset=china_ip --type=hash:ip success [root@localhost ~]# firewall-cmd --permanent --get-ipsets #查询ip集合 china_ip [root@localhost ~]# firewall-cmd --permanent --ipset=china_ip --add-entries-from-file=china_ip_list.txt success
4、添加白名单(富规则)
[root@localhost ~]# firewall-cmd --permanent --add-rich-rule 'rule family="ipv4" source ipset="china_ip" port port=80 protocol=tcp accept' [root@localhost ~]# firewall-cmd --permanent --add-rich-rule 'rule family="ipv4" source ipset="china_ip" port port=443 protocol=tcp accept' [root@localhost ~]# firewall-cmd --reload [root@localhost ~]# firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: ens33 ens36 sources: services: dhcpv6-client ssh ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: rule family="ipv4" source ipset="china" port port="443" protocol="tcp" accept rule family="ipv4" source ipset="china" port port="80" protocol="tcp" accept
5、测试(过程略)
- 配置海外ip进行访问测试(61.32.0.0/24是韩国IP可配置测试)
- 服务器端可以安装nginx或httpd
注意:防火墙启用配置规则前请确保自己内网访问服务器正常,已免造成不能访问故障,建议单独使用一台服务器做隔离且测试正常后投入生产,重要!重要!重要!
如果使用iptables请参考 https://www.qunniao.net/1358.html 应用实例
6、添加、删除集合IP(扩展)
[root@localhost ~]# firewall-cmd --permanent --ipset=china --add-entry="10.1.1.0/24" [root@localhost ~]# firewall-cmd --permanent --ipset=china --add-entry="10.0.0.1" [root@localhost ~]# firewall-cmd --permanent --ipset=china --remove-entry="10.0.0.1" [root@localhost ~]# firewall-cmd --reload
温馨提示:如无特殊说明,本站文章均为作者原创,转载请注明出处!