How to Configure UFW (Uncomplicated Firewall) on Ubuntu 24.04 and 22.04
Set up the UFW firewall on an Ubuntu server: default policies, allowing SSH and web traffic, trusted IP addresses, rate limiting, deleting rules, IPv6 and logging.
UFW (Uncomplicated Firewall) is the firewall front end that ships with Ubuntu. It lets you decide which ports on your server accept connections, using short commands instead of raw nftables or iptables rules. This guide sets up a sensible baseline on Ubuntu 24.04 LTS and 22.04 LTS; the same commands work on Debian once the ufw package is installed.
Install UFW
UFW is installed by default on Ubuntu Server. If ufw is not found, install it:
sudo apt update
sudo apt install ufw
A fresh installation is inactive, so nothing is blocked yet:
sudo ufw status
Set the default policy
Start from "refuse everything coming in, allow everything going out", then open only what you need:
sudo ufw default deny incoming
sudo ufw default allow outgoing
Allow SSH before you enable the firewall
If you turn the firewall on over an SSH session without allowing SSH first, your next connection will be refused. Add the rule now:
sudo ufw allow OpenSSH
If SSH listens on a non-standard port, allow that port instead, for example sudo ufw allow 2222/tcp.
Enable UFW
sudo ufw enable
UFW warns that existing SSH connections may be disrupted; answer y. The firewall is now active and starts again on every boot. Check the result:
sudo ufw status verbose
Allow connections
Open a port by number, by service name, or as a range. A service name works for anything listed in /etc/services.
# Web server
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
# The same, by service name
sudo ufw allow http
sudo ufw allow https
# A range of ports
sudo ufw allow 6000:6007/tcp
When you leave out /tcp or /udp, UFW opens the port for both protocols.
Application profiles
Packages such as OpenSSH, Nginx and Apache register named profiles, kept in /etc/ufw/applications.d. List them, inspect one, then allow it by name:
sudo ufw app list
sudo ufw app info "Nginx Full"
sudo ufw allow "Nginx Full"
Allow a trusted IP address or network
To let one address reach every port:
sudo ufw allow from 203.0.113.10
More often you want to open one port to one address or subnet, for example a database that only your application server should reach:
sudo ufw allow proto tcp from 203.0.113.10 to any port 3306
sudo ufw allow proto tcp from 10.0.0.0/24 to any port 3306
Deny and rate-limit connections
With a default policy of deny, you rarely need explicit deny rules. They are useful for blocking one address that is causing trouble:
sudo ufw deny from 198.51.100.25
Rules are checked in order, so a deny rule added after a matching allow rule never applies. Put it first with insert:
sudo ufw insert 1 deny from 198.51.100.25
For SSH, limit is a better fit than deny. It refuses an address that opens six or more connections within 30 seconds, which slows down password guessing:
sudo ufw limit OpenSSH
Review and delete rules
Show the rules with their numbers:
sudo ufw status numbered
Delete a rule by number, or by repeating the rule after delete:
sudo ufw delete 3
sudo ufw delete allow 80/tcp
The numbers shift after each deletion, so run status numbered again before removing the next one.
To see what a command would do without applying it, add --dry-run:
sudo ufw --dry-run allow http
IPv6
UFW manages IPv4 and IPv6 together. Confirm that /etc/default/ufw contains this line, which is the default on current Ubuntu releases:
IPV6=yes
With it set, every rule you add is created for both address families, and ufw status lists the IPv6 copies marked (v6).
Logging
Blocked packets are recorded in /var/log/ufw.log. Logging is on at the low level by default; change it with:
sudo ufw logging medium
Disable, reload or reset
# Apply changes made to the configuration files
sudo ufw reload
# Turn the firewall off but keep the rules
sudo ufw disable
# Remove every rule and return to the installed state
sudo ufw reset
reset also disables the firewall and saves backup copies of the old rule files in /etc/ufw.
A note for Docker users
Docker writes its own firewall rules, and ports you publish with -p are reachable from outside even when UFW does not allow them. If a container should only be reachable from the server itself, bind it to the loopback address, for example -p 127.0.0.1:8080:80, and put a reverse proxy in front of it.
Frequently asked questions
Do I still need UFW if my hosting provider has a cloud firewall?
Either one can protect the server. Using both is reasonable: the provider firewall drops traffic before it reaches the machine, and UFW still applies if the server is moved or the provider rules are changed by mistake.
I enabled UFW and lost SSH access. What now?
Log in through the web console in your hosting control panel, run sudo ufw allow OpenSSH, and try again.
Which ports does a typical web server need?
SSH (22, or your custom port), HTTP (80) and HTTPS (443). Keep database and cache ports closed to the internet unless another server must reach them.