# Netfilter

**Netfilter** 是 [Linux 内核](https://www.edgepedia.org/zh/linux-kernel)中的一个框架，允许已注册的内核模块在数据包经过 TCP/IP 协议栈时观察并处理它们，为数据包过滤、网络地址转换（NAT）和数据包修改提供基础。这个名字也指开发该框架及其用户空间工具的社区项目，其中最著名的是 iptables 和 nftables。

| 关键事实 | 详情 |
|---|---|
| 是什么 | 一个 Linux 内核框架，在数据包处理的固定点提供钩子，供防火墙、NAT 和修改代码使用<sup>[1](https://wiki.nftables.org/wiki-nftables/index.php/Configuring_chains)</sup> |
| 起源 | 由保罗·“拉斯蒂”·拉塞尔（Paul "Rusty" Russell）创立，旨在重新设计 Linux 2.2.x 的 ipchains 和 2.0.x 的 ipfwadm 系统<sup>[2](https://netfilter.org/about.html)</sup> |
| 用户空间工具 | iptables（Linux 2.4.x 及更高版本中的过滤和 NAT）、nftables（较新的 nf_tables 框架）、conntrack-tools、libnetfilter_queue<sup>[3](https://netfilter.org/projects/iptables/index.html)</sup><sup> • </sup><sup>[4](https://www.netfilter.org/projects/nftables/manpage.html)</sup> |
| 钩子 | ingress、prerouting、input、forward、output、postrouting；ingress 自 Linux 4.2 起存在于 netdev 族中，自 Linux 5.10 起存在于 inet 族中<sup>[1](https://wiki.nftables.org/wiki-nftables/index.php/Configuring_chains)</sup> |
| 排序 | 钩子上的操作按数值优先级递增的顺序运行；连接跟踪在 -200 运行，过滤在 0 运行，源 NAT 在 100 运行<sup>[5](https://wiki.nftables.org/wiki-nftables/index.php/Netfilter_hooks)</sup> |
| 许可证 | 自由软件，按 GNU GPLv2 或任何更高版本分发，与 Linux 内核其余部分相同<sup>[2](https://netfilter.org/about.html)</sup> |
| 治理 | 由 Netfilter 核心团队（Netfilter Core Team）维护，负责人为巴勃罗·内拉·阿尤索（Pablo Neira Ayuso）；拉斯蒂·拉塞尔是创始人和前任负责人<sup>[2](https://netfilter.org/about.html)</sup> |

## 钩子与数据包流

数据包穿越 Linux 网络栈时会经过一系列 Netfilter 钩子。prerouting 钩子在任何路由决策做出之前看到所有传入数据包；input 看到路由到本系统的数据包；forward 看到并非发往本系统的数据包；output 看到本地进程生成的数据包；postrouting 在路由之后、数据包即将离开系统之前看到所有数据包。ingress 钩子附加到特定网络接口，在数据包刚离开网卡驱动之后、甚至早于 prerouting 就能看到它们，并为早期过滤提供了 tc 之外的一种选择。<sup>[1](https://wiki.nftables.org/wiki-nftables/index.php/Configuring_chains)</sup>

每个钩子可承载多个已注册函数，并按优先级排序。在给定钩子内，Netfilter 按数值优先级递增的顺序执行操作，这一顺序既包括内部操作，也包括用户自定义链。连接跟踪分片重组在 -400 运行，连接跟踪本身在 -200 运行，目标 NAT 在 -100 运行，过滤在 0 运行，安全表在 50 运行，源 NAT 在 100 运行，连接跟踪辅助模块在 300 运行。因此，prerouting 钩子上优先级为 -300 的链会在连接跟踪之前运行。<sup>[5](https://wiki.nftables.org/wiki-nftables/index.php/Netfilter_hooks)</sup>

裁决与这种顺序的交互方式会让许多管理员感到意外。丢弃裁决立即生效，不再评估后续规则或链。接受裁决不一定是最终裁决：如果存在同一钩子类型且优先级更晚的另一条链，数据包会继续经过它；只有当同一钩子类型不存在更晚的链时，才能保证接受裁决是最终裁决。<sup>[1](https://wiki.nftables.org/wiki-nftables/index.php/Configuring_chains)</sup>

## 连接跟踪

连接跟踪又称 conntrack，是 Netfilter 的一个子系统，它将每个数据包与一条被跟踪的连接关联起来，使防火墙具有状态。它实现为一个 Netfilter 模块；每当数据包到达 prerouting 钩子时，连接跟踪都会根据该数据包创建一个 conntrack 元组，其他内核代码可以使用这一状态信息。<sup>[6](https://www.kernel.org/doc/mirror/ols2004v2.pdf)</sup> 连接跟踪处理在 prerouting 和 output 钩子中较早进行，以将数据包与被跟踪的连接关联起来；新的被跟踪连接在 input 和 postrouting 钩子的最后一步得到确认。<sup>[5](https://wiki.nftables.org/wiki-nftables/index.php/Netfilter_hooks)</sup>

规则可以匹配和设置连接元数据。nftables 的 conntrack 表达式暴露连接的状态、方向、标记、辅助模块、标签、区域、数据包和字节计数器，以及原始地址和回复地址；conntrack 语句设置标记、标签、区域或事件位。notrack 语句对选定的数据包禁用跟踪，但必须在优先级 -300（raw）或更早处应用，即在 conntrack 查找之前。<sup>[4](https://www.netfilter.org/projects/nftables/manpage.html)</sup>

conntrack-tools 软件包让管理员在用户空间控制这一状态。conntrack 命令行工具查看和管理内核内连接跟踪表，取代旧的 /proc/net/ip_conntrack 接口；conntrackd 守护进程支持高可用有状态防火墙，并可作为统计收集器。这些工具几乎完全由巴勃罗·内拉·阿尤索编写。<sup>[7](https://netfilter.org/projects/conntrack-tools/)</sup>

## iptables 与 nftables

通过命令行界面，用户空间工具 iptables 在 Linux 2.4.x 及更新内核上建立数据包过滤规则集；由于网络地址转换源自同一规则集，iptables 也管理 NAT，且该软件包附带用于 IPv6 的 ip6tables。<sup>[3](https://netfilter.org/projects/iptables/index.html)</sup> 在 iptables 中，filter、nat、mangle 和 raw 等表是预定义的，每张表都有一组固定的内置链，如 INPUT、OUTPUT 和 FORWARD；即使只需要其中一条，所有这些链也都会注册。<sup>[8](https://wiki.nftables.org/wiki-nftables/index.php/Main_differences_with_iptables)</sup>

**nftables** 用 nf_tables 内核子系统取代 iptables，其中“nf”代表 Netfilter；nft 命令在该框架中建立、维护和检查数据包过滤与分类规则。<sup>[4](https://www.netfilter.org/projects/nftables/manpage.html)</sup> 其设计在几个方面与 iptables 不同：

- 没有预定义的表或链。每张表都显式定义，只包含管理员添加的链、集合、映射、流表和有状态对象，并且只注册所需的基础链。<sup>[8](https://wiki.nftables.org/wiki-nftables/index.php/Main_differences_with_iptables)</sup>
- 一条规则可以执行多个动作。nftables 规则由表达式后跟一个或多个语句组成，从左到右求值，因此一条规则可以设置标记、计数、记录日志并作出裁决。<sup>[8](https://wiki.nftables.org/wiki-nftables/index.php/Main_differences_with_iptables)</sup>
- 基础链在某个钩子上注册，带有类型（filter、route 或 nat）、优先级以及 accept 或 drop 策略。nat 类型只看到一条流的第一个数据包，因此不得用于过滤。<sup>[1](https://wiki.nftables.org/wiki-nftables/index.php/Configuring_chains)</sup>

支持双向迁移。xtables-nft 工具，包括 iptables-nft、ip6tables-nft、arptables-nft 和 ebtables-nft，会针对 nftables 内核 API 运行经典 iptables 语法；与 iptables-legacy 不同，此后端下的规则添加和删除是原子的，而 iptables-legacy 会读取、修改并重新加载整个规则集。<sup>[9](https://man7.org/linux/man-pages/man8/xtables-nft.8.html)</sup> [红帽企业 Linux](https://www.edgepedia.org/zh/red-hat-enterprise-linux)（Red Hat Enterprise Linux）已弃用 iptables-nft 变体，并引导用户使用 nft 命令行工具。<sup>[10](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/10/pdf/configuring_firewalls_and_packet_filters/Red_Hat_Enterprise_Linux-10-Configuring_firewalls_and_packet_filters-en-US.pdf)</sup>

## 用户空间排队与流卸载

queue 语句通过 nfnetlink_queue 子系统将数据包传递给用户空间程序，并由 16 位队列号标识；用户空间必须返回丢弃或接受裁决，而在接受时，处理从下一个基础链钩子继续，而不是从下一条规则继续。<sup>[4](https://www.netfilter.org/projects/nftables/manpage.html)</sup> libnetfilter_queue 库为这些排队的数据包提供用户空间 API，从内核接收它们并发出裁决，或重新注入修改过的数据包；它弃用了较旧的 ip_queue/libipq 机制，由哈拉尔德·韦尔特（Harald Welte）编写。<sup>[11](https://www.netfilter.org/projects/libnetfilter_queue/index.html)</sup>

流表机制为已建立的连接提供快速路径。它利用连接跟踪绕过经典数据包转发路径，避免再次查询路由表，仅适用于 TCP 和 UDP，是一种与硬件无关的软件快速路径；带有 established 连接跟踪状态的规则会将匹配的流卸载到流表。<sup>[10](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/10/pdf/configuring_firewalls_and_packet_filters/Red_Hat_Enterprise_Linux-10-Configuring_firewalls_and_packet_filters-en-US.pdf)</sup>

## 项目历史与治理

Netfilter 项目由保罗·“拉斯蒂”·拉塞尔创立，他是 netfilter/iptables 的最初作者和负责人，创立项目是为了重新设计并大幅改进此前的 Linux 2.2.x ipchains 和 2.0.x ipfwadm 系统。早期贡献者包括马克·布歇（Marc Boucher），他推动在 iptables 中建立通用数据包选择框架，并编写了 mangle 表；还包括哈拉尔德·韦尔特，他的首个贡献是用于 IRC 的连接跟踪模块，后来编写了 ULOG 和 NFQUEUE 目标以及新的 libiptc。<sup>[2](https://netfilter.org/about.html)</sup><sup> • </sup><sup>[12](https://man7.org/linux/man-pages/man8/iptables.8.html)</sup>

决策由 Netfilter 核心团队做出，其成员拥有主源码树的提交权限，并选举一名成员担任负责人。当前活跃成员为巴勃罗·内拉·阿尤索（负责人）、约瑟夫·卡德莱奇克（Jozsef Kadlecsik）、弗洛里安·韦斯特法尔（Florian Westphal）和菲尔·萨特（Phil Sutter）；荣休成员包括拉斯蒂·拉塞尔、哈拉尔德·韦尔特、詹姆斯·莫里斯（James Morris）、马克·布歇和埃里克·勒布隆（Eric Leblond）。巴勃罗·内拉·阿尤索自 2011 年起担任共同维护者，后在哥本哈根（Copenhagen）举行的 2013 年 Netfilter 研讨会（Netfilter Workshop 2013）上正式成为负责人。<sup>[2](https://netfilter.org/about.html)</sup>

该软件是按 GNU GPLv2 或任何更高版本分发的自由软件，核心团队使用项目 PGP 密钥签署发布版本。<sup>[2](https://netfilter.org/about.html)</sup>

## 桥接流量

过滤桥接流量需要特殊处理，因为 iptables 接入的是路由栈。旧式 br_netfilter 模块会截获进入网桥的数据包，并假装它们正在被路由，因此 ip(6)tables 规则集也能看到它们；不鼓励使用该模块，而 nftables 不需要该模块，因为其功能无论协议族如何都能工作。<sup>[13](https://cdn.kernel.org/doc/html/latest/networking/bridge.html)</sup>

## References

1. [Configuring chains, nftables wiki](https://wiki.nftables.org/wiki-nftables/index.php/Configuring_chains)
2. [About the netfilter/iptables project, netfilter.org](https://netfilter.org/about.html)
3. [The netfilter.org "iptables" project](https://netfilter.org/projects/iptables/index.html)
4. [Man page of NFT, netfilter.org](https://www.netfilter.org/projects/nftables/manpage.html)
5. [Netfilter hooks, nftables wiki](https://wiki.nftables.org/wiki-nftables/index.php/Netfilter_hooks)
6. [Linux Symposium 2004, Volume Two (connection tracking in netfilter)](https://www.kernel.org/doc/mirror/ols2004v2.pdf)
7. [The netfilter.org "conntrack-tools" project](https://netfilter.org/projects/conntrack-tools/)
8. [Main differences with iptables, nftables wiki](https://wiki.nftables.org/wiki-nftables/index.php/Main_differences_with_iptables)
9. [xtables-nft(8), Linux manual page](https://man7.org/linux/man-pages/man8/xtables-nft.8.html)
10. [Red Hat Enterprise Linux 10: Configuring firewalls and packet filters](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/10/pdf/configuring_firewalls_and_packet_filters/Red_Hat_Enterprise_Linux-10-Configuring_firewalls_and_packet_filters-en-US.pdf)
11. [The netfilter.org "libnetfilter_queue" project](https://www.netfilter.org/projects/libnetfilter_queue/index.html)
12. [iptables(8), Linux manual page](https://man7.org/linux/man-pages/man8/iptables.8.html)
13. [Ethernet Bridging, The Linux Kernel documentation](https://cdn.kernel.org/doc/html/latest/networking/bridge.html)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 17, 2026; Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; 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/netfilter

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