nftables: more complex rules
When ufw is no longer enough: the structure of nftables tables and chains, a ready-to-use working config, address sets, rate limiting, and port forwarding.
Updated August 23, 2026
nftables is the very kernel mechanism that ufw runs on top of. You should use it directly when you need named address sets, rate limiting, different policies per interface, or port forwarding — that is, when your rules no longer fit into “allow a port.”
You don't need to use both tools at once: before switching, disable ufw with ufw disable, otherwise the rules will overlap and confuse you during debugging.
Structure
Rules live in tables, tables contain chains, and chains contain rules. A chain is tied to a packet processing stage (input, forward, output) and has a default policy.
apt install -y nftables
systemctl enable --now nftables
nft list ruleset
Working config
File /etc/nftables.conf:
#!/usr/sbin/nft -f
flush ruleset
table inet filter {
set admins {
type ipv4_addr
elements = { 203.0.113.5, 198.51.100.7 }
}
chain input {
type filter hook input priority 0; policy drop;
iif lo accept
ct state established,related accept
ct state invalid drop
ip protocol icmp icmp type echo-request limit rate 5/second accept
tcp dport 22 ip saddr @admins accept
tcp dport 22 ct state new limit rate 6/minute accept
tcp dport { 80, 443 } accept
}
chain forward {
type filter hook forward priority 0; policy drop;
}
chain output {
type filter hook output priority 0; policy accept;
}
}
Apply and save:
nft -c -f /etc/nftables.conf
nft -f /etc/nftables.conf
systemctl restart nftables
The -c flag only checks syntax without applying the rules. Always use it: a drop policy with a typo cuts off access, and you'll have to get back in through the console in the /hosting/vds/m/<id> panel.
Address sets and rate limiting
A named set is edited on the fly, without reloading the rules:
nft add element inet filter admins { 192.0.2.10 }
nft delete element inet filter admins { 198.51.100.7 }
nft list set inet filter admins
A dynamic set with a timeout is handy for custom blocks:
set blocked {
type ipv4_addr
flags timeout
timeout 1h
}
Forwarding and address substitution
Needed if a container or tunnel is running inside:
table ip nat {
chain prerouting {
type nat hook prerouting priority -100;
tcp dport 8443 dnat to 10.10.0.2:443
}
chain postrouting {
type nat hook postrouting priority 100;
oifname "eth0" masquerade
}
}
Common mistakes
| Mistake | Consequence | Solution |
|---|---|---|
No ct state established rule | Replies to outgoing requests are cut off | Add it as the first rule |
Forgot iif lo accept | Local services and databases break | Allow the loopback interface |
| Rules applied but not saved | Everything is open after a reboot | Write to /etc/nftables.conf |
| ufw and nftables together | Unpredictable result | Keep only one tool |
Test a new ruleset in a separate session without closing your working one. A simple safeguard:
sleep 300 && nft flush rulesetin the background — if you lock yourself out, access comes back on its own.
If you don't need these details, go back to ufw: it covers most tasks.