firewalld 是 Linux 系统(特别是基于 RHEL/CentOS/Fedora 的系统)中默认使用的动态防火墙管理器,它使用 iptables 或 nftables 作为底层工具,但提供了一个更友好、更动态的管理接口。
以下是关于 firewalld 的核心概念、常用命令及管理技巧的完整指南。
核心概念
- 区域 (Zones):
firewalld 的核心思想是“区域”,每个网络接口可以关联一个区域,每个区域定义了一组允许或拒绝的规则。- 常见区域:
public(默认,仅允许 ssh 和 dhcpv6-client)、trusted(完全信任)、block(拒绝所有)、drop(丢弃所有,无响应)。
- 常见区域:
- 服务 (Services):
为了方便管理,firewalld 预定义了一些常见服务(如http,https,ssh,mysql),你可以通过服务名来开放端口,而不是直接指定端口号。 - 运行时配置 vs 永久配置:
- 运行时 (Runtime):立即生效,重启后失效。
- 永久 (Permanent):写入配置文件,重启后生效,但需要重新加载才能立即生效。
基本操作命令
1 服务管理
# 启动 firewalld 服务 sudo systemctl start firewalld # 设置开机自启 sudo systemctl enable firewalld # 查看防火墙状态 sudo firewall-cmd --state # 查看当前活动的区域 sudo firewall-cmd --get-active-zones # 查看默认区域 sudo firewall-cmd --get-default-zone
2 区域与端口管理
# 查看所有可用区域 sudo firewall-cmd --get-zones # 查看指定区域的所有规则(public 区域) sudo firewall-cmd --zone=public --list-all # 开放端口(仅运行时生效) sudo firewall-cmd --add-port=8080/tcp # 开放端口(永久生效) sudo firewall-cmd --permanent --add-port=8080/tcp # 移除端口(永久生效) sudo firewall-cmd --permanent --remove-port=8080/tcp # 重新加载配置(使永久配置立即生效,不中断现有连接) sudo firewall-cmd --reload
3 服务管理
# 查看预定义的服务列表 sudo firewall-cmd --get-services # 允许 HTTP 服务(运行时) sudo firewall-cmd --add-service=http # 允许 HTTPS 服务(永久) sudo firewall-cmd --permanent --add-service=https # 移除 HTTP 服务 sudo firewall-cmd --permanent --remove-service=http
4 富规则 (Rich Rules)
富规则允许更复杂的逻辑,如限制 IP、指定接口等。
# 允许来自特定 IP 的所有访问 sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.100" accept' # 拒绝来自特定 IP 的 SSH 访问 sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.0.5" service name="ssh" reject' # 查看富规则 sudo firewall-cmd --list-rich-rules
高级功能
1 端口转发 (Port Forwarding)
将外部请求的端口转发到内部其他 IP 或端口。
# 将外部 8080 端口转发到本地 80 端口 sudo firewall-cmd --permanent --add-forward-port=port=8080:proto=tcp:toport=80:toaddr=127.0.0.1 # 或者转发到另一个 IP sudo firewall-cmd --permanent --add-forward-port=port=8080:proto=tcp:toport=80:toaddr=192.168.1.10
2 接口绑定
将网络接口绑定到特定区域。
# 将 eth0 接口绑定到 trusted 区域 sudo firewall-cmd --permanent --zone=trusted --add-interface=eth0 # 重新加载以生效 sudo firewall-cmd --reload
3 日志记录
firewalld 本身不直接记录日志,但可以通过富规则触发 LOG 目标,或者结合 iptables 日志模块。
# 记录所有被拒绝的包(需要配合 iptables 或 nftables 配置) # 更简单的方式是使用 firewalld 的 rich rule 记录特定流量 sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="0.0.0.0/0" service name="ssh" log prefix="SSH_LOG" level="warning" accept'
常见问题与故障排除
Q1: 如何查看防火墙是否正在运行?
sudo systemctl status firewalld
Q2: 修改配置后不生效?
确保使用了 --permanent 并执行了 --reload:
sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --reload
Q3: 如何临时禁用防火墙(仅用于调试)?
sudo systemctl stop firewalld
⚠️ 注意:在生产环境中不建议长期禁用防火墙。
Q4: 如何查看详细的防火墙规则?
# 查看 iptables 规则(如果底层是 iptables) sudo iptables -L -n -v # 查看 nftables 规则(如果底层是 nftables) sudo nft list ruleset
Q5: firewalld 和 iptables 的关系?
firewalld 是 iptables/nftables 的前端管理工具,你不需要直接操作 iptables 命令,除非你需要调试底层规则。
最佳实践
- 始终使用
--permanent和--reload:确保配置在重启后依然有效。 - 使用服务名而非端口号:如
http比80/tcp更易读且不易出错。 - 最小权限原则:只开放必要的端口和服务。
- 定期审计:使用
sudo firewall-cmd --list-all定期检查当前规则。 - 备份配置:
sudo cp /etc/firewalld/zones/public.xml /etc/firewalld/zones/public.xml.bak
首发原创文章,作者:世雄 - 原生数据库架构专家,如若转载,请注明出处:https://idctop.com/article/477719.html



