Advanced
Searching inHow-to guides
How to search
  • Type the task in your own words (find port, open firewall, disk full) or paste an error message in quotes.
  • Choose "Quick answer" to see only the commands, or an area to stay in one field.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

How-to 229Network · Firewall

How to write firewall rules with nftables

iptables is old; on current Linux the kernel firewall is nftables, and firewalld and ufw are front-ends for it. Writing nftables rules directly gives you one readable file for IPv4 and IPv6, sets of addresses, and atomic reloads. This how-to writes a small "deny by default" server firewall, tests it without locking yourself out, and makes it load at boot.

  • 5 Lessons
  • 8 Commands

$ run as a normal user# run as root (or with sudo)

229.1

The quick answer

/etc/nftables.conf
#!/usr/sbin/nft -f
flush ruleset
 
table inet filter {
  set admin_nets {
    type ipv4_addr
    flags interval
    elements = { 203.0.113.0/24, 198.51.100.7 }
  }
 
  chain input {
    type filter hook input priority filter; policy drop;
    ct state established,related accept
    ct state invalid drop
    iif "lo" accept
    meta l4proto { icmp, ipv6-icmp } accept
    tcp dport 22 ip saddr @admin_nets accept
    tcp dport { 80, 443 } accept
    limit rate 5/minute log prefix "nft-drop: "
  }
 
  chain forward {
    type filter hook forward priority filter; policy drop;
  }
 
  chain output {
    type filter hook output priority filter; policy accept;
  }
}
nft -c -f /etc/nftables.conf
nft -f /etc/nftables.conf
nft list ruleset
systemctl enable nftables

229.2

Step by step

  1. One tool only — do not mix with firewalld or ufw on the same host (systemctl disable --now firewalld), or the rules fight.
  2. **inet table** (quick answer) — one table for IPv4 and IPv6. The input chain has policy drop: everything not allowed is dropped.
  3. The order of rules — established connections first (fast, and keeps your SSH session), then loopback, ICMP (needed for path MTU and ping), then the allowed ports. The last rule logs (rate-limited) what is dropped.
  4. Sets — admin_nets holds the addresses allowed to SSH; add one at runtime without reloading: nft add element inet filter admin_nets { 192.0.2.10 } (put it in the file too).
  5. Test without locking yourself out — schedule an undo first, then load:
echo "nft flush ruleset" | at now + 5 minutes
nft -f /etc/nftables.conf

Open a new SSH session; if it works, cancel the job (atq, atrm).

  1. Check syntax only — nft -c -f FILE parses without loading.
  2. Load at boot — the nftables service loads /etc/nftables.conf (Debian/Ubuntu) or /etc/sysconfig/nftables.conf (RHEL — include your file there).
  3. Counters — add counter to a rule to see how often it matched: tcp dport { 80, 443 } counter accept, then nft list ruleset.

229.3

Check that it worked

nft list ruleset shows the table; from an allowed address SSH works, from another address it times out; curl -I http://SERVER/ works; dropped packets appear in journalctl -k | grep nft-drop; after a reboot nft list ruleset shows the same rules.

229.4

Common problems

ProblemCauseFix
Locked out after loadingSSH rule wrong or missing ct state established.The at safety net; console access; fix and reload.
Docker or Kubernetes networking brokeflush ruleset deleted their rules / forward policy drop.Do not flush everything on such hosts; use your own table, or firewalld.
Rules gone after rebootnftables service not enabled or wrong config path.systemctl enable nftables; check the path for your distro.
Error: Could not process rule: No such file or directoryTable or chain does not exist yet.Load the full file; check names.
IPv6 traffic blocked or open by surpriseRules only for ip in an inet table.Add IPv6 rules (ip6 saddr) or protocol-neutral ones.
firewalld also runningTwo managers.Use one of them only.

229.5

Go further

1Why is the established/related rule first in the input chain?

Most packets belong to existing connections; accepting them first is fast and keeps your current SSH session alive.

2How do you test new firewall rules on a remote server safely?

Schedule an undo with at (for example nft flush ruleset in 5 minutes), load the rules, test a new login, then cancel the undo.

All commands in this chapter