UFW Firewall on Ubuntu and Debian
UFW (Uncomplicated Firewall) is a simple interface for managing firewall rules on Ubuntu and Debian. It wraps the more complex iptables tool into easy, readable commands.
The basic principle is simple: block everything by default, then allow only what you need.
Requirements
- Ubuntu 20.04+ or Debian 11+
- SSH access to your server
- sudo privileges
Step 1 – Install UFW
UFW is included by default on Ubuntu. On Debian, install it if needed:
sudo apt update
sudo apt install -y ufwStep 2 – Set Default Policies
Block all incoming traffic and allow all outgoing traffic by default:
sudo ufw default deny incoming
sudo ufw default allow outgoingStep 3 – Allow SSH (Do This First!)
Before enabling the firewall, allow SSH so you don’t get locked out:
sudo ufw allow 22If you run SSH on a custom port (e.g. 2222):
sudo ufw allow 2222Step 4 – Allow Other Services
Allow only the ports your server actually needs:
# Web server
sudo ufw allow 80 # HTTP
sudo ufw allow 443 # HTTPS
# Proxmox web interface
sudo ufw allow 8006
# DNS
sudo ufw allow 53
# Custom application port
sudo ufw allow 3000You can also allow by service name instead of port number:
sudo ufw allow ssh
sudo ufw allow http
sudo ufw allow httpsStep 5 – Enable UFW
Once you have allowed SSH, enable the firewall:
sudo ufw enableYou will see a warning about SSH connections — type y and press Enter to confirm.
Step 6 – Check the Status
sudo ufw status verboseExample output:
Status: active
To Action From
-- ------ ----
22 ALLOW IN Anywhere
80 ALLOW IN Anywhere
443 ALLOW IN AnywhereManaging Rules
Delete a rule:
sudo ufw delete allow 80Allow from a specific IP only:
sudo ufw allow from 192.168.1.0/24 to any port 22This allows SSH only from your local network — useful for servers that don’t need public SSH access.
Deny a specific port:
sudo ufw deny 3306Disable UFW (turns off firewall):
sudo ufw disableReset all rules:
sudo ufw resetQuick Reference
| Command | What it does |
|---|---|
sudo ufw status verbose |
Show current rules and status |
sudo ufw allow 22 |
Allow port 22 (SSH) |
sudo ufw deny 3306 |
Block port 3306 |
sudo ufw delete allow 80 |
Remove a rule |
sudo ufw enable |
Enable the firewall |
sudo ufw disable |
Disable the firewall |
sudo ufw reset |
Reset all rules |
sudo ufw reload |
Reload rules after changes |
Recommended Minimal Setup
For a typical Linux server in a home lab:
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 22
sudo ufw enableThen add ports as you install services. Only open what you actually need.
Related Links
- UFW Documentation — Ubuntu community docs
- SSH Keys – The Right Way — secure your SSH access before locking down with UFW