Skip to content

Menu

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 ssh

Allow incoming SSH using the service name; do this before enabling

From the docs, not run by us
sudo ufw default deny incoming

Drop new inbound connections unless a rule allows them

From the docs, not run by us
sudo ufw default allow outgoing

Let the server start outbound connections freely

From the docs, not run by us
sudo ufw enable

Turn the firewall on now and at boot

From the docs, not run by us
sudo ufw status verbose

Show whether ufw is active, the default policies and every rule

From the docs, not run by us
sudo ufw status numbered

List rules with numbers so you can delete or insert by position

From the docs, not run by us
sudo ufw --dry-run enable

Print the rules ufw would apply without changing anything

From the docs, not run by us
sudo ufw reset

Disable ufw and remove all rules, back to the installed defaults

From the docs, not run by us

Port ranges and lists need a protocol; the man page example opens 80, 443 and 8080-8090 with proto tcp.

sudo ufw allow 80/tcp

Allow HTTP on TCP only

From the docs, not run by us
sudo ufw allow proto tcp from any to any port 80,443,8080:8090

Open a list of ports plus an inclusive range in one rule

From the docs, not run by us
sudo ufw allow from 192.168.0.0/24 to any port 22 proto tcp

Allow SSH only from one subnet

Try it: CIDR CalculatorFrom the docs, not run by us
sudo ufw deny from 207.46.232.182

Block all traffic from a single address

Try it: IP Address ValidatorFrom the docs, not run by us
sudo ufw limit ssh/tcp

Rate-limit SSH: deny an address that opens 6 or more connections in 30 seconds

Try it: ssh-key-generatorFrom the docs, not run by us
sudo ufw delete 3

Remove rule number 3 from status numbered

From the docs, not run by us
sudo ufw delete allow 80/tcp

Remove a rule by repeating it after delete

From the docs, not run by us
sudo ufw insert 1 deny from 203.0.113.9

Put a deny above existing allows so it is matched first

From the docs, not run by us

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 ruleset

Print every table, chain and rule currently loaded

From the docs, not run by us
sudo nft -a list table filter

List one table with handle numbers next to each rule

From the docs, not run by us
sudo nft add rule filter output ip daddr 8.8.8.8 counter

Append a rule that counts packets to one address

From the docs, not run by us
sudo nft insert rule filter output ip daddr 192.168.1.1 counter

Add a rule at the start of the chain instead of the end

From the docs, not run by us
sudo nft delete rule filter output handle 5

Delete one rule by its handle

From the docs, not run by us
sudo nft flush chain filter output

Remove every rule in one chain but keep the chain

From the docs, not run by us

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.rules

Save the live ruleset to a file you can edit and version

From the docs, not run by us
sudo nft -f /etc/nftables.rules

Load a ruleset file in one step

From the docs, not run by us
type filter hook input priority 0; policy drop;

Chain header from the wiki example: drop inbound by default

From the docs, not run by us
ct state established,related accept

Let replies to connections you started come back in

From the docs, not run by us
tcp dport { ssh, http } accept

Accept a set of ports in one rule using a set literal

From the docs, not run by us

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 enable over 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:8100 without a protocol and getting an error.

    Ranges need /tcp or /udp, or proto tcp in 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 numbered again 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 ruleset and load it with nft -f.