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.
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)
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 verbose2. ufw vs nftables vs cloud security groups
| Layer | Where it runs | Best for |
|---|---|---|
| Cloud Security Group | Hypervisor / VPC outside your VM | Primary perimeter—blocks traffic before it ever reaches your VM or Docker NAT |
ufw | Guest OS (front-end to nftables) | Simple port allowlists on a single Ubuntu/Debian server |
nftables | Linux kernel netfilter hook | Custom 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
nftablesrules inside each ephemeral container host is unnecessary. - Cost / complexity trade-off: Adding both cloud security groups and host
ufwtakes 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:5432insertsDOCKERNAT rules that route traffic around UFW'sINPUTchain. Always bind internal containers to127.0.0.1:5432:5432and keep your cloud security group closed. - Enabling
ufwbefore allowing SSH: Runningufw enablebeforeufw allow 22/tcpdrops your active SSH connection once the state or next connection resets. - Testing with
REJECTvsDROP:DROPgives no response (slowing down port scans);REJECTreturns ICMP unreachable immediately (helpful on internal LANs so clients fail fast instead of hanging).
Production checklist
- Cloud security group allows inbound
80/443(and22restricted to your IP or VPN if possible). -
ufw status verboseshowsDefault: deny (incoming), allow (outgoing). - Database and cache ports (
5432,6379) are bound to127.0.0.1or private interfaces only. - External port scan (
nmapor 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
Linux Server Hardening (Ubuntu 24.04)
Harden a single Ubuntu 24.04 LTS VPS with SSH key-only auth, no root login, least-privilege users, unattended-upgrades, fail2ban vs rate limiting, and auditing.
Reverse Proxy & TLS Termination
How a reverse proxy terminates TLS with ACME/Let's Encrypt, routes traffic, sets forwarding headers, and compares Nginx, Caddy, and managed edges.