温馨提示×

centos iptables为何被禁用

小樊
36
2026-08-18 05:37:36
栏目: 智能运维

CentOS 中 iptables 被“禁用”或不再使用,通常是系统默认改用 firewalld / nftables 导致的,而不是 iptables 模块被删除。下面按常见情况解释。


一、CentOS 7 以后:firewalld 成为默认防火墙

1️⃣ firewalld 接管了防火墙管理

  • CentOS 7 起默认使用 firewalld
  • firewalld 底层仍然使用 iptables(或 nftables)
  • 不建议直接操作 iptables 命令

结果:

iptables -L

可能看到规则为空,或提示:

iptables: command not found

✅ 这是设计行为,不是故障


二、CentOS 8 / Stream:iptables 被 nftables 替代

2️⃣ iptables 实际是兼容层

在 CentOS 8 / Stream 中:

  • 真正的防火墙框架是 nftables
  • iptables 命令是 nftables 的兼容前端
  • 很多情况下:
    • iptables 不生效
    • 或规则被 firewalld / nftables 覆盖
iptables -L
# 可能看不到任何规则

三、常见“被禁用”的原因总结

原因 说明
firewalld 启用 接管了 netfilter
nftables 替代 iptables 只是兼容层
服务未启动 iptables-service 未安装
命令不存在 只装了最小化系统
规则被清空 firewalld 重启后生效

四、如何确认当前防火墙机制

✅ 查看 firewalld 状态

systemctl status firewalld

✅ 查看是否使用 nftables

systemctl status nftables

✅ 查看 iptables 后端

iptables -V

输出类似:

iptables v1.8.4 (nf_tables)

说明是 nftables 后端


五、如果你“必须用 iptables”

✅ CentOS 7 方案(推荐)

systemctl stop firewalld
systemctl disable firewalld

yum install -y iptables-services
systemctl enable iptables
systemctl start iptables

然后:

iptables -L -n

✅ CentOS 8 / Stream(不推荐)

官方已不推荐继续使用 iptables

如果一定要用:

dnf install iptables-services
systemctl disable firewalld
systemctl enable iptables

⚠️ 但 可能与 nftables 冲突


六、最佳实践建议 ✅

系统 建议
CentOS 7 使用 firewalld
CentOS 8 / Stream 使用 firewalld 或 nftables
老脚本 逐步迁移到 firewalld / nftables

七、一句话总结

CentOS 中 iptables 并不是被“禁用”,而是被 firewalld / nftables 取代,直接操作 iptables 已不再推荐。

如果你愿意,可以告诉我:

  • ✅ CentOS 具体版本
  • ✅ 是服务器 / 云主机 / Docker 环境
  • ✅ 想实现什么规则(端口、转发、限制 IP)

我可以帮你给出等价且推荐的 firewalld / nftables 配置方案

0