温馨提示×

centos怎样查看iptables状态

小樊
46
2026-08-29 09:57:30
栏目: 智能运维

CentOS 6 及更早版本 中,默认使用 iptables 作为防火墙;而在 CentOS 7/8 中,默认使用 firewalld,但 iptables 仍然存在。下面分别说明如何查看 iptables 状态。


一、CentOS 6(或明确使用 iptables 的情况)

1️⃣ 查看 iptables 服务状态

service iptables status

/etc/init.d/iptables status

输出示例:

Table: filter
Chain INPUT (policy ACCEPT)
...

2️⃣ 查看 iptables 规则

iptables -L -n -v

常用参数说明:

  • -L:列出规则
  • -n:不解析域名(更快)
  • -v:显示详细信息
  • --line-numbers:显示规则编号

示例:

iptables -L INPUT -n -v --line-numbers

二、CentOS 7 / 8(使用 systemd)

1️⃣ 查看 iptables 服务状态

systemctl status iptables

如果未安装 iptables-services,可能会提示:

Unit iptables.service could not be found.

2️⃣ 如果未安装 iptables 服务(常见)

CentOS 7/8 默认使用 firewalld,iptables 只是一个后端。

查看是否由 firewalld 管理

systemctl status firewalld

停止 firewalld 并启用 iptables(可选)

systemctl stop firewalld
systemctl disable firewalld

yum install -y iptables-services
systemctl enable iptables
systemctl start iptables

3️⃣ 直接查看 iptables 规则(最常用 ✅)

无论是否启用 iptables 服务,都可以直接查看:

iptables -L -n -v

或查看 NAT 表:

iptables -t nat -L -n -v

三、查看 iptables 是否运行(通用方法)

查看内核中的 iptables 规则数量

iptables -L | wc -l

如果大于 0,说明存在规则(即使服务未显示“running”)


四、查看 iptables 是否开机启动(CentOS 7+)

systemctl is-enabled iptables

五、总结速查表

目的 命令
查看 iptables 规则 iptables -L -n -v
查看某条链 iptables -L INPUT -n -v
查看服务状态 systemctl status iptables
查看 firewalld 状态 systemctl status firewalld
NAT 表 iptables -t nat -L -n -v

如果你愿意,可以告诉我:

  • ✅ CentOS 具体版本
  • ✅ 是想 排查网络问题 还是 做端口/安全策略

我可以给你更针对性的命令或排查步骤。

0