$ run as a normal user# run as root (or with sudo)
The quick answer
#!/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;
}
}Step by step
- One tool only — do not mix with firewalld or ufw on the same host (
systemctl disable --now firewalld), or the rules fight. - **
inettable** (quick answer) — one table for IPv4 and IPv6. Theinputchain haspolicy drop: everything not allowed is dropped. - 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.
- Sets —
admin_netsholds 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). - Test without locking yourself out — schedule an undo first, then load:
Open a new SSH session; if it works, cancel the job (atq, atrm).
- Check syntax only —
nft -c -f FILEparses without loading. - Load at boot — the
nftablesservice loads/etc/nftables.conf(Debian/Ubuntu) or/etc/sysconfig/nftables.conf(RHEL — include your file there). - Counters — add
counterto a rule to see how often it matched:tcp dport { 80, 443 } counter accept, thennft list ruleset.
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.
Common problems
| Problem | Cause | Fix |
|---|---|---|
| Locked out after loading | SSH rule wrong or missing ct state established. | The at safety net; console access; fix and reload. |
| Docker or Kubernetes networking broke | flush ruleset deleted their rules / forward policy drop. | Do not flush everything on such hosts; use your own table, or firewalld. |
| Rules gone after reboot | nftables 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 directory | Table or chain does not exist yet. | Load the full file; check names. |
| IPv6 traffic blocked or open by surprise | Rules only for ip in an inet table. | Add IPv6 rules (ip6 saddr) or protocol-neutral ones. |
| firewalld also running | Two managers. | Use one of them only. |
Go further
- Command pages: nft, firewall-cmd, at.
- Next how-tos: Open a port in the firewall, Allow a port only from one IP address, Set up a firewall with UFW, Run a command once at a set time.
- Learn it: RHCSA — Firewall background, RHCSA — Test the firewall. Full guide: firewalld.
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.