# Iptables

**iptables** 是用于在 2.4.x 及更高版本内核上配置 [Linux 内核](https://www.edgepedia.org/zh/linux-kernel)包过滤规则集的用户空间命令行程序；由于网络地址转换（Network Address Translation）也通过同一规则集配置，它同样处理 NAT，且软件包中包含用于 IPv6 包过滤的 **ip6tables**。<sup>[1](https://iptables.org/projects/iptables/index.html)</sup> 它面向[系统管理员](https://www.edgepedia.org/zh/system-administrator)，与 netfilter——Linux 内核内部的包过滤框架——协同工作。<sup>[1](https://iptables.org/projects/iptables/index.html)</sup><sup> • </sup><sup>[2](https://netfilter.org/documentation/HOWTO/packet-filtering-HOWTO-3.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 是什么 | 用于在 Linux 内核中建立、维护和检查 IPv4/IPv6 包过滤与 NAT 规则的用户空间工具；需要内核的 `ip_tables` 功能<sup>[1](https://iptables.org/projects/iptables/index.html)</sup><sup> • </sup><sup>[3](https://man7.org/linux/man-pages/man8/iptables.8.html)</sup> |
| 起源 | 由保罗·“Rusty”·拉塞尔（Paul "Rusty" Russell）创立的 netfilter 项目编写，用以替代 ipfwadm（Linux 2.0）和 ipchains（Linux 2.2）；随 2001 年 1 月 4 日的 Linux 2.4.0 发布<sup>[4](https://iptables.org/about.html)</sup><sup> • </sup><sup>[5](https://rip.so/ipchains.html)</sup> |
| 表 | 五张独立的表：filter（默认）、nat、mangle、raw 和 security，每张表都带有 INPUT、FORWARD、OUTPUT 等内建链<sup>[3](https://man7.org/linux/man-pages/man8/iptables.8.html)</sup> |
| 规则模型 | 每条规则匹配一个数据包并指定一个目标：ACCEPT、DROP、RETURN、用户自定义链，或 SNAT、MASQUERADE、REJECT 等扩展目标<sup>[3](https://man7.org/linux/man-pages/man8/iptables.8.html)</sup> |
| 状态化过滤 | 连接跟踪（conntrack）将数据包分类为 NEW、ESTABLISHED 及相关状态，使规则可以放行已允许连接的回复流量<sup>[6](https://www.iptables.org/documentation/HOWTO/netfilter-hacking-HOWTO-4.html)</sup> |
| 当前状态 | 上游处于维护模式（1.8.13 于 2026 年 3 月 4 日发布）；在 Debian 10+ 和 Ubuntu 20.10+ 上，`iptables` 命令默认是 `iptables-nft`，它配置的是内核的 nf_tables 子系统<sup>[7](https://www.iptables.org/projects/iptables/downloads.html)</sup><sup> • </sup><sup>[8](https://serverside.com/blog/iptables-vs-nftables)</sup> |
| 弃用 | `ipset` 和 `iptables-nft` 软件包（包括 `iptables`、`ip6tables`、`arptables` 和 `ebtables` 工具）在 RHEL 9 中已弃用<sup>[9](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/10/html/configuring_firewalls_and_packet_filters/getting-started-with-nftables)</sup> |

## 历史

Linux 自 1.1 内核系列起就有了包过滤功能。第一代基于 BSD 的 ipfw，由艾伦·考克斯（Alan Cox）于 1994 年末移植，乔斯·沃斯（Jos Vos）等人为 Linux 2.0 做了增强，用户空间工具为 ipfwadm。1998 年中，拉塞尔为 Linux 2.2 重写了内核代码并引入了 ipchains。第四代工具 iptables 伴随着 1999 年中为 Linux 2.4 进行的又一次内核重写而问世。<sup>[2](https://netfilter.org/documentation/HOWTO/packet-filtering-HOWTO-3.html)</sup>

netfilter 项目由保罗·“Rusty”·拉塞尔创立，旨在重新设计并改进 ipfwadm 和 ipchains 系统。早期，马克·布歇（Marc Boucher）的贡献促成了一个核心团队的建立，拉塞尔和布歇构思了多表框架，将 iptables 通用化，并产生了 `iptable_{filter,nat,mangle}` 模块。随着团队壮大，詹姆斯·莫里斯（James Morris）和哈拉尔德·韦尔特（Harald Welte）加入，相关工作在 2.4 发布之前合并进了官方 Linux 代码树。<sup>[4](https://iptables.org/about.html)</sup> [Netfilter](https://www.edgepedia.org/zh/netfilter) 和 iptables 于 1999 年 8 月 26 日进入主线内核，并随 2001 年 1 月 4 日的 Linux 2.4.0 交付给所有用户。<sup>[5](https://rip.so/ipchains.html)</sup>

与 ipchains 相比，iptables 改变了遍历模型：INPUT 和 OUTPUT 链只对发往本地主机或由本地主机发出的数据包进行遍历，因此被转发的数据包只经过一条链，而非全部三条。NAT 被分离到独立的表中，且输入和输出接口在 FORWARD 链中都可用。<sup>[3](https://man7.org/linux/man-pages/man8/iptables.8.html)</sup>

## 规则如何组织

iptables 和 ip6tables 在内核中建立、维护和检查包过滤规则表。每张表包含内建链，也可以包含用户自定义链；每条链是一个规则列表，每条规则为匹配的数据包指定一个目标。<sup>[3](https://man7.org/linux/man-pages/man8/iptables.8.html)</sup> 五张表各有不同用途：

- **filter**，默认表，包含用于普通包过滤的 INPUT、FORWARD 和 OUTPUT 链。
- **nat** 在遇到创建新连接的数据包时被查询，包含 PREROUTING、INPUT、OUTPUT 和 POSTROUTING 链；IPv6 NAT 支持自内核 3.7 起可用。
- **mangle** 执行专门的数据包修改，例如修改 TTL 或 DSCP 字段。
- **raw** 以高于连接跟踪的优先级注册在 netfilter 钩子上，主要用于通过 NOTRACK 目标豁免连接跟踪。
- **security** 处理强制访问控制网络规则，例如 SECMARK，在 filter 表之后被调用。<sup>[3](https://man7.org/linux/man-pages/man8/iptables.8.html)</sup>

如果数据包不匹配某条规则，则检查链中的下一条规则；如果匹配，则由目标决定接下来发生什么。到达内建链的末尾，或匹配 RETURN，则应用该链的策略。<sup>[3](https://man7.org/linux/man-pages/man8/iptables.8.html)</sup>

**连接跟踪。** 内核中的 NAT 被分为不操作数据包的连接跟踪和 NAT 代码本身。连接跟踪挂接到 LOCAL_OUT 和 PRE_ROUTING 钩子上，在数据包进入系统之前就看到它们，并做出比 NAT 所需更精细的状态区分。<sup>[6](https://www.iptables.org/documentation/HOWTO/netfilter-hacking-HOWTO-4.html)</sup> 规则使用 conntrack 匹配来接受属于已允许连接的数据包，这是 iptables 状态化包检测的基础。<sup>[10](https://www.idc-online.com/technical_references/pdfs/data_communications/Iptables_Stateful_Firewall_and_NAT_Routing.pdf)</sup>

用 iptables 设置的规则存在于内核中，重启后丢失；可通过 iptables-save 和 iptables-restore，或通过初始化脚本使其永久化。<sup>[2](https://netfilter.org/documentation/HOWTO/packet-filtering-HOWTO-3.html)</sup> 该工具在启动时通过 /run/xtables.lock 文件获取排他锁，并且拒绝以 setuid-to-root 方式运行，因为它信任运行时加载的匹配和目标共享库。<sup>[3](https://man7.org/linux/man-pages/man8/iptables.8.html)</sup>

## nftables 过渡

Netfilter 面向用户的工具经历了三代：ipchains（内核 2.2）、iptables（内核 2.4，2001 年）和 nftables——后者于 2014 年合并进内核 3.13，由同一个 netfilter 团队编写，以修复 iptables 的结构性问题。<sup>[8](https://serverside.com/blog/iptables-vs-nftables)</sup> 从用户角度看，nftables 用完全可配置的表和基础链取代了预定义的表和基础链，使用受 tcpdump 启发的紧凑语法而非基于 getopt_long 的命令行，并增加了通用集合基础设施，支持映射、判定映射和区间，以实现面向性能的数据包分类。<sup>[11](https://wiki.nftables.org/wiki-nftables/index.php/Main_differences_with_iptables)</sup>

**兼容层。** 自 2019 年起，iptables 命令在很大程度上只是一个兼容适配层。xtables-nft 工具，包括 iptables-nft、ip6tables-nft、arptables-nft 和 ebtables-nft，接受原生 iptables 语法，但通过 nft_compat 模块挂接到 nf_tables 内核子系统；在 Debian 10+ 和 Ubuntu 20.10+ 上，`iptables` 命令默认是 iptables-nft，因此现有的 iptables 规则已经运行在 nftables 引擎上。<sup>[8](https://serverside.com/blog/iptables-vs-nftables)</sup><sup> • </sup><sup>[12](https://manpages.debian.org/experimental/iptables/iptables-nft.8.en.html)</sup> 一个行为差异：使用 iptables-legacy 时，添加一条规则意味着从内核取回当前规则集、修改它、再重新加载，而 iptables-nft 告诉内核添加一条规则，使规则的添加和删除具有原子性。<sup>[12](https://manpages.debian.org/experimental/iptables/iptables-nft.8.en.html)</sup>

**发行版状态。** RHEL 9 弃用了 ipset 和 iptables-nft 软件包，包括 iptables、ip6tables、arptables 和 ebtables 工具，引导用户使用 `nft` 命令；RHEL 10 则完全不再提供旧版 `ip_tables` 内核模块。<sup>[9](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/10/html/configuring_firewalls_and_packet_filters/getting-started-with-nftables)</sup><sup> • </sup><sup>[8](https://serverside.com/blog/iptables-vs-nftables)</sup> Debian 自 Debian 10（Buster）起将 nftables 设为默认防火墙框架，并指出即使安装了 iptables，它实际上也通过兼容层使用 nftables。<sup>[13](https://wiki.debian.org/nftables)</sup> 在上游，iptables 仍处于维护模式，仍会收到如 2026 年 3 月的 1.8.13 等版本发布，而功能开发则在 nftables 中进行。<sup>[7](https://www.iptables.org/projects/iptables/downloads.html)</sup><sup> • </sup><sup>[8](https://serverside.com/blog/iptables-vs-nftables)</sup>

迁移工具在两个方向上都有：RHEL 提供 iptables-translate 和 ip6tables-translate 来转换单条规则，以及 iptables-restore-translate 和 ip6tables-restore-translate 来将整个保存的规则集转换为 nftables 语法。<sup>[9](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/10/html/configuring_firewalls_and_packet_filters/getting-started-with-nftables)</sup>

## 性能

2017 年 netfilter 研讨会的基准测试发现，相似的配置性能相似，iptables 在小型线性规则集上略优，而 ipset 使大型规则集具备与 nftables 集合相同的可扩展性。<sup>[14](https://workshop.netfilter.org/2017/wiki/images/7/7b/Benchmarking_nftables.pp.pdf)</sup> 情况随规则集规模而变化：iptables 的顺序规则求值随规则累积而退化，而 nftables 基于集合的匹配保持平稳，复杂的多条件规则更有利于 nftables 的分组匹配。<sup>[8](https://serverside.com/blog/iptables-vs-nftables)</sup><sup> • </sup><sup>[15](https://adhdecode.com/articles/nftables/nftables-performance-vs-iptables/)</sup> 一项关于多规则下 netfilter 吞吐量的研究发现，随着规则数量增长，吞吐量损失接近线性但幅度很小，且与传输协议和帧大小无关。<sup>[16](https://arxiv.org/abs/1502.05487)</sup>

在编排规模下，差异变得明显。Kubernetes 的 kube-proxy 在 1.33 版本中获得了正式可用的 nftables 模式，因为为简单防火墙设计的 iptables API 在扩展到数万个 Service 时表现不佳：iptables 模式的规则集随 Service 加端点的数量成比例增长，而 nftables 模式更高效地处理端点变化和内核数据包，不过这一差异只有在拥有数万个服务的集群中才会显现。出于兼容性原因，iptables 仍是 kube-proxy 的默认模式。<sup>[17](https://kubernetes.io/blog/2025/02/28/nftables-kube-proxy/)</sup><sup> • </sup><sup>[18](https://v1-33.docs.kubernetes.io/docs/reference/networking/virtual-ips/)</sup> 对于拥有数十条规则的单一服务器，实际选择取决于表达力、原子重载以及维护精力投向何处，而非吞吐量。<sup>[8](https://serverside.com/blog/iptables-vs-nftables)</sup>

## References

1. [The netfilter.org "iptables" project](https://iptables.org/projects/iptables/index.html)
2. [Linux 2.4 Packet Filtering HOWTO: So What's A Packet Filter?](https://netfilter.org/documentation/HOWTO/packet-filtering-HOWTO-3.html)
3. [iptables(8) - Linux manual page](https://man7.org/linux/man-pages/man8/iptables.8.html)
4. [About the netfilter/iptables project](https://iptables.org/about.html)
5. [ipchains (1998 - 2001), rip.so](https://rip.so/ipchains.html)
6. [Linux netfilter Hacking HOWTO](https://www.iptables.org/documentation/HOWTO/netfilter-hacking-HOWTO-4.html)
7. [Releases of the iptables project](https://www.iptables.org/projects/iptables/downloads.html)
8. [iptables vs nftables: What Changed and How to Migrate, Serverside.com](https://serverside.com/blog/iptables-vs-nftables)
9. [Getting started with nftables, Red Hat Enterprise Linux 10 documentation](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/10/html/configuring_firewalls_and_packet_filters/getting-started-with-nftables)
10. [iptables Stateful Firewall and NAT Routing, IDC technical reference](https://www.idc-online.com/technical_references/pdfs/data_communications/Iptables_Stateful_Firewall_and_NAT_Routing.pdf)
11. [Main differences with iptables, nftables wiki](https://wiki.nftables.org/wiki-nftables/index.php/Main_differences_with_iptables)
12. [iptables-nft(8), Debian Manpages](https://manpages.debian.org/experimental/iptables/iptables-nft.8.en.html)
13. [nftables, Debian Wiki](https://wiki.debian.org/nftables)
14. [Benchmarking NFTables, netfilter workshop 2017](https://workshop.netfilter.org/2017/wiki/images/7/7b/Benchmarking_nftables.pp.pdf)
15. [nftables vs iptables Performance: Benchmark Results (2026), ADHDecode](https://adhdecode.com/articles/nftables/nftables-performance-vs-iptables/)
16. [Performance Evaluation of netfilter (Niemann, Pfingst, Göbel, 2015), arXiv](https://arxiv.org/abs/1502.05487)
17. [NFTables mode for kube-proxy, Kubernetes blog](https://kubernetes.io/blog/2025/02/28/nftables-kube-proxy/)
18. [Virtual IPs and Service Proxies, Kubernetes v1.33 documentation](https://v1-33.docs.kubernetes.io/docs/reference/networking/virtual-ips/)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 17, 2026; Sep 30, 2026; Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 网络与安全 › Network defense and threats › Firewalls and perimeter defense

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/iptables

English original: https://www.edgepedia.org/iptables
