一、引言:为什么需要 nftables?
如果你是 Linux 系统管理员,你一定用过 iptables。这个诞生于 1998 年的防火墙工具,陪伴我们走过了二十多年的风雨。然而,随着网络环境日益复杂,iptables 的局限性也越来越明显:
规则复杂难维护——IPv4、IPv6、ARP 分别有独立的工具(iptables、ip6tables、arptables),语法不统一,配置容易出错。性能瓶颈——每条规则都要遍历链表查找,规则多了之后效率急剧下降。竞态条件——规则更新时可能产生短暂的安全缺口。
2014 年,Linux 内核 3.13 版本带来了 nftables,这是一个全新的网络过滤框架,旨在彻底解决这些问题。现在,nftables 已经成为 RHEL 8、Debian 10+、Ubuntu 20.04+ 的默认防火墙方案。
┌─────────────────────────────────────────────────────────────────────────┐│ iptables vs nftables 对比 │├─────────────────────────────────────────────────────────────────────────┤│ ││ ┌─────────────────────────────────────────────────────────────────┐ ││ │ 传统 iptables 架构 │ ││ │ │ ││ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ ││ │ │ iptables │ │ ip6tables │ │ arptables │ │ ││ │ │ (IPv4) │ │ (IPv6) │ │ (ARP) │ │ ││ │ └──────┬──────┘ └──────┬──────┘ └──────┬──────┘ │ ││ │ │ │ │ │ ││ │ └────────────────┴────────────────┘ │ ││ │ │ │ ││ │ ┌─────┴─────┐ │ ││ │ │ Netfilter │ ← 内核模块各自独立实现 │ ││ │ │ Hooks │ │ ││ │ └───────────┘ │ ││ └─────────────────────────────────────────────────────────────────┘ ││ ││ ┌─────────────────────────────────────────────────────────────────┐ ││ │ 现代 nftables 架构 │ ││ │ │ ││ │ ┌─────────────────────────────────────────────────────┐ │ ││ │ │ nft 命令行工具 │ │ ││ │ │ (统一管理 IPv4/IPv6/ARP/Bridge) │ │ ││ │ └────────────────────────┬────────────────────────────┘ │ ││ │ │ │ ││ │ ┌────────────────────────┴────────────────────────────┐ │ ││ │ │ nftables 虚拟机引擎 │ │ ││ │ │ (统一的字节码解释器,生成高效的过滤逻辑) │ │ ││ │ └────────────────────────┬────────────────────────────┘ │ ││ │ │ │ ││ │ ┌─────┴─────┐ │ ││ │ │ Netfilter │ ← 统一的钩子接口 │ ││ │ │ Hooks │ │ ││ │ └───────────┘ │ ││ └─────────────────────────────────────────────────────────────────┘ ││ │└─────────────────────────────────────────────────────────────────────────┘
图示说明:iptables 为每种协议维护独立的内核模块,而 nftables 使用统一的虚拟机引擎
1.1 nftables 的核心优势
特性 | iptables | nftables |
架构 | 多个独立工具(iptables/ip6tables/arptables) | 统一的 nft 命令 |
规则更新 | 非原子性,可能有安全窗口 | 原子性替换,无窗口期 |
查找效率 | 链表查找,O(n) | 哈希集合,O(1) |
语法 | 每个工具语法略有差异 | 统一声明式语法 |
灵活性 | 固定匹配条件 | 表达式系统,高度灵活 |
内存占用 | 规则重复加载 | 共享基础设施,内存高效 |
二、核心概念:庖丁解牛 nftables
要理解 nftables,我们需要先理解它的核心架构。与 iptables 的"表-链-规则"三层结构类似,nftables 也是类似的层次,但更加灵活和强大。
2.1 基础结构:表、链、规则
nftables 的基本结构与 iptables 类似,但更加简洁统一:
概念 | 说明 | 类比 |
Table(表) | 规则的容器 | 文件柜 |
Chain(链) | 数据包的检查点 | 文件柜中的抽屉 |
Rule(规则) | 具体的过滤动作 | 抽屉中的文件夹 |
┌─────────────────────────────────────────────────────────────────┐│ nftables 结构 ││ ││ ┌─────────────────────────────────────────────────────────┐ ││ │ Table (filter) │ ││ │ │ ││ │ ┌─────────────┐ ┌─────────────┐ │ ││ │ │ Chain: input│ │ Chain: forward │ ││ │ │ priority 0 │ │ priority 0 │ │ ││ │ │ type filter │ │ type filter │ │ ││ │ │ hook input │ │ hook forward│ │ ││ │ └──────┬──────┘ └──────┬──────┘ │ ││ │ │ │ │ ││ │ ▼ ▼ │ ││ │ ┌─────────────────────────────────────────────┐ │ ││ │ │ Rule 1: ip saddr 192.168.1.0/24 accept │ │ ││ │ │ Rule 2: tcp dport ssh accept │ │ ││ │ │ Rule 3: tcp dport http accept │ │ ││ │ │ Rule 4: ct state established accept │ │ ││ │ │ Rule 5: drop │ │ ││ │ └─────────────────────────────────────────────┘ │ ││ └─────────────────────────────────────────────────────────┘ ││ │└─────────────────────────────────────────────────────────────────┘
图示说明:表包含链,链包含规则,规则是最终的执行单元
2.2 链的类型
nftables 中的链分为两种类型,理解它们的区别非常重要:
类型 | 说明 | 用途 |
Base Chain(基础链) | 挂载到 Netfilter Hook 点的链 | 直接处理进入/转发/离开的数据包 |
Regular Chain(普通链) | 不挂载 Hook 点,仅用于跳转 | 模块化规则,便于复用 |
# 定义 Base Chain(挂载到 hook 点)chain input { type filter hook input priority 0; # ... 规则}# 定义 Regular Chain(用于跳转)chain my_services { tcp dport {22, 80, 443} accept}
2.3 Hook 点与数据包流向
nftables 相比 iptables 多了 Ingress Hook,这是位于网络驱动的最底层:
┌─────────────────────────────────────────────────┐ │ 数据包进入网卡驱动 │ └─────────────────────────────────────────────────┘ │ ▼ ┌─────────────────────────────────────────────────┐ │ Ingress Hook (第 5 优先权) │ │ (内核 4.2+ 支持,最先处理) │ └─────────────────────────────────────────────────┘ │ ▼ ┌─────────────────────────────────────────────────┐ │ Prerouting Hook │ │ (路由决策之前的最后机会) │ └─────────────────────────────────────────────────┘ │ ▼ ┌────────────┴────────────┐ ▼ ▼ ┌─────────────────┐ ┌─────────────────┐ │ 路由决策 │ │ 路由决策 │ │ (本机/转发) │ │ (本机/转发) │ └────────┬────────┘ └────────┬────────┘ │ │ ┌────────┴────────┐ │ ▼ ▼ │ ┌──────────────┐ ┌──────────────┐ │ │ Input │ │ Forward │ │ │ Chain │ │ Chain │ │ └──────┬───────┘ └──────┬───────┘ │ │ │ │ ▼ ▼ │ ┌──────────────┐ ┌──────────────┐ │ │ 本机进程 │ │ Output │ │ │ 处理 │ │ Chain │ │ └──────────────┘ └──────┬───────┘ │ │ │ ▼ │ ┌─────────────────────────┐ │ │ Postrouting Hook │◄─┘ │ (离开前的最后处理) │ └─────────────────────────┘ │ ▼ ┌─────────────────────────────────────────────────┐ │ 数据包离开网卡驱动 │ └─────────────────────────────────────────────────┘
图示说明:nftables 支持 6 个 Hook 点,比 iptables 多了 Ingress
2.4 Sets(集合):高效查找的秘诀
nftables 最强大的特性之一是 Sets(集合)。在 iptables 中,如果你想匹配多个 IP,需要写多条规则;在 nftables 中,可以用一个 Set 存储所有 IP,实现 O(1) 时间复杂度的查找。
# 定义一个 Set 存储黑名单 IPset blacklist { type ipv4_addr flags timeout elements = { 192.168.1.100, 10.0.0.50, 203.0.113.0/24 }}# 在规则中使用 Setchain input { ip saddr @blacklist drop}
💡性能对比:假设有 10000 个黑名单 IP,使用 iptables 的链表查找需要遍历 10000 次,而使用 nftables 的哈希集合只需 1 次查找。
2.5 Maps(映射):动态决策
Maps 类似编程语言中的字典,可以实现根据一个值查找另一个值:
# 端口映射:根据访问的端口转发到不同的后端map port_redirect { type inet_service : ipv4_addr elements = { 80 : 192.168.1.100, 443 : 192.168.1.100, 8080 : 192.168.1.200 }}chain prerouting { type nat hook prerouting priority 0; dnat to tcp dport map @port_redirect}
三、语法入门:从零开始
掌握了核心概念后,我们来学习 nftables 的基本语法。nftables 的语法采用声明式风格,比 iptables 更加清晰和易于理解。
3.1 安装与基本命令
# Debian/Ubuntusudo apt install nftables# RHELsudo dnf install nftables# 启动服务sudo systemctl enable --now nftables# 查看当前配置sudo nft list ruleset# 清空所有规则sudo nft flush ruleset
3.2 创建表和链
将下面的配置保存在文件中,使用 nft -f example.nft应用到服务器上
# 创建 inet 表(同时支持 IPv4 和 IPv6)table inet filter { # 创建 input 链(基础链,挂载到 hook) chain input { type filter hook input priority 0; # 规则写在这里 } # 创建 forward 链 chain forward { type filter hook forward priority 0; } # 创建 output 链 chain output { type filter hook output priority 0; }}
或使用命令行
# 创建 inet 地址族下的 filter 表nft add table inet filter# 在 filter 表中创建 input 链(并指定类型为 filter,钩子为 input,优先级可选)nft add chain inet filter input { type filter hook input priority 0 \; }
3.3 添加规则
# 允许回环接口nft add rule inet filter input iif lo accept# 允许已建立的连接nft add rule inet filter input ct state established,related accept# 允许 SSHnft add rule inet filter input tcp dport 22 accept# 允许 HTTP/HTTPSnft add rule inet filter input tcp dport {80, 443} accept# 默认拒绝nft add rule inet filter input drop
3.4 使用脚本文件
为了便于管理和版本控制,建议使用脚本文件:
# /etc/nftables.conf#!/usr/sbin/nft -fflush rulesettable inet filter { chain input { type filter hook input priority 0; # 基础规则 iif lo accept ct state established,related accept # 服务规则 tcp dport 22 accept tcp dport {80, 443} accept # 默认策略 drop } chain output { type filter hook output priority 0; ct state established,related accept }}
# 加载配置sudo nft -f /etc/nftables.conf# 查看配置sudo nft list ruleset
四、进阶特性:让防火墙更强大
nftables 相比 iptables 最大的优势在于其灵活的表达式和丰富的特性。下面介绍几个最实用的进阶特性。
4.1 Sets 实战:动态黑名单
Sets 是 nftables 最强大的特性之一,支持动态更新:
table inet filter { # 基础链定义 chain input { type filter hook input priority 0; # 允许已建立连接 ct state established,related accept # 允许回环 iif lo accept # 动态黑名单(timeout 表示自动过期) set blackhole { type ipv4_addr flags timeout } ip saddr @blackhole drop # 允许的服务 tcp dport 22 accept tcp dport {80, 443} accept # 默认拒绝 drop }}
# 添加 IP 到黑名单(立即生效)sudo nft add element inet filter blackhole { 203.0.113.50 }# 添加 IP 段到黑名单sudo nft add element inet filter blackhole { 192.168.100.0/24 }# 从黑名单移除sudo nft delete element inet filter blackhole { 203.0.113.50 }# 查看黑名单内容sudo nft list set inet filter blackhole
4.2 Maps 实战:智能端口转发
使用 Maps 可以实现智能的端口转发:
table nat { chain prerouting { type nat hook prerouting priority 0; # 端口映射:根据 dport 查找 target_addr map port_map { type inet_service : ipv4_addr elements = { 80 : 192.168.1.100, 443 : 192.168.1.100, 8080 : 192.168.1.200, 3306 : 192.168.1.201 } } # DNAT 转发 dnat to tcp dport map @port_map } chain postrouting { type nat hook postrouting priority 0; # MASQUERADE(动态 SNAT) oif eth0 masquerade }}
4.3 流量限制:防止 DDoS
table inet filter { chain input { type filter hook input priority 0; # 基础规则 iif lo accept ct state established,related accept # SSH 暴力破解防护:每个 IP 每分钟最多 5 次新连接 tcp dport 22 meter ssh_limit { type ip_addr size 65535 } { ip saddr limit rate 5/minute accept } tcp dport 22 drop # HTTP DDoS 防护:限制连接速率 tcp dport 80 meter http_flood { type ip_addr size 65535 } { ip saddr limit rate 100/second accept } tcp dport 80 drop # 默认拒绝 drop }}
4.4 复合条件匹配
table inet filter { chain input { type filter hook input priority 0; # 组合条件:特定 IP 访问特定端口 ip saddr 192.168.1.100 tcp dport 22 accept # 范围匹配:内网 IP 访问多个端口 ip saddr 192.168.0.0/16 tcp dport {22, 80, 443, 8080} accept # MAC 地址匹配 ether saddr 00:11:22:33:44:55 accept # 连接速率限制 tcp dport 80 ip saddr & 255.255.255.0 { 192.168.1.0 } limit rate 10/second accept }}
五、从 iptables 迁移
5.1 迁移工具
nftables 提供了 iptables-translate 工具,可以将 iptables 规则转换为 nftables 格式:
# 安装迁移工具sudo apt install iptables-nftables-compat # Debian/Ubuntusudo dnf install iptables-nft # RHEL# 转换单条规则sudo iptables-translate -A INPUT -p tcp --dport 22 -j ACCEPT# 输出: nft add rule ip filter input tcp dport 22 accept# 转换整个规则链sudo iptables-save | sudo iptables-restore-translate -f -# 查看所有转换sudo iptables-translate -L -n
5.2 混合使用的风险
虽然 nftables 可以与 iptables 混合使用,但不推荐:
# 风险:# 1. 规则执行顺序不确定# 2. 可能产生冲突# 3. 性能下降(需要维护两套规则)# 推荐做法:# 1. 备份现有 iptables 规则sudo iptables-save > /root/iptables.backup# 2. 停止 iptables 服务sudo systemctl stop iptablessudo systemctl mask iptables# 3. 使用 nftablessudo nft -f /etc/nftables.conf
5.3 持久化配置
# 保存当前配置到文件sudo nft list ruleset > /etc/nftables.conf# 设置开机自动加载# 编辑 /etc/systemd/system/nftables.service[Unit]Description=nftablesAfter=network-pre.targetWants=network-pre.target[Service]Type=oneshotExecStart=/etc/nftables.confExecReload=/etc/nftables.confExecStop=/etc/nftables.confRemainAfterExit=yes[Install]WantedBy=multi-user.target# 启用服务sudo systemctl enable --now nftables.service
六、常见问题与最佳实践
6.1 常见问题
问题一:规则不生效
# 1. 检查语法错误sudo nft -f -n /etc/nftables.conf # 试运行,不实际加载# 2. 查看错误信息sudo nft -a list ruleset# 3. 检查链是否正确挂载sudo nft list ruleset# 确认 chain 显示 "hook input priority 0;"
问题二:连接被意外断开
# 1. 先测试规则,不实际应用sudo nft -f -n /etc/nftables.conf# 2. 使用 timeout 延迟生效sudo nft -f /etc/nftables.conf &sleep 60 && sudo nft flush ruleset # 60 秒后自动清空# 3. 确保有"允许已建立连接"的规则ct state established,related accept
问题三:性能问题
# 1. 使用 Sets 替代多个 OR 条件# 错误: tcp dport {22, 80, 443, 8080, 9000, 9001, 9002}# 正确: 定义 set 存储端口# 2. 优化规则顺序# 高频规则放在前面tcp dport 80 accept # 常用,放在前面tcp dport 3306 accept # 不常用,放在后面# 3. 查看规则统计sudo nft list ruleset -a
6.2 最佳实践
实践一:使用原子更新
# 不要一条一条添加规则sudo nft add rule ...sudo nft add rule ...sudo nft add rule ...# 而是使用脚本一次性加载sudo nft -f /etc/nftables.conf# 整个配置原子性替换,无窗口期
实践二:模块化配置
# /etc/nftables.conf#!/usr/sbin/nft -fflush rulesetinclude "/etc/nftables/tables.nft"include "/etc/nftables/chains.nft"include "/etc/nftables/rulesets.nft"
实践三:日志记录
chain input { type filter hook input priority 0; # 记录被拒绝的连接 drop log prefix "nft-drop: " level info flags all # 可选:记录到达的包(调试用) # log prefix "nft-input: " level debug flags all}
实践四:定期备份
# 创建备份脚本#!/bin/bashBACKUP_FILE="/root/nftables-backup-$(date +%Y%m%d).conf"nft list ruleset > $BACKUP_FILEecho "备份完成: $BACKUP_FILE"# 添加到 crontab# 0 3 * * 0 /root/backup-nftables.sh
实践五:监控和告警
# 监控规则变化#!/bin/bashHASH=$(nft list ruleset | md5sum | cut -d' ' -f1)while true; do sleep 60 NEW_HASH=$(nft list ruleset | md5sum | cut -d' ' -f1) if [ "$HASH" != "$NEW_HASH" ]; then echo "nftables 规则已变更!" | mail -s "告警: 防火墙规则变更" admin@example.com HASH=$NEW_HASH fidone
七、总结
nftables 是 Linux 防火墙的未来。相比 iptables,它具有以下优势:
核心优势
- 统一架构:IPv4、IPv6、ARP、Bridge 一套语法
- 高性能:基于虚拟机机制,使用字节码解释器
- 原子更新:规则替换无窗口期,无竞态条件
- 灵活表达式:支持 Sets、Maps、复合条件
- 简洁语法:声明式风格,易于理解和维护
核心概念回顾
概念 | 说明 |
Table | 规则的容器 |
Chain | 数据包检查点(基础链或普通链) |
Rule | 具体过滤动作 |
Set | IP/端口集合,O(1) 查找 |
Map | 键值映射,动态决策 |
学习路径
- 入门:理解基本结构,掌握表、链、规则的创建
- 进阶:学习 Sets 和 Maps,实现动态规则
- 实战:在生产环境中应用,积累经验
- 精通:深入虚拟机机制,优化性能