云服务器
centos7如何使用firewalld打开关闭防火墙与端口
2025-07-17 16:43  点击:0
在CentOS 7上,可以使用firewalld工具来管理和控制防火墙,包括打开或关闭特定的端口。以下是一些基本操作,以帮助您理解和配置firewalld。

1. 检查firewalld状态
shell
sudo systemctl status firewalld


2. 启动或停止firewalld
shell
sudo systemctl start firewalld


shell
sudo systemctl stop firewalld


3. 设置firewalld在系统启动时自动运行
shell
sudo systemctl enable firewalld


4. 永久关闭或打开防火墙
shell
sudo systemctl disable firewalld


shell
sudo systemctl enable firewalld


5. 添加或移除端口规则
- 打开端口
shell
sudo firewall-cmd --zone=public --add-port=port_number/tcp --permanent

例如,要永久打开80端口
shell
sudo firewall-cmd --zone=public --add-port=80/tcp --permanent


- 关闭端口
shell
sudo firewall-cmd --zone=public --remove-port=port_number/tcp --permanent

例如,要永久关闭80端口
shell
sudo firewall-cmd --zone=public --remove-port=80/tcp --permanent


6. 重新加载防火墙配置
shell
sudo firewall-cmd --reload


7. 查看当前规则
shell
sudo firewall-cmd --zone=public --list-all


请注意,在上述命令中,--zone=public 指的是将规则应用于公共区域。您可以根据需要将其更改为其他区域,例如 --zone=home--zone=work

firewalld提供了灵活的配置选项,可以根据具体需求来设置规则。如果需要更多高级配置或有特定的安全需求,建议参考firewalld的官方文档或咨询系统管理员。