如何在CentOS中关闭防火墙 CentOS 7/8停止firewalld服务的方法

2026-05-29电脑数码181651

必须同时执行 systemctl stop firewalld 和 systemctl disable firewalld 才能彻底停用,否则重启后自动恢复;firewall-cmd --state 返回 not running 且 systemctl is-enabled firewalld 输出 disabled 才算真正关闭。

firewalld 在 CentOS 7/8 中是默认启用的防火墙服务,只执行 systemctl stop firewalld 不足以真正“关闭”它——重启后会自动恢复运行,且部分残留规则可能仍生效。


确认当前 firewalld 状态是否真关闭

很多人以为 systemctl stop firewalld 就完事了,但实际常遇到:服务显示 inactive,firewall-cmd --state 却返回 running,或端口仍被拦截。

  • systemctl status firewalld 查看服务状态(关注 Active: 行是否为 inactive (dead)
  • firewall-cmd --state 必须返回 not running,否则说明内核模块或 runtime 规则仍在工作
  • 若返回 running,说明服务虽停止,但 firewalld 进程或 netfilter 链未完全清理——常见于 systemd 未彻底 kill 子进程或 --panic-on 残留

此时需强制清理:sudo firewall-cmd --panic-off(关闭 panic 模式),再重试 stop + disable。


systemctl disable firewalld 是永久关闭的关键动作

CentOS 7/8 使用 systemd 管理服务,disable 不是可选项,而是必须步骤——它删除 /etc/systemd/system/multi-user.target.wants/firewalld.service 软链接,防止 reboot 后自动拉起。

  • stopdisable → 重启即复活
  • disablestop → 当前仍运行,规则生效中
  • 正确顺序:sudo systemctl stop firewalldsudo systemctl disable firewalld
  • 验证:systemctl is-enabled firewalld 应输出 disabled,而非 enabled

注意:systemctl mask firewalld 更激进(禁止所有启停操作),一般不需要;unmask 后才能再次 enable,容易误锁服务。


检查是否有 iptablesip6tables 干扰

CentOS 7/8 默认不启用 iptables 服务,但某些镜像或手动安装过旧工具链的系统可能残留 iptables-services 包,导致 iptables 规则与 firewalld 冲突,甚至掩盖真实状态。

PHP建站宝(Centos 64位_ Apache)

PHP建站宝(Centos 64位_ Apache)

下载

  • 运行 systemctl list-unit-files | grep -i iptables,检查是否存在 iptables.serviceip6tables.serviceiptables-restore.service
  • 若存在,同样执行:sudo systemctl stop iptablessudo systemctl disable iptables
  • 清空规则(谨慎):sudo iptables -F(IPv4)、sudo ip6tables -F(IPv6)——仅在确认无业务依赖时使用

特别注意:iptables -L 显示非空 ≠ iptables 服务在运行,可能是 firewalld 底层调用的临时规则;真正要盯的是 systemctl status iptables 输出是否为 inactive (dead)


关闭后务必验证端口连通性,别信状态输出

服务状态和规则状态是两回事。即使 firewalld 显示 stopped 且 firewall-cmd --state 返回 not running,仍可能因以下原因丢包:

  • 网卡绑定的 zone 未重置(如 eth0 仍属 public zone,且该 zone 有 drop 规则残留)
  • SELinux 的 booleanshttpd_can_network_connect 被禁用,造成“像防火墙拦了”的假象
  • 云平台安全组(AWS/Aliyun)或宿主机 iptables(KVM/LXC)在更外层拦截

最可靠验证方式:nc -zv localhost 22(本地测)+ nc -zv 22(从另一台机器测)。如果本地通、外部不通,问题大概率不在本机 firewalld

真正麻烦的不是关不掉,而是关了之后你以为它没拦,结果某条服务死活连不上——多一层验证,少半夜爬起来查日志。

《如何在CentOS中关闭防火墙 CentOS 7/8停止firewalld服务的方法.doc》

下载本文的Word格式文档,以方便收藏与打印。