Centos7防火墙设置

作者:Keysqiu
创建时间:2025-11-30 18:19:01 最后一次修改时间:2025-11-30 18:19:01
Categories: Tags:

CentOS 7.0默认使用的是firewall作为防火墙, 使用systemctl来管理服务和程序,包括了service和chkconfig

1、检查是否已安装防火墙

yum install firewalld firewall-config

2、查看默认防火墙状态(开启后显示running,关闭后显示not running)

firewall-cmd --state

3、检查防火墙的状态(enabled表示正常运行,disabled表示防火墙已经关闭)

systemctl list-unit-files|grep firewalld.service

4、开启防火墙

systemctl start firewalld.service  		--启动firewall
systemctl enable firewalld.service		--开机时启动firewall

5、关闭防火墙

systemctl stop firewalld.service    		--停止firewall
systemctl disable firewalld.service		--禁止firewall开机启动

6、重启防火墙

systemctl restart firewalld.service

7、查看防火墙是否开机自启(enabled表示自启,disabled表示不自启)

systemctl is-enabled firewalld.service;echo $?

8、查看已启动的服务列表

systemctl list-unit-files|grep enabled

9、开启端口

# 这里开启的是80端口,开启其他端口可自定义
firewall-cmd --zone=public --add-port=80/tcp --permanent
# 开启之后一定要重启一下防火墙,不然不生效!!!
firewall-cmd --reload  
# 命令含义:
–zone #作用域
–add-port=80/tcp #添加端口,格式为:端口/通讯协议
–permanent #永久生效,没有此参数重启后失效

10、查看已经开放的端口

firewall-cmd --permanent --list-port

11、屏蔽FirewallD服务

systemctl mask firewalld

12、反屏蔽FirewallD服务

# 这是反屏蔽FirewallD服务,它会移除屏蔽FirewallD服务时创建的符号链接,故能重新启用服务。(主要针对上面第4点的开机自启服务)
systemctl unmask firewalld