Migrating from iptables to nftables
You'll learn how to convert your existing iptables firewall rules to the newer nftables format and understand key differences in behavior.
Why Migration Matters
If you've built a firewall using iptables (like we covered in lessons 436-442), you might want to migrate to nftables for better performance and simpler syntax. However, the two systems have different rule structures, so you can't just copy-paste rules between them.
The iptables-translate Tool
Linux provides iptables-translate specifically for converting old rules to nftables syntax. Think of it like a translator between two languages—it reads your iptables rules and outputs equivalent nftables commands.
Example workflow:
# Convert a single rule
iptables-translate -A INPUT -p tcp --dport 22 -j ACCEPT
# Output: nft add rule ip filter INPUT tcp dport 22 accept
You can also batch-convert entire rulesets:
iptables-save | iptables-restore-translate
This outputs a complete nftables configuration file you can review and apply.
Understanding Behavioral Differences
Not everything translates perfectly:
- Table structure: iptables has fixed tables (filter, nat, mangle). nftables lets you create custom tables with any name.
- Chain types: nftables explicitly declares chain types (filter, route, nat), while iptables inferred them from table names.
- Connection tracking: Both support stateful filtering, but nftables syntax uses
ct stateinstead of-m conntrack --ctstate. - No automatic reverse NAT: Unlike iptables, nftables requires explicit rules for both directions of NAT connections.
Always test translated rules in a safe environment before deploying to production—automated translation may need manual refinement for complex rulesets.
Key Takeaway: Use iptables-translate to convert existing iptables rules to nftables syntax, but always review and test the output since behavioral differences and structural changes require verification before production deployment.