温馨提示×

CentOS怎么查看iptables

小樊
43
2026-07-19 06:54:21
栏目: 智能运维

CentOS 上查看 iptables 规则,常用方法如下(按系统版本略有不同):


一、CentOS 6 及更早版本(默认使用 iptables)

1️⃣ 查看所有规则

iptables -L

2️⃣ 查看详细规则(含端口、IP、计数器)

iptables -L -n -v

3️⃣ 查看指定表(如 nat、mangle)

iptables -t nat -L -n -v
iptables -t mangle -L -n -v

4️⃣ 查看规则并显示行号(方便删除)

iptables -L -n --line-numbers

二、CentOS 7 / 8(默认使用 firewalld)

⚠️ CentOS 7/8 默认不是直接管理 iptables,而是 firewalld

1️⃣ 查看 firewalld 状态

systemctl status firewalld

2️⃣ 查看 firewalld 规则(推荐)

firewall-cmd --list-all
firewall-cmd --list-all-zones

3️⃣ 如果仍想查看 iptables(firewalld 底层仍用 iptables)

iptables -L -n -v

如果提示 iptables: command not found,需要先安装:

yum install -y iptables-services

三、查看 iptables 配置文件(持久化规则)

cat /etc/sysconfig/iptables

仅在使用 iptables-services 时有效


四、临时关闭 firewalld 使用 iptables(不推荐生产环境)

systemctl stop firewalld
systemctl disable firewalld
systemctl enable iptables
systemctl start iptables

五、常见排查命令

查看当前生效规则

iptables -S

查看 nat 表

iptables -t nat -S

总结

场景 命令
查看规则 iptables -L -n -v
查看行号 iptables -L --line-numbers
CentOS 7/8 优先用 firewall-cmd
查看底层 iptables iptables -S

如果你愿意,可以告诉我 CentOS 版本你是想排查端口/转发/防火墙问题,我可以给你更精确的命令。

0