如何在 Linux 上列出所有带行号的 iptables 规则
是的,您可以使用Linux 上的以下命令轻松列出所有 iptables 规则
: (1) iptables 命令– IPv4 netfilter 管理工具,用于显示 iptables 防火墙规则。
(2) ip6tables 命令 – IPv6 netfilter 管理工具,用于显示规则。
教程详细信息 | |
---|---|
难度等级 | 简单的 |
Root 权限 | 是的 |
要求 | Linux 终端 |
类别 | 防火墙 |
先决条件 | iptables 或 ip6tables 命令 |
操作系统兼容性 | Alma • Alpine • Arch • Debian • Fedora • Mint • openSUSE • Pop!_OS • RHEL • Rocky • Stream • SUSE • Ubuntu |
预计阅读时间 | 6 分钟 |
如何列出 Linux 上的所有 iptables 规则
在Linux上列出所有规则的步骤如下:
- 打开终端应用程序或使用 ssh 命令登录:
$ ssh user@server-name
- 列出所有 IPv4 规则:
$ sudo iptables -S
- 获取所有 IPv6 规则的列表:
$ sudo ip6tables -S
- 列出所有表规则:
$ sudo iptables -L -v -n | more
- 仅列出 INPUT 表的所有规则:
$ sudo iptables -L INPUT -v -n
$ sudo iptables -S INPUT
让我们详细了解所有语法和用法,以显示和列出 Linux 操作系统上的所有 iptables 规则。
查看 Linux 中所有 iptables 规则
语法是:
iptables -S iptables --list iptables -L iptables -S TABLE_NAME iptables --table NameHere --list iptables -t NameHere -L -n -v --line-numbers
打印选定链中的所有规则
对于 IPv4 规则,命令语法如下:
对于 IPv6 规则:
$ sudo iptables -S
$ sudo iptables -S INPUT
$ iptables -S OUTPUT
$ sudo ip6tables -S
$ sudo ip6tables -S INPUT
$ ip6tables -S OUTPUT
如何列出给定表的规则
以 root 用户身份输入以下命令:IPv4 的示例输出:
# iptables -L INPUT
# iptables -L FORWARD
# iptables -L OUTPUT
# iptables -L
# Listing IPv6 rules #
# ip6tables -L INPUT
# ip6tables -L FORWARD
# ip6tables -L OUTPUT
# ip6tables -L
target prot opt source destination ACCEPT tcp -- anywhere anywhere tcp dpt:domain ACCEPT udp -- anywhere anywhere udp dpt:domain ACCEPT tcp -- anywhere anywhere tcp dpt:bootps ACCEPT udp -- anywhere anywhere udp dpt:bootps ufw-before-logging-input all -- anywhere anywhere ufw-before-input all -- anywhere anywhere ufw-after-input all -- anywhere anywhere ufw-after-logging-input all -- anywhere anywhere ufw-reject-input all -- anywhere anywhere ufw-track-input all -- anywhere anywhere Chain FORWARD (policy DROP) target prot opt source destination ACCEPT all -- anywhere anywhere ACCEPT all -- anywhere anywhere ufw-before-logging-forward all -- anywhere anywhere ufw-before-forward all -- anywhere anywhere ufw-after-forward all -- anywhere anywhere ufw-after-logging-forward all -- anywhere anywhere ufw-reject-forward all -- anywhere anywhere ufw-track-forward all -- anywhere anywhere Chain OUTPUT (policy ACCEPT) target prot opt source destination ufw-before-logging-output all -- anywhere anywhere ufw-before-output all -- anywhere anywhere ufw-after-output all -- anywhere anywhere ufw-after-logging-output all -- anywhere anywhere ufw-reject-output all -- anywhere anywhere ufw-track-output all -- anywhere anywhere ..... .. .. Chain ufw-user-limit (0 references) target prot opt source destination LOG all -- anywhere anywhere limit: avg 3/min burst 5 LOG level warning prefix "[UFW LIMIT BLOCK] " REJECT all -- anywhere anywhere reject-with icmp-port-unreachable Chain ufw-user-limit-accept (0 references) target prot opt source destination ACCEPT all -- anywhere anywhere Chain ufw-user-logging-forward (0 references) target prot opt source destination Chain ufw-user-logging-input (0 references) target prot opt source destination Chain ufw-user-logging-output (0 references) target prot opt source destination Chain ufw-user-output (1 references) target prot opt source destination
让我们尝试理解规则输出:
- 目标– 告诉当数据包与规则匹配时该做什么。通常,您会接受、拒绝或丢弃数据包。您也可以跳转到另一个链。
- prot——规则的协议。
- opt—规则的附加选项。
- 源– 源 IP 地址/子网/域名。
- 目标– 目标 IP 地址/子网/域名。
如何查看nat规则:
filter默认情况下使用该表。要查看 NAT 规则,请输入:
# iptables -t nat -L
其他表选项:
# iptables -t filter -L
# iptables -t raw -L
# iptables -t security -L
# iptables -t mangle -L
# iptables -t nat -L
# ip6tables -t filter -L
如何查看带有行号的 nat 规则:
通过--line-numbers选项:IPv4 的示例输出:
# iptables -t nat -L --line-numbers -n
# for IPv4 rules #
# ip6tables -t nat -L --line-numbers -n
Chain PREROUTING (policy ACCEPT 28M packets, 1661M bytes) num pkts bytes target prot opt in out source destination 1 0 0 DNAT tcp -- eth0 * 10.10.29.68 0.0.0.0/0 tcp dpt:3306 to:10.0.3.19:3306 2 0 0 DNAT tcp -- eth0 * 10.10.29.68 0.0.0.0/0 tcp dpt:11211 to:10.0.3.20:11211 3 0 0 DNAT udp -- eth0 * 10.10.29.68 0.0.0.0/0 udp dpt:11211 to:10.0.3.20:11211 Chain INPUT (policy ACCEPT 18M packets, 1030M bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 23M packets, 1408M bytes) num pkts bytes target prot opt in out source destination Chain POSTROUTING (policy ACCEPT 33M packets, 1979M bytes) num pkts bytes target prot opt in out source destination 1 38927 2336K MASQUERADE all -- * * 10.0.3.0/24 !10.0.3.0/24 2 0 0 MASQUERADE all -- * * 10.0.3.0/24 !10.0.3.0/24
如何使用计数器(字节和数据包)查看 nat 规则
将选项传递-v给 iptables 命令来查看 Linux 上的所有 iptables 规则:
# iptables -t nat -L -n -v
图 01:Linux 查看所有 iptables NAT、DNAT、MASQUERADE 规则
向 ip6tables 问好
ip6tables 是 IPv6 数据包过滤和 NAT 的管理工具。要查看 IPv6 表,请输入:
# ip6tables -L -n -v
Chain INPUT (policy DROP 239 packets, 16202 bytes) pkts bytes target prot opt in out source destination 136K 30M ufw6-before-logging-input all * * ::/0 ::/0 136K 30M ufw6-before-input all * * ::/0 ::/0 241 16360 ufw6-after-input all * * ::/0 ::/0 239 16202 ufw6-after-logging-input all * * ::/0 ::/0 239 16202 ufw6-reject-input all * * ::/0 ::/0 239 16202 ufw6-track-input all * * ::/0 ::/0 Chain FORWARD (policy DROP 483 packets, 32628 bytes) pkts bytes target prot opt in out source destination 483 32628 ufw6-before-logging-forward all * * ::/0 ::/0 483 32628 ufw6-before-forward all * * ::/0 ::/0 483 32628 ufw6-after-forward all * * ::/0 ::/0 483 32628 ufw6-after-logging-forward all * * ::/0 ::/0 483 32628 ufw6-reject-forward all * * ::/0 ::/0 483 32628 ufw6-track-forward all * * ::/0 ::/0 Chain OUTPUT (policy ACCEPT 122 packets, 8555 bytes) pkts bytes target prot opt in out source destination 136K 30M ufw6-before-logging-output all * * ::/0 ::/0 136K 30M ufw6-before-output all * * ::/0 ::/0 183 14107 ufw6-after-output all * * ::/0 ::/0 183 14107 ufw6-after-logging-output all * * ::/0 ::/0 183 14107 ufw6-reject-output all * * ::/0 ::/0 183 14107 ufw6-track-output all * * ::/0 ::/0 Chain ufw6-after-forward (1 references) pkts bytes target prot opt in out source destination ... .... .. pkts bytes target prot opt in out source destination 19 1520 ACCEPT tcp * * ::/0 ::/0 ctstate NEW 42 4032 ACCEPT udp * * ::/0 ::/0 ctstate NEW Chain ufw6-user-forward (1 references) pkts bytes target prot opt in out source destination Chain ufw6-user-input (1 references) pkts bytes target prot opt in out source destination Chain ufw6-user-limit (0 references) pkts bytes target prot opt in out source destination 0 0 LOG all * * ::/0 ::/0 limit: avg 3/min burst 5 LOG flags 0 level 4 prefix "[UFW LIMIT BLOCK] " 0 0 REJECT all * * ::/0 ::/0 reject-with icmp6-port-unreachable Chain ufw6-user-limit-accept (0 references) pkts bytes target prot opt in out source destination 0 0 ACCEPT all * * ::/0 ::/0 Chain ufw6-user-logging-forward (0 references) pkts bytes target prot opt in out source destination Chain ufw6-user-logging-input (0 references) pkts bytes target prot opt in out source destination Chain ufw6-user-logging-output (0 references) pkts bytes target prot opt in out source destination Chain ufw6-user-output (1 references) pkts bytes target prot opt in out source destination
要查看 nat 规则和行号,请输入:
# iptables -t nat -L --line-numbers -nip6tables -L -n -v -t nat --line-numbers
相关另外,请查看我们针对Alpine Linux Awall、CentOS 8、OpenSUSE、RHEL 8、Debian 12/11、Ubuntu Linux 版本16.04 LTS / 18.04 LTS / 20.04 LTS和22.04 LTS 的
所有完整防火墙教程。
结论
您学习了如何使用 CLI 显示、过滤和列出 Linux 系统上的所有 iptables 规则。通过键入以下 man 命令或 help 命令查看 iptables 手册页:
$ man iptables
$ man ip6tables