Build a Default-Deny Firewall with nftables
A default-deny firewall has a pleasantly simple rule: traffic is blocked unless you explicitly permit it. The difficult bit is applying that rule without disabling IPv6, upsetting container networking, or discovering that your SSH exception contains the wrong address just after pressing Enter.
This guide builds a stateful firewall for an ordinary dual-stack Linux host. Incoming and forwarded traffic default to drop. Outbound traffic remains permitted, because default-deny egress filtering is a separate job requiring an accurate inventory of DNS, DHCP, NTP, package repositories and application destinations.
Find out what already owns the firewall
Install the userspace tools using your distribution's package manager:
sudo apt install nftables
sudo dnf install nftablesDon't flush anything yet. First inspect the active rules and the services which may be managing them:
sudo nft list ruleset
systemctl is-active nftables firewalld ufw
sudo iptables-saveReplacing the complete ruleset is likely to break something if any of these manages rules on this machine:
- firewalld
- UFW
- Docker
- Podman
- libvirt
- Kubernetes
Docker rules are especially easy to erase and surprisingly tedious to reconstruct from memory. Either integrate your policy with that system or give your table a distinct name and avoid flush ruleset.
The configuration below assumes nftables is the only thing managing the firewall on this host. The nft manual confirms that flush ruleset removes every table, leaving no filtering until replacement rules are installed.
Write the ruleset
Save the following as /etc/nftables.conf. Replace the example SSH source addresses with the public addresses or networks you administer the machine from; the documentation ranges used here will not match real clients.
flush ruleset
table inet firewall {
chain input {
type filter hook input priority filter; policy drop;
ct state invalid counter drop
ct state { established, related } counter accept
iifname "lo" counter accept
ip protocol icmp counter accept
meta l4proto ipv6-icmp counter accept
ip saddr 203.0.113.10 tcp dport 22 ct state new counter accept
ip6 saddr 2001:db8:1234::/48 tcp dport 22 ct state new counter accept
tcp dport { 80, 443 } ct state new counter accept
limit rate 5/second burst 10 packets counter log prefix "nft input drop: "
counter
}
chain forward {
type filter hook forward priority filter; policy drop;
}
chain output {
type filter hook output priority filter; policy accept;
}
}One table for both IP versions, with state tracking
The inet family handles IPv4 and IPv6 in one table. That saves you maintaining two nearly identical firewalls which inevitably become two subtly different firewalls six months later.
Connection tracking makes the ruleset stateful:
- Replies belonging to connections initiated by the host match
established. - Associated flows can match
related. - Invalid packets are discarded early.
The Netfilter connection-tracking documentation shows the same pattern for a simple stateful firewall.
Loopback and ICMP are not optional
Loopback traffic must be accepted explicitly. Dropping it produces failures which look nothing like firewall problems: local databases disappear, monitoring agents fail, and applications start reporting connection errors against 127.0.0.1.
The ICMP rules deliberately accept more than ping. ICMP carries error reporting and path MTU information, and ICMPv6 also carries essential IPv6 control traffic. Blocking it wholesale is not hardening; it breaks the network protocol and leaves you waiting for an awkward packet size to reveal the damage.
Quick detour into a subtle one: meta l4proto ipv6-icmp also handles packets containing IPv6 extension headers, which a naive immediate-next-header test misses. The nftables packet-header documentation covers the difference.
Rate-limited logging, then the policy drops
The final two rules rate-limit log messages and count everything which reaches the default policy. There is no explicit drop because the chain policy already supplies it.
The rate limit is there for a reason: an unbounded log rule lets somebody on the network turn a port scan into disk and CPU consumption.
Remove the rules for services you do not run
The example permits HTTP and HTTPS from any source. If this is an SSH-only server, remove that rule. For a workstation, remove all three service rules and keep the state, loopback and ICMP rules.
Before opening a port, see what is actually listening:
sudo ss -lntupOpening TCP port 443 in nftables does not publish a service by itself. Equally, binding a daemon to 0.0.0.0 or :: does not mean the firewall permits access. The listening socket and firewall rule are separate controls, and both should agree with your intended exposure.
Validate, then apply with an automatic rollback
Ask nft to parse and validate the file without applying it:
sudo nft --check --file /etc/nftables.confThis catches syntax errors and invalid expressions. It cannot tell you that 203.0.113.10 is not your real administration address, or that SSH actually listens on port 2222. Check those yourself.
For a remote machine, save the current rules and arrange an automatic rollback. First create a restorable file containing an initial flush followed by the current ruleset:
sudo sh -c 'printf "flush ruleset\n" > /root/nftables.before'
sudo sh -c 'nft list ruleset >> /root/nftables.before'
command -v nftUse the path printed by command -v in the rollback command if it differs from /usr/sbin/nft:
sudo systemd-run --unit=nft-rollback --on-active=2m /usr/sbin/nft --file /root/nftables.before
sudo nft --file /etc/nftables.confTest with a new SSH session, not the old one
Keep the existing SSH session open, but don't trust it. Its packets will normally keep flowing through the established rule even if the new SSH admission rule is wrong, so that session proves less than it appears to.
Open a second SSH connection from a fresh terminal, and test the web service too if you kept it. Once the new connection works, cancel the rollback timer:
sudo systemctl stop nft-rollback.timerIf access fails, wait for the timer to restore the previous rules. A provider console or other out-of-band access is still worth having: automatic rollback reduces the risk without making remote firewall changes magically consequence-free.
Make the rules survive a reboot
Distributions don't all ship the same service unit. Inspect yours before enabling it:
systemctl cat nftables.serviceIf your unit loads /etc/nftables.conf, enable the service and restart it:
sudo systemctl enable nftables.service
sudo systemctl restart nftables.serviceA correct-looking nft list ruleset now says nothing about what loads after a reboot. Reboot while console access is available, then inspect the loaded rules again.
Verify from outside, over both protocols
List the kernel's active ruleset, including counters:
sudo nft list ruleset
sudo journalctl --dmesg --grep='nft input drop:'From another machine, scan the host rather than scanning localhost:
nmap -Pn -p 22,80,443,8080 SERVER_ADDRESSThe permitted ports should appear open when their services are listening. An unpermitted port will commonly appear filtered, because its packets are silently dropped.
Try both the IPv4 and IPv6 addresses. A firewall tested over only one protocol is half tested.
An accept is not always the final word
Actually, this bit is interesting: an accept verdict is not necessarily final when another base chain is registered later on the same hook. That later chain can still drop the packet, whereas a drop takes effect immediately.
The base-chain documentation explains the ordering rules. It's one more reason to identify other firewall managers before declaring the job finished.
By now the useful question has changed from "which ports did I remember to block?" to "which narrowly described traffic did I choose to permit?" That inversion is the whole value of default deny.