新系统或需要原子规则更新、集合匹配和更好性能时,从 iptables 迁移到 nftables。Docker/Kubernetes/fail2ban 等动态写规则的组件要单独评估。
nft。iptables-save > /root/iptables-backup.rules
ip6tables-save > /root/ip6tables-backup.rules
iptables --version
iptables-translate -A INPUT -p tcp --dport 22 -j ACCEPT
iptables-translate -t nat -A POSTROUTING -o eth0 -j MASQUERADE
/etc/nftables.conf:table inet filter {
chain input {
type filter hook input priority 0; policy drop;
ct state established,related accept
iif lo accept
tcp dport 22 accept
tcp dport { 80, 443 } accept
icmp type echo-request accept
}
chain forward { type filter hook forward priority 0; policy drop; }
chain output { type filter hook output priority 0; policy accept; }
}
nft -c -f /etc/nftables.conf
nft -f /etc/nftables.conf
nft list ruleset
systemctl enable --now nftables
nft -f /etc/nftables.conf
确认 SSH/业务端口仍可用后,再清理旧 iptables 规则。
nft list ruleset 与预期一致。iptables-backup.rules 和旧服务配置。nft flush ruleset 清空 nft 规则。iptables-restore 恢复旧规则,重新启用 iptables 服务。iptables-nft 和 iptables-legacy 是两套后端;先确认当前实际使用哪套。nft 的集合和 map 语法与 iptables 不同,不要只做字符串替换。nft — nftablesiptables — 旧规则iptables-translate — 规则翻译tcpdump — 验证连通性