温馨提示×

CentOS如何关闭iptables

小樊
41
2026-07-19 07:01:16
栏目: 智能运维

CentOS 中关闭 iptables(防火墙)的方法取决于你使用的 CentOS 版本,因为 CentOS 7 及以后默认使用 firewalld,而不是 iptables 服务。下面分情况说明。


一、CentOS 6 及更早版本(使用 iptables 服务)

1. 停止 iptables 服务

service iptables stop

2. 禁止开机自启动

chkconfig iptables off

3. 查看状态

service iptables status

✅ 如果看到 Firewall is stoppediptables: Firewall is not running,说明已关闭。


二、CentOS 7 / 8 / Stream(使用 firewalld)

注意:CentOS 7+ 默认 没有 iptables 服务,而是 firewalld

1. 停止 firewalld

systemctl stop firewalld

2. 禁止开机自启

systemctl disable firewalld

3. 查看状态

systemctl status firewalld

三、如果你手动启用了 iptables 服务(CentOS 7+)

有些系统会手动安装 iptables-services

1. 停止 iptables

systemctl stop iptables

2. 禁止开机启动

systemctl disable iptables

3. 查看状态

systemctl status iptables

四、临时清空 iptables 规则(不推荐长期使用)

⚠️ 仅用于临时测试,重启后失效:

iptables -F
iptables -X
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT

五、确认是否真的关闭

iptables -L -n

如果看到类似:

Chain INPUT (policy ACCEPT)
Chain FORWARD (policy ACCEPT)
Chain OUTPUT (policy ACCEPT)

说明防火墙规则已清空。


六、生产环境建议 ⚠️

不建议直接关闭防火墙
✅ 建议做法:

  • 按需开放端口
  • 使用 firewall-cmd 管理规则

例如开放 80 端口:

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

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

  • ✅ CentOS 具体版本(cat /etc/centos-release
  • ✅ 是否是服务器 / 云主机 / 本地虚拟机

我可以给你最安全、最合适的操作方案。

0