CentOS 7 防火墙详细配置

Detailed Configuration of CentOS 7 Firewall

Posted by ifeng on October 5, 2022

这篇文章转载自 HiFeng’Blog ,转载请保留链接 ;)

1、查看防火墙状态

1
systemctl status firewalld

2、开启防火墙

1
systemctl start firewalld

3、关闭防火墙

1
systemctl stop firewalld

4、设置开机启动

1
systemctl enable firewalld

5、禁止开机启动

1
systemctl disable firewalld

6、查看防火墙配置

1
2
firewall-cmd --state
firewall-cmd --list-all

7、查询端口是否开放

1
firewall-cmd --query-port=8080/tcp

8、允许80端口访问

1
2
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload    #重新加载防火墙配置才会起作用

9、移除80端口

1
2
firewall-cmd --permanent --remove-port=80/tcp
firewall-cmd --reload

10、允许某个端口段

1
2
firewall-cmd --permanent --zone=public --add-port=1000-2000/tcp
firewall-cmd --reload

11、允许某个IP访问,默认允许

1
2
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=192.168.1.169 accept'
firewall-cmd --reload

12、禁止某个IP访问

1
2
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=10.0.0.42 drop'
firewall-cmd --reload

13、允许某个IP访问某个端口

1
2
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=192.168.1.169 port protocol=tcp port=6379 accept'
firewall-cmd --reload

14、移除以上规则

1
2
firewall-cmd --permanent --remove-rich-rule='rule family="ipv4" source address="192.168.1.169" port port="6379" protocol="tcp" accept'
firewall-cmd --reload

15、允许某个IP段访问

1
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=10.0.0.0/24 accept'

16、重启防火墙

1
firewall-cmd --reload