CentOS 防火墙(firewalld)是 CentOS 7 及以后版本中默认的动态防火墙管理工具,支持网络区域(zones)概念,可以根据不同的信任级别来管理网络连接和端口。以下是一些常用的 firewalld 安全策略操作:
服务管理:
systemctl status firewalld
systemctl start firewalld
systemctl stop firewalld
(注意:停止防火墙会使系统暴露在网络风险中,不推荐在生产环境操作)systemctl restart firewalld
systemctl enable firewalld
systemctl disable firewalld
防火墙状态与规则查看:
firewall-cmd --state
(输出 running 表示正在运行)firewall-cmd --list-all
(这将显示默认区域的名称、接口、已开放的服务、端口、协议、转发规则、伪装状态等)firewall-cmd --list-all-zones
(如果你需要管理多个网络区域,这个命令很有用)理解区域(Zones):
firewall-cmd --get-default-zone
firewall-cmd --get-active-zones
firewall-cmd --set-default-zone=home
端口管理:
--permanent
的更改只在当前运行时生效,firewalld 重启或系统重启后会丢失。带 --permanent
的更改会写入配置文件,需要执行 firewall-cmd --reload
或重启 firewalld 服务后才会生效,并且会持久保留。firewall-cmd --permanent --query-port=8080/tcp
(返回 yes 或 no)# 开放单个端口 80/tcp
firewall-cmd --permanent --add-port=80/tcp
# 开放端口范围 8080-8090/tcp
firewall-cmd --permanent --add-port=8080-8090/tcp
# 开放 UDP 端口 53
firewall-cmd --permanent --add-port=53/udp
firewall-cmd --permanent --remove-port=8080/tcp
--permanent
更改生效):firewall-cmd --reload
(这是非常重要的一步,所有带 --permanent
的修改都需要 reload 后才能激活)firewall-cmd --list-ports
(要查看持久配置中开放的端口,使用 firewall-cmd --permanent --list-ports
)基于预定义服务:
firewall-cmd --get-services
# 开放 http 服务(通常是 80/tcp)
firewall-cmd --permanent --add-service=http
# 开放 https 服务(通常是 443/tcp)
firewall-cmd --permanent --add-service=https
重新加载防火墙:
firewall-cmd --reload
firewall-cmd --complete-reload
(通常在防火墙出现严重问题时使用)配置文件管理:
/usr/lib/firewalld/services/
目录中,保存了 daemon 文件,每个文件对应一项具体的网络服务,如 samba 服务等。/etc/firewalld/services/
目录中,每加载一项已设置好的 service 配置意味着开放了对应的端口访问。通过这些命令和策略,可以有效地管理和配置 CentOS 防火墙,以确保系统的安全性。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: centos下如何进行java编译调试