一、为什么迁移到nftables?
iptables统治Linux防火墙配置已经超过20年,但它存在一些架构上的先天不足:规则集是线性的、语法不够直观、用户态和内核态通信效率低。nftables作为新一代防火墙框架,从Linux内核3.13开始引入,到CentOS 8/RHEL 8和Ubuntu 20.04之后已成为默认方案。
nftables的优势:
-
更简洁的语法,支持脚本化配置
-
原子性规则替换(不会出现规则加载中间状态)
-
更好的性能,规则匹配使用哈希表
-
一次修改,同时影响IPv4和IPv6
-
更丰富的调试和诊断工具
二、nftables基础概念
2.1 核心概念
-
Table(表):最顶层容器,包含链和规则
-
Chain(链):规则的有序集合,有类型(filter/nat/route)和钩子(input/output/forward等)
-
Rule(规则):由匹配条件+动作组成
-
Set(集合):可高效查找的元素集合,如IP白名单
-
Map(映射):KV映射表,用于动态规则
2.2 与iptables的对应关系
iptables 概念 → nftables 概念
--------------------------------
iptable -t filter → table inet filter
INPUT chain → chain input {type filter hook input priority 0;}
-FLUSH → flush ruleset
-A INPUT -s 1.1 → add rule ip saddr 1.1.1.1 drop
三、nftables配置实战
3.1 基础防火墙配置
#!/usr/sbin/nft -f
# /etc/nftables.conf
flush ruleset
table inet filter {
# 入站流量的默认处理
chain input {
type filter hook input priority 0; policy drop;
# 允许已建立的连接
ct state established,related accept
# 允许回环接口
iif lo accept
# 允许ICMP(限制速率防洪水)
icmp type echo-request limit rate 10/second burst 20 packets accept
icmpv6 type { echo-request, nd-neighbor-solicit, nd-router-solicit } accept
# 允许服务端口
tcp dport { 22, 80, 443 } accept
# 记录被拒绝的流量(用于排障)
log prefix "nftables-drop: " flags all
}
# 转发链
chain forward {
type filter hook forward priority 0; policy drop;
}
# 出站流量默认允许
chain output {
type filter hook output priority 0; policy accept;
}
}
3.2 高级配置:IP白名单与限速
flush ruleset
table inet filter {
# IP白名单集合
set whitelist {
type ipv4_addr
flags interval
elements = {
10.0.0.0/8,
172.16.0.0/12,
192.168.0.0/16,
# 公司出口IP
218.75.100.50,
218.75.100.51
}
}
# 攻击源IP黑名单
set blacklist {
type ipv4_addr
flags timeout
auto-merge
}
chain input {
type filter hook input priority 0; policy drop;
ct state { established, related } accept
iif lo accept
# 自动拉黑:每分钟超过20次SSH连接尝试
tcp dport 22 meter ssh-meter {
type ipv4_addr; size 65535;
rate over 20/minute burst 5 packets
} add @blacklist { ip saddr timeout 3600s }
# 阻止黑名单IP
ip saddr @blacklist drop
# 管理端口仅限白名单IP
tcp dport 22 ip saddr @whitelist accept
# Web服务放通(速率限制)
tcp dport { 80, 443 } accept
log prefix "DROP: " drop
}
}
3.3 NAT配置(iptables MASQUERADE替代)
table ip nat {
chain prerouting {
type nat hook prerouting priority 0;
}
chain postrouting {
type nat hook postrouting priority 100;
# SNAT/VPN场景
oif eth0 masquerade
}
}
四、iptables到nftables的迁移工具
iptables-translate工具可以帮助将现有iptables规则转换为nftables语法:
# 查看单个规则的转换 iptables-translate -A INPUT -p tcp --dport 80 -j ACCEPT # 输出:nft add rule inet filter input tcp dport 80 accept # 批量转换所有规则 iptables-save > /tmp/rules.v4 iptables-restore-translate -f /tmp/rules.v4 > /tmp/rules.nft # 加载转换后的规则 nft -f /tmp/rules.nft
五、日常运维命令
# 查看当前所有规则 nft list ruleset # 查看特定表 nft list table inet filter # 查看规则计数 nft list ruleset -a # 监控网络流量实时统计 nft monitor # 添加临时规则(IP白名单) nft add rule inet filter input ip saddr 203.0.113.50 tcp dport 22 accept # 删除规则(通过handle号) nft -a list ruleset | grep "ssh" nft delete rule inet filter input handle 12
六、迁移注意事项
-
迁移前备份现有iptables规则:
iptables-save > /root/rules.v4.backup -
建议先在测试环境验证nftables规则
-
服务器远程配置时,务必设置crontab自动恢复脚本,防止把自己锁在外面
-
nftables的log输出在/var/log/kern.log中,配置rsyslog分离
-
部分老旧应用可能依赖iptables的特定行为,需要逐一测试
七、总结
从iptables迁移到nftables是Linux网络安全的必然趋势。虽然初期学习曲线稍陡,但nftables的简洁语法和强大功能会让你的防火墙管理更加高效。建议从简单场景开始,逐步过渡到完整的nftables配置。