How to use nftables as a modern firewall
· Category: Linux
Short answer
nftables is the modern Linux firewall framework that replaces iptables with a unified syntax and better performance.
Steps
- Create a table:
sudo nft add table inet filter
- Add a chain:
sudo nft add chain inet filter input { type filter hook input priority 0 \; policy drop \; }
- Add rules:
sudo nft add rule inet filter input tcp dport 22 accept
- Save and restore:
sudo nft list ruleset > /etc/nftables.conf
Tips
inetfamily handles both IPv4 and IPv6 in one table.- Use sets and maps for efficient rule matching.
- Many distributions now default to nftables under the hood.
Common issues
- Syntax errors in ruleset files prevent loading.
- Conflicts with legacy iptables rules if both are active.