centos 关闭防火墙命令要区分版本,centos7 以及更新版本默认防火墙是 firewalld,使用 systemctl 体系管理,centos6 默认是 iptables 服务,两套命令不能混用,很多运维新手踩坑就是拿 centos6 的 iptables 命令去操作 centos7,执行之后提示服务不存在,因为 centos7 已经默认替换成 firewalld,关闭防火墙分为临时关闭和永久关闭两种模式,临时关闭只是本次开机生效,服务器重启之后防火墙会自动恢复启动,永久关闭是设置开机禁用,重启之后防火墙依然不会自启,生产环境直接完全关闭防火墙会暴露端口带来安全风险,推荐优先使用放行指定端口的方式,而不是直接全部关闭,下面按照版本分别给出完整命令,同时配套查看状态 重新开启 端口放行相关命令,补充常见报错排查要点。
一 CentOS7 CentOS8 CentOS Stream firewalld 操作
清单
1 临时关闭防火墙
systemctl stop firewalld
2 永久关闭防火墙,禁止开机自启
systemctl disable firewalld
3 查看防火墙当前运行状态
systemctl status firewalld
4 重新开启防火墙
systemctl start firewalld
5 恢复开机自启
systemctl enable firewalld
firewalld 补充命令
清单
1 查看 firewalld 详细状态
firewall-cmd --state
2 查看已经放行的端口
firewall-cmd --list-ports
3 放行指定端口示例,比如 80 端口
firewall-cmd --add-port=80/tcp --permanent
4 重载配置让放行规则生效
firewall-cmd --reload
二 CentOS6 iptables 防火墙操作
清单
1 临时关闭 iptables 防火墙
service iptables stop
2 永久关闭,取消开机自启
chkconfig iptables off
3 查看 iptables 运行状态
service iptables status
4 重新启动 iptables
service iptables start
5 恢复开机自启
chkconfig iptables on
三 临时关闭和永久关闭对比表格
| 操作类型 | CentOS7 firewalld | CentOS6 iptables | 特点 |
|---|---|---|---|
| 临时关闭 | systemctl stop firewalld | service iptables stop | 重启服务器之后防火墙自动启动 |
| 永久关闭 | systemctl disable firewalld | chkconfig iptables off | 重启之后防火墙不会自动启动 |
四 重要安全说明
清单
1 测试环境可以直接执行 centos 关闭防火墙命令,方便调试端口连通性
2 生产服务器不建议直接执行 centos 关闭防火墙命令,全部端口对外开放会增加入侵风险
3 推荐方案是保留 firewalld 运行,单独放行业务需要端口,不需要彻底关闭防火墙
4 如果服务器还部署了 selinux,关闭防火墙之后 selinux 依然会做访问控制,很多人误以为关闭防火墙就全部放行,这个是常见误区
5 执行 centos 关闭防火墙命令需要 root 权限,普通用户执行会提示权限不足,需要前面加 sudo 或者切换 root 账号
五 常见故障排查
清单
1 执行 centos 关闭防火墙命令提示 Unit firewalld.service not found,说明系统没有安装 firewalld,或者是 centos6 版本,不能使用 systemctl 命令
2 执行命令之后仍然无法访问端口,检查是不是云服务器,云厂商还有额外安全组,安全组独立于系统防火墙,就算系统防火墙关闭,安全组没放行依旧无法连通
3 永久关闭之后重启,防火墙又启动,确认是否同时存在其他防火墙组件,比如 iptables 服务还在运行
【相关 FAQ】
问题 1 centos 关闭防火墙命令 centos7 和 centos6 命令一样吗
回答 1 centos 关闭防火墙命令在 centos7 和 centos6 并不一样,centos 关闭防火墙命令 centos7 使用 systemctl stop firewalld,centos 关闭防火墙命令 centos6 使用 service iptables stop,两套服务名称不同不能混用。
问题 2 centos 关闭防火墙命令临时关闭之后重启会恢复吗
回答 1 centos 关闭防火墙命令里面 stop 属于临时关闭,centos 关闭防火墙命令临时关闭之后服务器重启防火墙会自动启动,如果需要重启也保持关闭,centos 关闭防火墙命令需要额外执行 disable 设置开机禁用。
问题 3 centos 关闭防火墙命令执行完端口还是不通是什么原因
回答 1 centos 关闭防火墙命令执行成功之后端口依旧不通,大概率是云服务器安全组没有放行,centos 关闭防火墙命令只关闭系统内部防火墙,云平台安全组是独立一层访问控制,需要单独配置。
问题 4 centos 关闭防火墙命令需要 root 权限吗
回答 1 centos 关闭防火墙命令属于系统服务管理操作,centos 关闭防火墙命令必须 root 权限,普通账号执行 centos 关闭防火墙命令会报权限错误,可以使用 sudo 执行 centos 关闭防火墙命令。
【免责申明】
本文为 Linux 运维技术操作科普,直接关闭防火墙会降低服务器安全防护等级,请根据业务环境谨慎操作,生产环境请遵守企业安全规范,所有操作带来的安全风险由操作者自行承担。


