To set up a UFW firewall on an Ubuntu server, allow SSH first, set the default policy to deny incoming traffic, allow the ports your sites need (usually 80 and 443), and then run sudo ufw enable. UFW (Uncomplicated Firewall) is a friendly front end for the kernel's netfilter rules, installed by default on Ubuntu but switched off until you enable it.
The order matters. If you enable the firewall before allowing SSH, you lock yourself out of a remote machine. This guide walks through a safe sequence, the rules a typical web server needs, and how to check and remove rules later.
A fresh server often listens on more ports than you realise: a database, a mail daemon, a monitoring agent. A firewall makes sure only the ports you chose are reachable from the internet. UFW writes the rules into netfilter (through nftables or iptables, depending on the release), so you never edit those tools directly.
UFW is a host firewall. It runs on the server itself and is a good second layer on top of any firewall your hosting provider offers in its control panel. Keep both in sync, or you may debug a closed port for an hour that is blocked in the provider panel.
On Ubuntu Server UFW is usually present. Check its state before changing anything:
If the command is not found, install it:
Default policies decide what happens to traffic that matches no rule. The standard choice is to deny everything coming in and allow everything going out:
Your server can still download updates and call APIs, but nothing reaches it unless a rule allows it.
Add the SSH rule first, so your current session survives:
OpenSSH is an application profile that UFW reads from /etc/ufw/applications.d/. List the profiles with sudo ufw app list. If you moved SSH to another port, allow that port number instead, for example sudo ufw allow 2222/tcp.
To slow down password guessing, you can rate limit SSH. This denies an address that opens six or more connections within 30 seconds:
Use either allow or limit for the same service, not both.
A typical site needs HTTP and HTTPS:
If Nginx or Apache registered profiles, you can write sudo ufw allow "Nginx Full" or sudo ufw allow "Apache Full" instead, which opens both ports.
You can also narrow a rule to a source address or a subnet:
The second rule lets only the private network reach PostgreSQL. Never open database ports to the whole internet.
Now turn it on:
UFW warns that the command may disrupt existing SSH connections. Because you already allowed SSH, answer y. Then check:
The (v6) lines cover IPv6. They are created when IPV6=yes is set in /etc/default/ufw, which is the default.
Delete a rule by its number or by repeating it with delete:
Numbers shift after each deletion, so run status numbered again before deleting a second rule. To start over, sudo ufw reset removes every rule and disables the firewall. To turn the firewall off without deleting rules, use sudo ufw disable.
| Task | Command |
|---|---|
| Show rules | sudo ufw status verbose |
| Allow a port | sudo ufw allow 8080/tcp |
| Deny a port | sudo ufw deny 3306/tcp |
| Allow a port range | sudo ufw allow 6000:6010/tcp |
| Rate limit a service | sudo ufw limit OpenSSH |
| Turn logging on | sudo ufw logging on |
| Delete a rule | sudo ufw delete allow 80/tcp |
Logs go to /var/log/ufw.log and are useful when a connection is blocked and you do not know why.
Not fully. Docker inserts its own netfilter rules for published ports, and those rules can bypass UFW. Bind containers to 127.0.0.1 or put a reverse proxy in front instead of relying on UFW alone.
If you enabled UFW without an SSH rule, use your provider's web console to log in and run sudo ufw allow OpenSSH. Most providers offer a rescue or VNC console for this reason.
It is optional but useful. A cloud firewall drops traffic before it reaches your server, while UFW protects the machine if the provider rules change or are misconfigured.
No. It limits which ports are reachable, but you still need SSH hardening, updates and brute force protection for the services you do expose.
Set the defaults to deny incoming and allow outgoing, allow SSH first, open only the ports you serve (80 and 443 for a website), and then enable UFW. Check with ufw status numbered, delete rules by number, and remember that Docker and your provider's firewall can change what is actually reachable.