温馨提示×

centos如何关闭iptables服务

小樊
36
2026-08-29 10:03:30
栏目: 智能运维

CentOS 上关闭 iptables 服务的方法取决于 CentOS 版本 以及你是否在使用 iptables 还是 firewalld。下面分别说明。


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

1. 停止 iptables 服务

service iptables stop

2. 禁止开机自启

chkconfig iptables off

3. 查看状态

service iptables status

二、CentOS 7 / 8(默认使用 firewalld)

注意:CentOS 7 开始默认使用 firewalld,而不是 iptables 服务。
如果你没有手动安装 iptables-services,那么 iptables 服务可能根本不存在。

情况 1:你使用的是 firewalld(最常见)

停止 firewalld

systemctl stop firewalld

禁止开机启动

systemctl disable firewalld

查看状态

systemctl status firewalld

情况 2:你手动安装了 iptables 服务

如果你确认安装了 iptables-services

systemctl stop iptables
systemctl disable iptables

查看状态:

systemctl status iptables

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

如果你只是想临时放行所有流量(不关闭服务):

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

⚠️ 重启后会失效


四、如何确认当前使用的是哪种防火墙?

systemctl status iptables
systemctl status firewalld

哪个在运行,就是哪个在生效。


五、建议(非常重要)

  • 生产服务器不建议直接关闭防火墙
  • 推荐做法:
    ✅ 开放需要的端口(如 22、80、443),而不是关闭防火墙

例如:

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

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

  • ✅ CentOS 具体版本(cat /etc/centos-release
  • ✅ 是本地虚拟机、云服务器还是物理机

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

0