Skip to Content
Mengty Learns
Foundations

Firewall Basics (UFW, nftables & Security Groups)

How stateful firewalls work, default-deny inbound rules, ufw vs nftables, cloud security groups, and why database ports must never be public.

explanationbeginnerTested with ubuntu 24.04 LTS (docs), ufw 0.36.2 (docs), nftables 1.0.9 (docs)Updated Nov 27, 2026

TL;DR

A stateful firewall tracks active connections and enforces default-deny inbound, allowing only explicit ports (22, 80, 443). Use ufw for simple single-host rules or nftables directly for complex routing, pair host rules with cloud security groups, and never expose database ports to the public internet.

Why it exists

Any service listening on 0.0.0.0 (an admin dashboard, Redis, Postgres, or a debug port) is immediately reachable from the public internet unless a firewall drops unsolicited packets first.

Mental model

Think of a stateful firewall as a building receptionist with a visitor log: strangers are turned away by default (default deny incoming), invited guests on the allowlist (443/tcp) are signed in (conntrack), and anyone you call from inside (default allow outgoing) is allowed to reply back on that same call.

▶ Flow: watch it work

Stateful Firewall: Packet Filtering (Accept vs Drop)

Overview of Stateful Firewall: Packet Filtering (Accept vs Drop). 4 steps.
Overview4 steps in this flow

Press Start or tap any step dot to trace the path.

▸All steps (4)

Do it

1. Default-deny UFW baseline (Ubuntu 24.04)

Always allow SSH before enabling ufw so you don't lock yourself out:

# Set default policies and allow SSH + HTTP/HTTPS (not run)
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw limit 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

# Enable and inspect active rules (not run)
sudo ufw enable
sudo ufw status verbose

2. ufw vs nftables vs cloud security groups

LayerWhere it runsBest for
Cloud Security GroupHypervisor / VPC outside your VMPrimary perimeter—blocks traffic before it ever reaches your VM or Docker NAT
ufwGuest OS (front-end to nftables)Simple port allowlists on a single Ubuntu/Debian server
nftablesLinux kernel netfilter hookCustom NAT, multi-interface routers, or advanced rate-limiting sets

3. Never make the database port public

Even with a strong password, Postgres (5432), MySQL (3306), and Redis (6379) should never be open to 0.0.0.0/0. Bind them to 127.0.0.1 when running on the same host, or restrict them to your private VPC subnet / WireGuard interface (ufw allow in on wg0 to any port 5432).

When you DON'T need this

  • Static sites / serverless platforms: Hosting static files on Cloudflare Workers means there is no VM, kernel firewall, or open TCP port to manage.
  • Behind a managed load balancer with no public VM IP: If your app instances live in a private subnet with zero public IP and a cloud security group only permits traffic from the load balancer, duplicating complex nftables rules inside each ephemeral container host is unnecessary.
  • Cost / complexity trade-off: Adding both cloud security groups and host ufw takes 5 minutes to configure and costs $0, in exchange for defense-in-depth if either layer is misconfigured.

Gotchas

  • Docker bypassing UFW: Publishing a container with -p 5432:5432 inserts DOCKER NAT rules that route traffic around UFW's INPUT chain. Always bind internal containers to 127.0.0.1:5432:5432 and keep your cloud security group closed.
  • Enabling ufw before allowing SSH: Running ufw enable before ufw allow 22/tcp drops your active SSH connection once the state or next connection resets.
  • Testing with REJECT vs DROP: DROP gives no response (slowing down port scans); REJECT returns ICMP unreachable immediately (helpful on internal LANs so clients fail fast instead of hanging).

Production checklist

  • Cloud security group allows inbound 80/443 (and 22 restricted to your IP or VPN if possible).
  • ufw status verbose shows Default: deny (incoming), allow (outgoing).
  • Database and cache ports (5432, 6379) are bound to 127.0.0.1 or private interfaces only.
  • External port scan (nmap or cloud check) confirms no unintended ports are reachable.

Check yourself

Check Yourself

1. You set UFW to default-deny incoming and allow only ports 22, 80, and 443. Your app makes an outbound HTTPS request to an external API. Why does the API's reply packet get through?

2. You run Postgres in Docker with -p 5432:5432 on an Ubuntu VPS with UFW enabled (5432 not allowed in UFW). Why might Postgres still be reachable from the internet?

Sources