Linux防火墙是系统安全的核心组件,及时查询其规则状态、开放的端口及服务,对保障系统安全和排查网络问题至关重要,不同Linux发行版可能采用不同的防火墙工具(如iptables、firewalld、nftables),查询方法也存在差异,本文将详细介绍主流防火墙的查询方式,帮助用户快速掌握规则查看技巧。
iptables防火墙查询(适用于CentOS 6、Debian 7等旧系统)
iptables是Linux传统的防火墙工具,通过规则链控制流量,查询规则的核心命令是iptables -L -n -v
,参数解释如下:
-L
:列出所有规则链(如INPUT、OUTPUT、FORWARD);-n
:以数字形式显示IP和端口(避免DNS解析延迟);-v
:显示详细统计(数据包数、字节数)。
输出表格关键列说明:
| 列名 | 含义 | 示例值 |
|————|————————–|—————–|
| Chain | 规则链 | INPUT |
| Target | 目标动作(ACCEPT/DROP) | ACCEPT |
| Proto | 协议(tcp/udp/icmp) | tcp |
| Source | 源IP地址 | 0.0.0.0/0 |
| Destination| 目标IP地址 | 0.0.0.0/0 |
| Port | 端口号 | 22 |
| pkts | 匹配的数据包数 | 1024 |
| bytes | 匹配的字节数 | 102400 |
若Chain INPUT
下Target
为ACCEPT
、Port
为22
,表示允许SSH流量访问。
firewalld防火墙查询(适用于CentOS 7+、RHEL 7+等新系统)
firewalld是动态防火墙管理工具,支持区域(Zone)和服务(Service)策略,查询命令更灵活:
firewall-cmd --list-all
:显示当前区域的所有配置(接口、服务、端口等);firewall-cmd --list-ports
:仅列出开放的端口(如“8080/tcp”);firewall-cmd --list-services
:显示允许的服务名称(如“http”“ssh”)。
输出表格关键列说明:
| 列名 | 含义 | 示例值 |
|————|————————–|—————–|
| Zone | 安全区域(如public/内部)| public |
| Interface | 绑定的网络接口 | eth0 |
| Services | 允许的服务列表 | ssh, https |
| Ports | 开放的端口列表 | 8080/tcp |
| Sources | 信任的源IP段 | 192.168.1.0/24 |
public
区域下Services
包含https
,表示允许HTTPS流量通过。
nftables防火墙查询(适用于Ubuntu 20.04+、CentOS 8+等新系统)
nftables是iptables的替代方案,语法更简洁,查询命令为nft list ruleset
,输出以“表(table)-链(chain)-规则(rule)”结构呈现。
table inet filter {
chain input {
type filter hook input priority 0;
policy accept;
tcp dport 22 accept; # 允许SSH
}
}
其中tcp dport 22 accept
明确表示允许22端口流量。
通用查询步骤
- 确认防火墙状态:
- firewalld:
systemctl status firewalld
(查看是否运行); - iptables:
service iptables status
(查看是否启动)。
- firewalld:
- 查看日志:
- firewalld日志:
journalctl -u firewalld
; - iptables日志:
grep iptables /var/log/messages
(需开启日志记录)。
- firewalld日志:
相关问答FAQs
问题1:如何判断当前系统使用的是哪种防火墙?
解答:可通过以下方式判断:
- 检查服务状态:
systemctl status firewalld
,若显示“active (running)”,则使用firewalld;service iptables status
,若显示“running”,则使用iptables。 - 查看命令是否存在:
which firewalld
存在则优先使用firewalld,which iptables
存在则可能使用iptables。 - 查看配置文件路径:firewalld配置在
/etc/firewalld/
,iptables规则在/etc/sysconfig/iptables
(CentOS 6)或/etc/iptables/rules.v4
(Ubuntu)。
问题2:查询到防火墙规则后,如何临时开放某个端口(如8080端口)?
解答:根据防火墙类型操作:
- firewalld:
- 临时开放(重启后失效):
firewall-cmd --add-port=8080/tcp
; - 永久开放:
firewall-cmd --add-port=8080/tcp --permanent
,然后重载防火墙firewall-cmd --reload
。
- 临时开放(重启后失效):
- iptables:
- 临时开放:
iptables -A INPUT -p tcp --dport 8080 -j ACCEPT
; - 永久保存规则(CentOS 6):
service iptables save
; - Ubuntu系统:
iptables-save > /etc/iptables/rules.v4
。
- 临时开放:
原创文章,发布者:酷番叔,转转请注明出处:https://cloud.kd.cn/ask/38508.html