CentOS7防火墙配置

229 阅读1分钟

Firewalld服务是红帽RHEL7系统中默认的防火墙管理工具,其防火墙策略是由内核层面的nftables包过滤框架来处理,firewalld支持动态更新技术并加入了区域(zone)的概念,使用图形化工具firewall-config或文本管理工具firewall-cmd来进行策略配置。

1. 服务说明

编号服务命令命令含义备注
1systemctl start firewalld启动防火墙
2systemctl restart firewalld重启防火墙
3systemctl stop firewalld停止防火墙
4systemctl status firewalld防火墙服务运行状态

2. 基础命令

2.1. 查看防火墙配置

[root@hdp1 ~]$ firewall-cmd --state

image.png

# 防火墙配置规则
[root@hdp1 ~]$ firewall-cmd --list-all

image.png

2.2. 开放80端口

[root@hdp1 ~]$ firewall-cmd --permanent --add-port=80/tcp 
[root@hdp1 ~]$ firewall-cmd --reload #重新加载防火墙配置才会起作用

image.png

2.3. 移除80端口

[root@hdp1 ~]$ firewall-cmd --permanent --remove-port=80/tcp 
[root@hdp1 ~]$ firewall-cmd --reload

2.4. 开放端口段

[root@hdp1 ~]$ firewall-cmd --permanent --zone=public --add-port=1000-2000/tcp 
[root@hdp1 ~]$ firewall-cmd --reload

2.5. 放通某个IP访问,默认允许

[root@hdp1 ~]$ firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=192.168.10.58 accept'
[root@hdp1 ~]$ firewall-cmd --reload

2.6. 禁止某个IP访问

[root@hdp1 ~]$ firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=192.168.10.10 drop' 
[root@hdp1 ~]$ firewall-cmd --reload

2.7. 开放IP白名单和端口

[root@hdp1 ~]$ firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=192.168.10.3 port protocol=tcp port=3306 accept'
[root@hdp1 ~]$ firewall-cmd --reload

image.png

2.8. 移除2.7中的规则

[root@hdp1 ~]$ firewall-cmd --permanent --remove-rich-rule='rule family="ipv4" source address="192.168.10.3" port port="3306" protocol="tcp" accept'
[root@hdp1 ~]$ firewall-cmd --reload

2.9. 开放某个IP段访问白名单

[root@hdp1 ~]$ firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=192.168.10.0/24 accept'