Firewall Cheat Sheet: ufw and nftables Rules
Checked against ufw, nftables ufw 0.36.2 man page (Ubuntu 24.04 noble); Ubuntu community UFW page; nftables wiki, read 28 Nov 2026 on 28 November 2026. Each section links to the official docs it was checked against.
This sheet is for developers who run their own Linux VPS and need a host firewall without becoming network engineers. It follows the ufw 0.36.2 man page shipped with Ubuntu 24.04, Ubuntu's UFW guide and the nftables wiki. None of these commands could be run on the Mac used to write it, so every row is marked untested; try them on a server where you have console access.
When to use what
On Ubuntu or Debian, start with ufw. It writes the underlying rules for you, and the syntax reads like a sentence: ufw allow from 10.0.0.0/8 to any port 22 proto tcp. Most single-server setups need four or five rules and never outgrow it.
Reach for nftables directly when you need things ufw does not express well: sets of addresses, several tables, NAT, or a ruleset you keep in Git and load in one atomic step with nft -f. Don't mix hand-written nft rules with ufw on the same host unless you understand how both hook in; debugging two layers of policy is miserable.
Whatever you use, the order of operations is the same: allow SSH, set the default to deny incoming, then enable. The ufw man page warns that enabling flushes its chains and can drop your current SSH session. Remember that a cloud provider's network firewall is a separate layer from these host rules. Use the CIDR calculator to check a range before you allow it, and the port reference to confirm what a port is for.
The man page's REMOTE MANAGEMENT section says to add the SSH rule before ufw enable because enabling flushes the chains.
sudo ufw allow sshAllow incoming SSH using the service name; do this before enabling
sudo ufw default deny incomingDrop new inbound connections unless a rule allows them
sudo ufw default allow outgoingLet the server start outbound connections freely
sudo ufw enableTurn the firewall on now and at boot
sudo ufw status verboseShow whether ufw is active, the default policies and every rule
sudo ufw status numberedList rules with numbers so you can delete or insert by position
sudo ufw --dry-run enablePrint the rules ufw would apply without changing anything
sudo ufw resetDisable ufw and remove all rules, back to the installed defaults
Port ranges and lists need a protocol; the man page example opens 80, 443 and 8080-8090 with proto tcp.
sudo ufw allow 80/tcpAllow HTTP on TCP only
sudo ufw allow proto tcp from any to any port 80,443,8080:8090Open a list of ports plus an inclusive range in one rule
sudo ufw allow from 192.168.0.0/24 to any port 22 proto tcpAllow SSH only from one subnet
sudo ufw deny from 207.46.232.182Block all traffic from a single address
sudo ufw limit ssh/tcpRate-limit SSH: deny an address that opens 6 or more connections in 30 seconds
sudo ufw delete 3Remove rule number 3 from status numbered
sudo ufw delete allow 80/tcpRemove a rule by repeating it after delete
sudo ufw insert 1 deny from 203.0.113.9Put a deny above existing allows so it is matched first
Examples use a table called filter, as the wiki does. -a shows rule handles, which you need to delete or replace a single rule.
sudo nft list rulesetPrint every table, chain and rule currently loaded
sudo nft -a list table filterList one table with handle numbers next to each rule
sudo nft add rule filter output ip daddr 8.8.8.8 counterAppend a rule that counts packets to one address
sudo nft insert rule filter output ip daddr 192.168.1.1 counterAdd a rule at the start of the chain instead of the end
sudo nft delete rule filter output handle 5Delete one rule by its handle
sudo nft flush chain filter outputRemove every rule in one chain but keep the chain
A file that starts with flush ruleset replaces everything in one atomic load, so you never sit half-configured.
sudo nft list ruleset > /etc/nftables.rulesSave the live ruleset to a file you can edit and version
sudo nft -f /etc/nftables.rulesLoad a ruleset file in one step
type filter hook input priority 0; policy drop;Chain header from the wiki example: drop inbound by default
ct state established,related acceptLet replies to connections you started come back in
tcp dport { ssh, http } acceptAccept a set of ports in one rule using a set literal
Worked example: Lock down a new web server with ufw
A fresh VPS runs SSH and a web server. You want SSH from your office range only, HTTP and HTTPS from anywhere, and nothing else in. Order matters, so add rules before enabling:
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow from 198.51.100.0/24 to any port 22 proto tcp
sudo ufw allow proto tcp from any to any port 80,443
sudo ufw enable
sudo ufw status numbered
The last command should list the SSH rule with your range as the source and the web ports rule. Rules that match both IPv4 and IPv6 apply to both, so the web rule may also appear with a (v6) marker. If SSH is missing from that list, don't close your terminal; add it from the open session.
Then test from outside. From another machine, a request to port 80 should connect and a connection to any other port should fail. If you later need to remove the web rule, run sudo ufw delete with its number from status numbered. Not run here: the steps follow the ufw 0.36.2 man page and Ubuntu's UFW guide.
Common mistakes
Running
ufw enableover SSH before adding an SSH allow rule.Add
ufw allow ssh(or a source-restricted rule for port 22) first, and keep a provider console handy.Writing
ufw allow 8000:8100without a protocol and getting an error.Ranges need
/tcpor/udp, orproto tcpin the full syntax.Opening a port range in ufw and forgetting it is open for every source address.
Add
from <range>to the rule so only the networks that need the ports can reach them.Deleting rules by number after an earlier delete has shifted the numbering.
Run
ufw status numberedagain before every numbered delete.Editing live nft rules one by one on a production box and losing track of what is loaded.
Keep the ruleset in a file that starts with
flush rulesetand load it withnft -f.
Related cheat sheets
Sources
- https://help.ubuntu.com/community/UFW
- https://manpages.ubuntu.com/manpages/noble/man8/ufw.8.html
- https://wiki.nftables.org/wiki-nftables/index.php/Simple_rule_management
- https://wiki.nftables.org/wiki-nftables/index.php/Quick_reference-nftables_in_10_minutes
How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.