温馨提示×

Debian防火墙怎么查看状态

小樊
36
2026-07-06 18:18:34
栏目: 智能运维

在 Debian 系统中,查看防火墙状态主要取决于你使用的防火墙管理工具。目前最常见的是 UFW(简单防火墙)和 nftables/iptables。以下是针对每种情况的查看方法:

1. 如果你使用的是 UFW(最常见,特别是服务器)

Debian 默认可能没有安装 UFW,但它是管理防火墙最简便的方式。

  • 查看 UFW 状态:

    sudo ufw status
    
    • 如果返回 Status: inactive,表示防火墙已关闭。
    • 如果返回 Status: active,表示防火墙正在运行,并会列出允许/拒绝的规则。
  • 查看详细状态(显示规则对应的端口号):

    sudo ufw status verbose
    
  • 查看已加载的规则(原始格式):

    sudo ufw status numbered
    

2. 如果你使用的是 nftables(Debian 10 Buster 及更新版本的默认防火墙后端)

现代 Debian 系统底层通常使用 nftables。

  • 查看 nftables 规则(当前生效的配置):

    sudo nft list ruleset
    

    如果输出为空,通常表示没有配置防火墙规则(或者处于全通状态)。

  • 检查 nftables 服务状态:

    sudo systemctl status nftables
    

    这会告诉你 nftables 服务是否正在运行,以及启动时是否加载了配置。

3. 如果你使用的是 iptables(传统的防火墙工具)

虽然较新的系统使用 nftables,但 iptables 命令仍然可用(通常作为 nftables 的兼容层)。

  • 查看 IPv4 防火墙规则:

    sudo iptables -L -v -n
    
    • -L: 列出规则。
    • -v: 显示详细信息(流量计数等)。
    • -n: 数字格式显示(不解析域名,速度更快)。
  • 查看 IPv6 防火墙规则:

    sudo ip6tables -L -v -n
    
  • 检查 iptables 服务状态(如果使用 iptables-persistent):

    sudo systemctl status netfilter-persistent
    # 或者
    sudo systemctl status iptables
    

总结:如何判断你用的是哪个?

  1. 先输入 sudo ufw status

    • 如果提示 Status: activeinactive,说明你用的是 UFW
    • 如果提示 sudo: ufw: command not found,说明没装 UFW,继续下一步。
  2. 输入 sudo systemctl status nftables

    • 如果显示 active (exited)loaded,说明你用的是 nftables
    • 如果提示 Unit nftables.service could not be found,继续下一步。
  3. 输入 sudo iptables -L

    • 如果看到很多规则(尤其是 Chain INPUT/DROP 等),说明你在用 iptables

建议: 对于大多数 Debian 桌面用户或新手,推荐使用 UFW,因为它命令简单直观。如果你发现系统没有安装,可以通过以下命令安装:

sudo apt update
sudo apt install ufw

0