CentOS怎么关闭系统防火墙
如果只是临时停掉 firewalld,可以执行 sudo systemctl stop firewalld,这会立刻生效,不过系统一旦重启,它通常还会自己恢复;要想彻底禁用,还得继续执行 sudo systemctl disable firewalld,并确认 systemctl is-enable
如果只是临时停掉 firewalld,可以执行 sudo systemctl stop firewalld,这会立刻生效,不过系统一旦重启,它通常还会自己恢复;要想彻底禁用,还得继续执行 sudo systemctl disable firewalld,并确认 systemctl is-enabled firewalld 的返回结果是 disabled;至于是否真的关干净了,还需要再核对一遍:firewall-cmd --state 应该报错,同时 iptables -L -n 里也不应再有残留的拦截规则。

CentOS 7 及以上版本默认用 firewalld,关防火墙不是“停一个服务”就完事——得区分临时关闭、永久禁用、以及是否清空规则,否则重启后又自动开了,或者服务停了但残留规则还在干扰网络通信。
怎么临时停止 firewalld(重启后恢复)
适用于调试、部署测试、快速验证端口连通性等短时场景。执行后立即生效,但系统重启就会自动拉起服务:
sudo systemctl stop firewalld—— 停止当前运行的 firewalld 进程sudo systemctl status firewalld—— 查看输出中是否含inactive (dead)- 注意:
firewall-cmd --state会报错或返回not running,这是正常现象
怎么永久禁用 firewalld(避免开机自启)
只停服务不等于“永久关闭”,必须同时禁用开机启动,否则 reboot 后它又活了:
sudo systemctl disable firewalld—— 移除开机启动链接,但不删配置文件sudo systemctl is-enabled firewalld—— 应返回disabled,不是enabled或static- 常见错误:漏掉这步,只执行
stop,结果第二天发现服务又 running 了 - 集群环境需逐台确认,不能只在一台机器上操作
怎么确认 firewalld 真的关干净了
光看 systemctl status 不够,firewalld 停了,但内核 netfilter 规则可能还挂着旧策略,尤其之前开过端口或设过 zone:
sudo firewall-cmd --list-all—— 如果报错FirewallD is not running,说明服务已停;若返回规则列表,说明没真正停掉sudo iptables -L -n | head -10—— 检查底层 iptables 链是否还有REJECT或DROP规则(firewalld 停后,这些规则通常会被清空,但某些异常状态可能残留)- 更稳妥做法:停 + 禁用后,再执行一次
sudo systemctl daemon-reload,防止 unit 文件缓存干扰
CentOS 6 怎么关(别用错命令)
如果你误把 CentOS 7 的命令套到 CentOS 6 上,systemctl 会报错——因为 CentOS 6 用的是 iptables 服务,管理方式完全不同:
sudo service iptables stop—— 临时停止sudo chkconfig iptables off—— 永久禁用开机启动sudo iptables -F—— 清空当前所有规则(仅内存,不改配置文件)- 配置文件在
/etc/sysconfig/iptables,清空前建议先备份:sudo cp /etc/sysconfig/iptables /etc/sysconfig/iptables.bak
最常被忽略的一点:有些脚本或容器平台(比如 Docker)会自己往 iptables 里加规则,即使 firewalld 停了,它们仍可能拦截流量。关防火墙后连不通,别急着骂系统,先 iptables -L -n 看一眼底层链有没有“幽灵规则”。


































