Prerequisites
- A Linux server with nftables
- Console access: one rule too many and SSH closes
01The ruleset
The input chain’s default policy is drop. Then we accept replies to outbound connections (established, related), loopback, the bare minimum of ICMPv6 that IPv6 needs to discover neighbours, then the desired ports. This ruleset is the one from the official nftables wiki.
flush ruleset
table inet firewall {
chain inbound_ipv4 {
icmp type echo-request limit rate 5/second accept
}
chain inbound_ipv6 {
icmpv6 type { nd-neighbor-solicit, nd-router-advert, nd-neighbor-advert } accept
icmpv6 type echo-request limit rate 5/second accept
}
chain inbound {
type filter hook input priority 0; policy drop;
ct state vmap { established : accept, related : accept, invalid : drop }
iifname lo accept
meta protocol vmap { ip : jump inbound_ipv4, ip6 : jump inbound_ipv6 }
tcp dport { 22, 80, 443 } accept
}
chain forward {
type filter hook forward priority 0; policy drop;
}
}02Check before loading
nft -c checks the syntax without applying anything. On Debian, the nftables service loads /etc/nftables.conf at every boot.
nft -c -f /etc/nftables.conf # check only
nft -f /etc/nftables.conf
nft list ruleset
systemctl enable nftables03Trap #1: Docker
Docker writes its own rules (address translation, forwarding of published ports). flush ruleset wipes them: containers lose network access until Docker restarts. And ports published by Docker go through the forward chain, not input: your inbound rules don’t filter them.
On a Docker host, only clear your own table, and restrict published ports by binding them to the intended address only (127.0.0.1:8080:80 for example).
# Instead of "flush ruleset" on a Docker host:
table inet firewall
delete table inet firewall04Trap #2: locking yourself out
On a remote machine, plan an automatic rollback: a job that clears the rules two minutes after loading, cancelled only if you can still log in.
( sleep 120 && nft flush ruleset ) & # safety net
nft -f /etc/nftables.conf
# New SSH session works? Then cancel the safety net:
kill %1Key takeaways
- Drop policy, then explicit exceptions.
- established/related first: replies come back.
- nft -c before loading, a safety net when remote.
- Docker host: never flush ruleset, and its ports go through forward.