UFW firewall setup on Ubuntu (with Docker notes)

A minimal UFW pattern for a public web-facing VPS, the IPv6 details people forget, and how to deal with Docker — which manipulates iptables behind UFW's back and silently bypasses your rules.

Disclosure: contains an affiliate link to Inferno VPS.

The minimum-viable UFW

sudo apt install -y ufw
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status verbose

Three lines of allow rules — SSH plus HTTP/HTTPS — for a typical web-facing box. Everything else inbound is denied.

Before enabling: confirm you can SSH back in from a second terminal. UFW lockouts are the #1 way to lose a server.

IPv6

UFW handles IPv6 by default on modern Ubuntu, but verify:

sudo grep IPV6 /etc/default/ufw
# IPV6=yes
sudo ufw status verbose
# Should list both v4 and v6 rules per port

If your provider gives you IPv6 connectivity, leave it on — many CDN edges and external monitoring probes prefer IPv6. Disabling it doesn't improve security; it just breaks tools.

Per-port-per-source rules

You don't have to expose everything to 0.0.0.0/0:

sudo ufw allow from 203.0.113.42 to any port 5432       # Postgres only from this office IP
sudo ufw allow from 10.0.0.0/24 to any port 9100        # node_exporter on private subnet
sudo ufw allow proto tcp from 192.0.2.0/24 to any port 22 comment 'office SSH'

Rate limit SSH

sudo ufw limit OpenSSH

UFW's rate limiter blocks an IP that opens 6+ connections in 30 seconds. Light defense; pair with fail2ban for stronger throttling.

The Docker problem

Docker installs its own iptables chains (DOCKER, DOCKER-USER) and inserts rules ahead of UFW's chain. When you publish a container port, Docker punches a hole that UFW does not see and cannot block:

# Compose
ports:
  - "5432:5432"          # publishes Postgres to 0.0.0.0:5432
                         # UFW shows "5432 DENY" — but Docker bypasses it
                         # Postgres is REACHABLE FROM THE INTERNET

Two cleanest fixes:

Fix A — Bind to localhost (recommended)

ports:
  - "127.0.0.1:5432:5432"

Docker only listens on the loopback interface. UFW becomes the source of truth again. This is the pattern we use across every guide on this site — Nginx (allowed via UFW on 80/443) proxies to the container locally; the container ports are never on a public interface.

Fix B — Add rules to DOCKER-USER

If you must publish to 0.0.0.0, edit /etc/ufw/after.rules to add an explicit DOCKER-USER rule. The Docker docs cover this in detail. It works; it's also fiddly and easy to get wrong.

Fix A is overwhelmingly more robust. Reach for B only when you have a specific reason to expose a container without a reverse proxy.

Listing what's actually open

sudo ufw status verbose                      # what UFW thinks
sudo ss -tlnp                                # what's actually listening
sudo iptables -L DOCKER-USER -n              # what Docker bypassed

The combination of these three tells you the truth. ss -tlnp is the one that matters most — if a port is bound to 0.0.0.0 there, it's reachable, period.

Common UFW mistakes

"Database in container is on the internet despite UFW deny"

Docker bypass. Use the localhost-bind fix.

"Locked out of SSH after enabling UFW"

You allowed OpenSSH on IPv4 but your provider gave you IPv6-only management connectivity. Or you blocked your own outbound DNS. Use the provider's console/VNC to recover; sudo ufw allow OpenSSH and try again.

"ufw allow worked, but UFW says inactive"

You forgot sudo ufw enable. Rules are stored but not loaded.

"Rules vanished after reboot"

UFW survives reboots when enabled with ufw enable. If you wrote raw iptables rules instead, those don't persist without iptables-persistent.

"UFW deny rule logs nothing"

sudo ufw logging on (or medium for more detail). Logs land in /var/log/ufw.log.

VPS with sane firewall defaults?

Inferno's images ship with UFW installed and ready; the network layer also includes DDoS protection so volumetric attacks don't even hit your UFW. Good baseline for any of the patterns above.

View Inferno plans →

What to do next


Author: Hanna Wagner — Frankfurt. Last tested: 2026-05-22 on Ubuntu 22.04 LTS, UFW 0.36.