Ubuntu — UFW Firewall
UFW (Uncomplicated Firewall) is the default firewall tool on Ubuntu. It wraps iptables with a simpler interface. This guide covers a complete baseline setup for a typical server.
Pre-requisites
- Ubuntu 20.04 or later
sudoaccess- Active SSH session — read the SSH warning below before enabling UFW
Defaults
UFW ships inactive. The sensible defaults for a server:
Allow SSH before enabling
Always add your SSH rule before running ufw enable. Enabling UFW without an SSH rule on a remote server locks you out immediately.
Common Allow Rules
By service name
UFW knows common services by name:
By port number
Port range
From a specific IP
From a subnet
Deny Rules
deny silently drops packets. reject drops them and sends back an error (faster timeout for the sender):
Rate Limiting
Built-in rate limiting blocks IPs that make 6+ connections within 30 seconds. Essential for SSH:
Use this instead of allow ssh on any public-facing server.
Enable / Disable
Check Status
Example output:
Status: active
To Action From
-- ------ ----
[ 1] 22/tcp LIMIT IN Anywhere
[ 2] 80/tcp ALLOW IN Anywhere
[ 3] 443/tcp ALLOW IN Anywhere
Delete Rules
By rule number (get numbers from sudo ufw status numbered):
By rule definition:
Baseline Setup — Typical Web Server
The minimum ruleset for a server running SSH, HTTP, and HTTPS: