小易说IT 小易说IT

Linux 如何关闭防火墙

⚠️ 重要提醒:生产环境不建议直接关闭防火墙,优先放行端口,仅内网测试、排错临时关闭!云服务器还要额外看控制台【安全组】

一、CentOS7+/Rocky/Alibaba Linux(firewalld)

# 临时关闭(重启后恢复)
sudo systemctl stop firewalld

# 永久关闭(停止+取消开机自启)
sudo systemctl stop firewalld
sudo systemctl disable firewalld

# 查看状态
firewall-cmd --state
# 或者
systemctl status firewalld

恢复开启:

sudo systemctl enable --now firewalld

二、Ubuntu / Debian(ufw)

# 永久关闭ufw(一条命令即可)
sudo ufw disable

# 查看状态
sudo ufw status

恢复开启:

sudo ufw enable

三、老版本 CentOS6(iptables)

# 临时关闭
service iptables stop
# 永久关闭开机自启
chkconfig iptables off

四、直接操作 iptables(通用)

清空规则(临时,重启失效)

iptables -F
iptables -X
iptables -Z

五、配套:关闭 SELinux(集群常用,和防火墙不是一回事)

# 临时关闭
setenforce 0

# 永久关闭:修改 /etc/selinux/config,SELINUX=disabled
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

✅ 最佳实践(不关闭防火墙,放行端口)

firewalld 放行端口示例:

# 放行80端口,永久生效
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload

ufw 放行 ssh:

ufw allow 22/tcp