在 CentOS 7/8 中,iptables 与 firewalld 不能直接“共存”同时生效,因为它们的底层机制存在冲突。下面分情况说明,并给出可行方案。
firewalld 和 iptables 服务不能同时运行
但 iptables 命令(netfilter)与 firewalld 可以“间接共存”
原因:
| 方式 | 说明 | 是否推荐 |
|---|---|---|
| firewalld | CentOS 7/8 默认 | ✅ 推荐 |
| iptables-service | 传统方式 | ⚠️ 仅兼容 |
| 手写 iptables 命令 | 临时 | ❌ 不建议长期 |
systemctl status firewalld
systemctl start firewalld
systemctl enable firewalld
然后 完全不要启用 iptables 服务
systemctl disable iptables
systemctl stop iptables
✅ firewalld 本质就是 iptables/nftables 的“前端管理器”
适合老系统或老运维习惯
systemctl stop firewalld
systemctl disable firewalld
yum install -y iptables-services
systemctl enable iptables
systemctl start iptables
规则文件:
/etc/sysconfig/iptables
iptables -I / -A 做 临时调试iptables-service⚠️ firewalld reload 会清空你手写的 iptables 规则
firewall-cmd --add-rich-rule='
rule family="ipv4" source address="192.168.1.100" port port="8080" protocol="tcp" accept'
firewall-cmd --direct --add-rule ipv4 filter INPUT 0 \
-p tcp --dport 3306 -j ACCEPT
查看:
firewall-cmd --direct --get-all-rules
CentOS 8:
iptables -L
本质是:
nftables → iptables compatibility
❌ 同时启用:
systemctl start firewalld
systemctl start iptables
❌ 写了 iptables 规则,却重启 firewalld
| 场景 | 建议 |
|---|---|
| CentOS 7/8 新系统 | ✅ firewalld |
| 老脚本依赖 iptables | ✅ 关闭 firewalld |
| 临时测试 | ⚠️ 手动 iptables |
| 生产环境 | ✅ firewalld + rich/direct rule |
iptables 和 firewalld 不能“同时作为服务共存”
要么 firewalld,要么 iptables-service,二选一
如果你愿意,可以告诉我:
我可以给你一套完整迁移方案。