温馨提示×

CentOS怎么开启iptables

小樊
33
2026-07-19 07:00:16
栏目: 智能运维

CentOS 7 及以后版本,系统默认使用的是 firewalld,而不是传统的 iptables 服务。如果你想“开启 iptables”,通常有两种情况,我分别说明。


一、CentOS 7 / 8 / Stream:使用 iptables(而不是 firewalld)

1️⃣ 停止并禁用 firewalld

systemctl stop firewalld
systemctl disable firewalld

2️⃣ 安装 iptables 服务

yum install -y iptables-services

3️⃣ 启动并设置开机自启

systemctl start iptables
systemctl enable iptables

4️⃣ 查看 iptables 状态

systemctl status iptables

iptables -L -n

✅ 到此,iptables 已成功开启。


二、CentOS 6(老系统)

CentOS 6 默认使用 iptables:

service iptables start
chkconfig iptables on

查看状态:

service iptables status

三、CentOS 7+ 但只想用 iptables 命令(不装服务)

⚠️ 注意:firewalld 运行时,直接用 iptables 命令修改的规则 重启后会丢失

如果你只是临时用:

iptables -L -n
iptables -I INPUT -p tcp --dport 80 -j ACCEPT

不推荐 长期使用这种方式。


四、保存 iptables 规则(重要)

CentOS 7+

service iptables save
# 或
/usr/libexec/iptables/iptables.init save

规则会保存到:

/etc/sysconfig/iptables

五、建议(很重要)

场景 建议
新系统 ✅ 使用 firewalld
老项目 / 运维习惯 ✅ 使用 iptables-services
Docker / K8s 节点 ⚠️ 谨慎关闭 firewalld

六、firewalld 快速对照(可选)

firewall-cmd --state
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload

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

  • ✅ CentOS 具体版本(cat /etc/centos-release
  • ✅ 是要 服务器防火墙 还是 Docker / 端口映射

我可以给你一套 最合适的安全规则示例

0