在Linux系统中,防火墙是保障服务器安全的重要组件,用于控制进出网络的流量,防止未授权访问,查看防火墙状态是日常运维中的常见操作,不同Linux发行版可能使用不同的防火墙管理工具(如iptables、firewalld、ufw等),本文将详细介绍如何通过这些工具查看防火墙状态,包括核心命令、参数解析及实际输出示例。
Linux防火墙工具概述
Linux系统的防火墙实现主要分为两类:基于内核 netfilter 框架的 iptables(传统工具)和 nftables(新一代工具),以及用户空间的管理工具如 firewalld(动态管理,CentOS 7+ 默认)和 ufw(Uncomplicated Firewall,Ubuntu/Debian 简化工具),查看防火墙状态需根据系统默认工具选择对应命令。
通过iptables查看防火墙状态
iptables 是 Linux 2.4 及以上版本内置的防火墙工具,通过表(table)和链(chain)管理规则,默认使用 filter
表(过滤数据包),查看状态的核心命令如下:
基础命令:列出所有规则
iptables -L -n -v
-L
:列出指定表的所有规则(默认filter
表,包含INPUT
、OUTPUT
、FORWARD
三条链)。-n
:以数字形式显示IP地址和端口(避免DNS解析,加快速度)。-v
:显示详细信息,如规则匹配的包数量、字节大小、网络接口等。
输出示例:
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
1500 120K ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
3 240 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
policy ACCEPT
:链的默认策略(ACCEPT/DROP/REJECT)。pkts bytes
:规则匹配的数据包数量和总字节数。target
:规则动作(ACCEPT接受、DROP丢弃、REJECT拒绝并返回错误)。
查看指定链的规则
iptables -L INPUT --line-numbers
--line-numbers
:为规则添加行号,方便后续修改(如删除规则时通过行号定位)。
查看防火墙运行状态(需确保iptables服务已启动)
systemctl status iptables # 或 service iptables status
若输出显示 Active: active (running)
,则防火墙正在运行;若 Active: inactive (dead)
,则未启动。
通过firewalld查看防火墙状态
firewalld 是 CentOS 7+、RHEL 7+、Fedora 等发行版的默认防火墙管理工具,支持动态规则更新(无需重启服务),查看状态的核心命令如下:
查看防火墙运行状态
firewall-cmd --state
- 输出
running
表示防火墙运行中,not running
表示未运行。
列出所有规则(含区域、服务、端口等)
firewall-cmd --list-all
输出示例:
public (active)
target: default
icmp-block-inversion: no
interfaces: eth0
sources:
services: ssh dhcpv6-client
ports: 8080/tcp
protocols:
forward: no
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
public (active)
:当前激活的区域(默认为public
,可自定义)。services
:允许通过的服务的列表(如ssh
对应22端口)。ports
:手动开放的端口(如8080/tcp
)。
查看激活的区域及规则
# 查看当前激活的区域 firewall-cmd --get-active-zones # 查看指定区域的规则(如public区域) firewall-cmd --zone=public --list-services
检查防火墙服务状态
systemctl status firewalld
若 Active: active (running)
,则服务正常运行。
通过ufw查看防火墙状态
ufw(Uncomplicated Firewall)是 Ubuntu/Debian 系列的简化防火墙工具,通过 ufw
命令管理规则,查看状态的核心命令如下:
查看防火墙状态(是否启用)
ufw status
- 输出
Status: active
表示已启用,Status: inactive
表示未启用。
查看详细规则(含方向、协议、端口等)
ufw status verbose
输出示例:
Status: active
To Action From
-- ------ ----
22/tcp ALLOW Anywhere
80/tcp ALLOW Anywhere
22/tcp (v6) ALLOW Anywhere (v6)
80/tcp (v6) ALLOW Anywhere (v6)
To
:目标端口/协议(如22/tcp
)。Action
:动作(ALLOW/DENY)。From
:来源地址(Anywhere
表示所有IP,Anywhere (v6)
表示IPv6)。
查看带行号的规则(方便管理)
ufw status numbered
输出中每条规则前会添加行号,
Status: active
To Action From
-- ------ ----
[ 1] 22/tcp ALLOW IN Anywhere
[ 2] 80/tcp ALLOW IN Anywhere
[ 3] 22/tcp (v6) ALLOW IN Anywhere (v6)
[ 4] 80/tcp (v6) ALLOW IN Anywhere (v6)
检查ufw服务状态
systemctl status ufw
Active: active (exited)
表示服务已启用并正常运行(exited
是正常状态,因ufw无持续守护进程)。
通过nftables查看防火墙状态
nftables 是 iptables 的替代品,CentOS 8+、Ubuntu 20.04+ 等较新版本已默认使用,其规则通过 nft
命令管理,查看状态的核心命令如下:
列出所有规则集
nft list ruleset
输出示例:
table inet filter {
chain input {
type filter hook input priority filter; policy accept;
iifname lo accept
tcp dport 22 drop
}
chain forward {
type filter hook forward priority filter; policy accept;
}
}
table inet filter
:定义inet
类型的filter
表(同时支持IPv4/IPv6)。chain input
:输入链,type filter hook input priority filter
表示钩子到input
链,优先级为filter
。policy accept
:链的默认策略。
查看指定表或链的规则
# 查看filter表的所有链 nft list table inet filter # 查看input链的规则 nft list chain inet filter input
检查nftables服务状态
systemctl status nftables
Active: active (running)
表示服务正常运行。
防火墙状态查看工具对比
为方便快速选择工具,以下表格总结不同发行版及对应的核心查看命令:
工具名称 | 适用发行版 | 核心查看命令 | 输出关键字段 |
---|---|---|---|
iptables | CentOS 7-、RHEL 7-、Ubuntu 14.04 | iptables -L -n -v |
链策略、规则匹配数、目标动作 |
firewalld | CentOS 7+、RHEL 7+、Fedora | firewall-cmd --list-all |
激活区域、允许服务、开放端口 |
ufw | Ubuntu、Debian | ufw status verbose |
规则方向、目标端口、来源地址 |
nftables | CentOS 8+、Ubuntu 20.04+ | nft list ruleset |
表/链定义、钩子类型、默认策略 |
常见问题排查
若查看防火墙状态时发现异常(如规则未生效、服务未启动),可按以下步骤排查:
- 确认防火墙服务是否运行:通过
systemctl status <防火墙服务>
检查(如firewalld
、iptables
)。 - 检查规则语法:使用
iptables -L -n
或firewall-cmd --check
验证规则是否正确。 - 确认规则是否加载到内核:iptables 规则需通过
service iptables save
保存(CentOS 7-),firewalld 规则需通过firewall-cmd --reload
重新加载。
相关问答FAQs
Q1:为什么执行 iptables -L
显示的规则为空,但防火墙状态显示为运行?
A:这可能是由于以下原因:
- 防火墙的默认策略为
ACCEPT
(允许所有流量),且未添加任何过滤规则,因此规则列表为空,可通过iptables -L -n -v
查看链的默认策略(policy ACCEPT
)。 - 规则未正确保存:在 CentOS 7- 等系统中,
iptables
规则需手动保存(service iptables save
),否则重启后会丢失。 - 使用了其他防火墙工具:如系统默认使用
firewalld
,其规则会覆盖iptables
,需通过firewall-cmd --list-all
查看规则。
Q2:如何判断防火墙是否阻止了某个端口的访问?
A:可通过以下步骤排查:
- 测试端口连通性:使用
telnet
或nc
(netcat)测试目标端口,若连接失败,可能是防火墙拦截。telnet <IP地址> <端口号> # telnet 192.168.1.100 22 nc -zv <IP地址> <端口号> # nc -zv 192.168.1.100 80
- 查看防火墙规则:根据使用的防火墙工具,检查对应端口是否被允许(如
iptables -L INPUT -n | grep 22
查看22端口规则)。 - 检查防火墙日志:若规则为
DROP
或REJECT
,可通过日志确认拦截记录。- iptables 日志:
grep 'DENY' /var/log/iptables.log
(需提前配置日志)。 - firewalld 日志:
journalctl -u firewalld | grep 'BLOCK'
。 - ufw 日志:
ufw status verbose
查看规则,或tail -f /var/log/ufw.log
查看实时日志。
- iptables 日志:
原创文章,发布者:酷番叔,转转请注明出处:https://cloud.kd.cn/ask/20789.html