1. nftables核心优势
与iptables对比
特性 | iptables | nftables |
语法结构 | 多命令分散(filter/nat等) | 统一语法,JSON式结构化配置 |
性能 | 依赖链表遍历 | 基于寄存器和哈希表优化 |
规则集管理 | 需多表多链协调 | 单规则集全局管理 |
扩展性 | 依赖内核模块 | 内置表达式和动态更新 |
IPv4/IPv6支持 | 需ip6tables分开配置 | 支持多协议族(ip/inet等) |
核心概念
- 表(Table):容器,关联协议族(如ip/ip6/inet)。
- 链(Chain):规则容器,绑定钩子(hook)和优先级。
- 规则(Rule):匹配条件 + 执行动作。
- 集合(Set):动态或静态的IP/端口集合。
- 字典(Map):键值对映射(高级动态规则)。
2. 基础配置实战
案例1:创建基础防护规则
# 创建名为filter的inet协议族表(支持IPv4/IPv6)
nft add table inet filter
# 创建输入链(hook input,优先级0)
nft add chain inet filter input {
type filter hook input priority 0;
policy drop; # 默认拒绝
}
# 允许已建立连接和ICMP
nft add rule inet filter input ct state established,related accept
nft add rule inet filter input ip protocol icmp accept
# 开放SSH和HTTP
nft add rule inet filter input tcp dport {22, 80, 443} accept
查看配置
nft list ruleset # 导出完整规则集
nft list table inet filter # 查看指定表
3. 高级功能实践
案例2:动态黑名单(IP集合)
# 创建名为blacklist的ipv4地址集合
nft add set inet filter blacklist { type ipv4_addr; flags dynamic; }
# 拦截黑名单IP并自动过期(60秒)
nft add rule inet filter input ip saddr @blacklist counter drop
nft add rule inet filter input tcp dport 22 add @blacklist { ip saddr timeout 60s }
案例3:VLAN间流量控制
# 创建vlan10和vlan20的过滤表
nft add table bridge vlan_filter
# 过滤不同VLAN间的ICMP和SSH
nft add chain bridge vlan_filter forward {
type filter hook forward priority 0;
}
nft add rule bridge vlan_filter forward vlan id 10 ip saddr 192.168.10.0/24 vlan id 20 ip daddr 192.168.20.0/24 tcp dport 22 accept
nft add rule bridge vlan_filter forward vlan id 20 ip saddr 192.168.20.0/24 vlan id 10 icmp type echo-request accept
4. 网络地址转换(NAT)
SNAT配置(共享上网)
nft add table nat
nft add chain nat postrouting { type nat hook postrouting priority 100; }
nft add rule nat postrouting oifname "eth0" masquerade
DNAT配置(端口转发)
nft add chain nat prerouting { type nat hook prerouting priority -100; }
nft add rule nat prerouting tcp dport 80 dnat to 192.168.1.100:8080
5. 从iptables迁移到nftables
迁移工具
# 将iptables规则转换为nftables语法
iptables-save > iptables.rules
iptables-restore-translate -f iptables.rules > nftables.conf
# 直接导入生效
nft -f nftables.conf
手动转换示例
# iptables原始规则
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# 对应的nftables规则
nft add rule inet filter input tcp dport 22 accept
6. 性能优化技巧
- 命名集合加速匹配:
# 定义端口集合
nft add set inet filter web_ports { type inet_service; elements = { 80, 443 } }
nft add rule inet filter input tcp dport @web_ports accept
- 原子规则加载:
# 批量更新规则(避免临时失效)
nft -f /etc/nftables.conf
- JIT编译器启用:
sysctl -w net.netfilter.nf_tables_jit_enable=1
7. 实战任务
任务1:构建Web应用防护
- 阻止SQL注入尝试:
nft add rule inet filter input tcp dport 80 \
payload base 0 @ 0,0 65535 \ # 匹配整个数据包
string "union select" \ # SQL注入特征
counter drop
- 限制每个IP每秒最多10个HTTP请求:
nft add rule inet filter input tcp dport 80 \
meter http_limit { ip saddr limit rate 10/second } \
counter accept
任务2:网络分流策略
- 根据源IP分流流量:
nft add map inet filter route_map { type ipv4_addr : ipv4_addr; }
nft add rule inet filter prerouting \
ip saddr { 192.168.1.100 } \
dnat to tcp dport map @route_map
8. 注意事项
- 服务管理:
# CentOS/RHEL
systemctl enable --now nftables
# Ubuntu/Debian
apt install nftables && systemctl start nftables
- 配置持久化:
# 保存规则到配置文件
nft list ruleset > /etc/nftables.conf
- 兼容模式:
- 通过nf_tables内核模块实现与iptables共存使用iptables-nft兼容层逐步迁移