Linux关闭防火墙命令:firewalld/ufw速查

互联网资讯

Linux关闭防火墙命令:firewalld/ufw速查

2026-08-29 06:26


Linux关闭firewalld、ufw、iptables、nftables防火墙命令教程

linux关闭防火墙命令

在 Linux 系统中,防火墙主要用于控制入站和出站网络连接,保护服务器免受未经授权的访问。常见的防火墙工具有 firewalldufwiptablesnftables。不同 Linux 发行版默认使用的防火墙工具不同,因此关闭防火墙的命令也会有所区别。下面整理一份常见的 Linux 关闭防火墙命令教程,方便你在不同系统中快速操作。

一、关闭 firewalld 防火墙

firewalld 常见于 CentOS、RHEL、Fedora、Rocky Linux、AlmaLinux 等系统。

1. 临时关闭 firewalld

如果只是临时关闭防火墙,重启系统后会再次启动,可以使用以下命令:

sudo systemctl stop firewalld

查看 firewalld 状态:

sudo systemctl status firewalld

如果显示 inactivedead,说明防火墙已经停止。

2. 永久关闭 firewalld

如果你希望系统重启后防火墙也不会自动启动,可以执行:

sudo systemctl disable firewalld

也可以停止并禁用一起执行:

sudo systemctl stop firewalld
sudo systemctl disable firewalld

3. 检查 firewalld 是否开机自启

systemctl is-enabled firewalld

如果返回 disabled,表示已禁用开机自启。


二、关闭 ufw 防火墙

ufw 常见于 Ubuntu、Debian 等系统,是 iptables 的简化管理工具。

1. 临时或永久关闭 ufw

在 Ubuntu 中,关闭 ufw 通常使用:

sudo ufw disable

该命令会立即停止防火墙规则,并且通常不会在系统重启后自动恢复开启状态。

查看 ufw 状态:

sudo ufw status

如果显示:

Status: inactive

说明防火墙已经关闭。

2. 开启 ufw 的方法

如果后续需要重新开启,可以使用:

sudo ufw enable

三、关闭 iptables 防火墙

iptables 是较经典的 Linux 防火墙工具,很多旧系统中仍然常见。

1. 清空当前 iptables 规则

如果你只是想让当前防火墙规则不再生效,可以清空规则链:

sudo iptables -F
sudo iptables -X
sudo iptables -t nat -F
sudo iptables -t nat -X
sudo iptables -t mangle -F
sudo iptables -t mangle -X

同时,还需要将默认策略设置为允许:

sudo iptables -P INPUT ACCEPT
sudo iptables -P FORWARD ACCEPT
sudo iptables -P OUTPUT ACCEPT

执行后,当前系统的 iptables 防火墙规则基本相当于关闭。

2. 保存 iptables 配置

在 CentOS 6 或部分旧系统中,可以使用:

service iptables save

或者:

/etc/init.d/iptables save

如果系统使用 iptables-services,可以停止并禁用服务:

sudo systemctl stop iptables
sudo systemctl disable iptables

四、关闭 nftables 防火墙

部分新版 Linux 系统使用 nftables 作为底层防火墙框架。

1. 清空 nftables 规则

可以使用以下命令清空当前规则:

sudo nft flush ruleset

该命令会清除当前加载的 nftables 规则集。

2. 停止 nftables 服务

如果系统通过服务管理 nftables,可以执行:

sudo systemctl stop nftables

禁止开机自启:

sudo systemctl disable nftables

查看状态:

sudo systemctl status nftables

五、如何判断当前 Linux 使用哪种防火墙

如果不确定系统使用哪种防火墙,可以先查看相关服务状态:

systemctl status firewalld
systemctl status ufw
systemctl status iptables
systemctl status nftables

也可以使用以下命令查看正在运行的防火墙服务:

systemctl list-units --type=service | grep -E "firewalld|ufw|iptables|nftables"

如果某个服务显示为 active,通常说明它正在运行。


六、关闭防火墙前的注意事项

关闭 Linux 防火墙会让服务器暴露在网络中,尤其是云服务器、VPS 或公网服务器,存在较高安全风险。因此,在关闭防火墙之前,建议先确认以下几点:

  1. 是否真的需要关闭防火墙
    很多时候,只需要开放指定端口即可,例如开放 Web 服务的 80443 端口,SSH 的 22 端口,不一定需要完全关闭防火墙。

  2. 是否会影响远程连接
    如果正在通过 SSH 远程管理服务器,错误修改防火墙规则可能导致无法连接服务器。

  3. 云服务器是否还有安全组限制
    阿里云、腾讯云、华为云、AWS 等云平台通常还有安全组或网络 ACL,即使关闭系统防火墙,也可能仍然受云平台安全规则控制。

  4. 建议备份防火墙规则
    对于 iptables 或 nftables,可以先导出当前规则,避免误操作后无法恢复。


七、常用关闭防火墙命令汇总

防火墙类型 临时关闭命令 永久关闭命令
firewalld systemctl stop firewalld systemctl disable firewalld
ufw ufw disable ufw disable
iptables 服务 systemctl stop iptables systemctl disable iptables
nftables systemctl stop nftables systemctl disable nftables
iptables 规则清空 iptables -F 需保存为空规则
nftables 规则清空 nft flush ruleset 需配置启动时不加载规则

总结

本文介绍了常见的 linux关闭防火墙命令,包括 firewalld、ufw、iptables 和 nftables 的关闭方法。firewalld 可使用 systemctl stop firewalld 停止,并用 systemctl disable firewalld 禁止开机自启;Ubuntu 或 Debian 中的 ufw 可使用 ufw disable 关闭;iptables 可通过清空规则或停止服务实现关闭;nftables 则可使用 nft flush ruleset 清空规则。需要注意的是,关闭防火墙会降低服务器安全性,生产环境中更推荐只开放必要端口,而不是直接完全关闭防火墙。


标签: Linux 防火墙 关闭命令