Install UFW on Ubuntu: Set Up a Firewall Without Locking Yourself Out
Part of New VPS setup, our guide to this topic
To install UFW on Ubuntu 24.04, run sudo apt install ufw — the package usually ships with Ubuntu, so this mostly confirms it’s there. Before you enable it, allow SSH with sudo ufw allow OpenSSH, and only then run sudo ufw enable. From there you open ports with sudo ufw allow, see what’s open with sudo ufw status numbered, and remove a rule with sudo ufw delete.
On this page
This guide has no affiliate links in the steps.
UFW is step 6 of our new VPS setup checklist — the five minutes that stop the whole internet from knocking on your server’s ports. The checklist gives you the two commands that matter; this guide goes deeper: installing UFW, opening a port to your own IP only, and the one ordering mistake that locks you out of a fresh server.

UFW stands for Uncomplicated Firewall. It’s a front end for iptables, the kernel’s packet filter: you type short commands, and UFW writes the real rules underneath. By the end of this guide your server answers SSH and nothing else by default, with 80 and 443 open only if you serve a website.
Before you start
You need four things:
- A server running Ubuntu 24.04 LTS and a user with
sudo. The steps also work on Debian 12, where UFW isn’t installed by default. - An SSH session to the server — see how to SSH into a VPS if you haven’t logged in yet.
- Your provider’s web console (VNC or serial) open in another tab. If you mis-type a rule and lock yourself out, that’s how you get back in.
- Ten minutes. Step 4 is the only step that can cut you off, and step 2 is what prevents it.
In the commands, YOUR_IP and YOUR_PORT are placeholders. Replace them with your own values before you press Enter.
How to install UFW on Ubuntu: 7 steps
The order matters. Steps 2 and 4 only work in that sequence — enabling the firewall before you allow SSH is the classic lockout.
Install UFW
On the server:
serversudo apt update
sudo apt install ufw
Ubuntu 24.04 ships UFW pre-installed, so apt install ufw usually just confirms it’s already there. On Debian 12 it isn’t, and this same command is the first step there.
If it fails: Unable to locate package ufw. Run sudo apt update first, or confirm the image is Ubuntu/Debian-based — on Fedora or RHEL you’d manage the firewall with firewalld instead.
Allow SSH before you enable it
serversudo ufw allow OpenSSH
OpenSSH is UFW’s named app profile for port 22. This one rule is what keeps you out of the lockout in step 4. If you changed your SSH port, allow that port instead: sudo ufw allow YOUR_PORT/.
Check: UFW prints a confirmation that the rule was added, once for IPv4 and once for IPv6.
Set the defaults
serversudo ufw default deny incoming
sudo ufw default allow outgoing
deny incoming is the point of the whole exercise: anything you don’t explicitly open is refused. allow outgoing lets your server reach the internet — package updates, DNS lookups, whatever your apps need. You almost never want to change that second one.
Enable the firewall
serversudo ufw enable
UFW asks you to confirm, because enabling can cut existing SSH connections. Type y. Your current session keeps working; the rule from step 2 is what lets your next login in.
If it fails: New SSH logins time out after ufw enable. SSH wasn’t allowed first — step 2 got skipped. Log in through your provider’s web console and run sudo ufw allow OpenSSH, then try again.
Open the ports, IPs and ranges you need
Serve a website? Open 80 and 443:
serversudo ufw allow 80/tcp
sudo ufw allow 443/tcp
A single app port works the same way:
serversudo ufw allow 8080/tcp
To let only your own IP reach a service, restrict the source. Allow one IP for everything, or one IP to one port:
serversudo ufw allow from YOUR_IP
sudo ufw allow from YOUR_IP to any port 22 proto tcp
A range of ports is one rule. UFW needs the protocol (/ or /) on a range:
serversudo ufw allow 6000:6007/tcp
Check what’s open
serversudo ufw status
sudo ufw status numbered
status lists the active rules. numbered puts a number in front of each one — you’ll need those numbers in the next step. Add verbose to also see the default policy:
serversudo ufw status verbose
Check: A table that starts with Status: active, then To, Action and From columns listing each rule you opened.
Delete a rule
Delete by the number from status numbered:
serversudo ufw delete 2
Or by the rule itself:
serversudo ufw delete allow 8080/tcp
If you’ve made a real mess, sudo ufw reset wipes every rule and disables the firewall — but it also removes the OpenSSH rule, so don’t run it over SSH unless your console is ready.
Key takeaways
- Allow SSH before you enable UFW — that order is what keeps you out of a lockout.
sudo ufw allow OpenSSH, thensudo ufw enable, withdefault deny incomingunderneath.- Open a port with
allow, one IP withallow from, a range withallow 6000:6007/tcp. - Read
sudo ufw status numberedand remove a rule withsudo ufw delete <number>. - UFW is a front end for iptables; Docker’s published ports still bypass it.
A VPS for this guide
Everything above runs on any VPS with 1 GB of RAM. The cheapest 1 GB+ plan at each provider whose prices we read every day, IPv4 included:
| Plan · provider | vCPU · RAM · disk | You pay | Visit |
|---|---|---|---|
KVM VPS Special 1 GB RackNerd · Los Angeles +19 | 1 vCPU·1 GB·20 GB | $1.83/mo | |
VPS pico G11.5s netcup · Nuremberg | 1 vCPU·1 GB·30 GB | $2.00/mo | |
SSD VPS 1 CloudCone · USA | 2 vCPU·1 GB·30 GB | $2.33/mo | |
1 slice InterServer · New York +2 | 1 vCPU·2 GB·40 GB | $3.00/mo | |
NVMe 2 GB HostHatch · Amsterdam +15 | 1 vCPU·2 GB·10 GB | $4.00/mo |
Prices read Dec 4, 2026. gets that provider's deals on Telegram. Compare every 1 GB plan →
UFW vs iptables: what’s the difference
UFW isn’t a second firewall sitting next to iptables. It’s a front end: the kernel’s netfilter does the actual filtering through iptables rules, and UFW turns short commands like sudo ufw allow 22/tcp into those rules for you. Run sudo iptables -L after adding a few rules and you’ll see the chains UFW built.
You can manage a firewall with raw iptables — plenty of older guides do — but the commands are long, order-dependent and easy to get wrong. UFW exists to hide that. For a VPS you configure once and mostly leave alone, UFW’s defaults cover the common case with far less to remember.
On Ubuntu 24.04 the iptables command is itself a compatibility layer over nftables, the kernel’s newer packet filter. For day-to-day use nothing changes: you still write rules through UFW, and the backend sorts out the rest.
Common UFW problems
Locked out after ufw enable.New SSH connections time out because SSH wasn’t allowed first. Use the provider’s web console, run sudo ufw allow OpenSSH, then log back in. If the console won’t load, boot rescue mode and disable UFW from there.
ufw status says inactive after a reboot.According to the ufw man page, ufw enable also turns the firewall on at boot, so this usually means it was never enabled, or someone ran ufw disable. Run sudo ufw enable again. If it still drops after a reboot, check systemctl is-enabled ufw; if that says disabled, run sudo systemctl enable ufw.
ufw enable also switches the firewall on at boot. Source: manpages.ubuntu.com, captured Nov 20, 2026.
Docker’s published ports stay open whatever UFW says.Docker writes its own iptables rules that run before UFW, so a port published with -p 8080:80 is reachable even when UFW denies it. Bind containers to 127.0.0.1 or route through a proxy. Our Docker install guide explains this in full.
Docker’s own docs: published ports skip UFW’s rules. Source: docs.docker.com, captured Nov 20, 2026.
A port is open in UFW but the site still won’t load.Some providers filter traffic before it reaches your server — Oracle Cloud’s security lists, for example, or Hetzner’s optional Cloud Firewall. If your provider has one, open the port there too, then check the service is actually listening with sudo ss -tlnp.
After you set up UFW
The firewall is one wall, not the whole fort. These are worth the next hour:
- fail2ban or CrowdSec to cut the brute-force noise in your SSH logs.
- Tailscale so SSH isn’t reachable from the public internet at all.
- SSH key login with password login off — see how SSH key login works.
- Automatic security updates with
unattended-upgrades, so patches apply themselves. - The rest of the checklist — a sudo user, key login and off-server backups are the other steps of the new VPS setup checklist.
UFW itself needs almost nothing, so the smallest plan at any provider runs it fine. The table at the end lists the cheapest 1 GB plan at each provider we read daily.
Questions
Is UFW the same as iptables?
No. iptables (via the kernel’s netfilter) is the firewall that actually filters packets. UFW is a front end that writes those iptables rules for you, in a much shorter syntax.
Does UFW start automatically after a reboot?
Yes. sudo ufw enable turns the firewall on now and at every boot. If sudo ufw status says inactive after a reboot, run sudo ufw enable again and check that systemctl is-enabled ufw says enabled.
What’s the difference between ufw allow 22 and ufw allow OpenSSH?
Both open port 22. OpenSSH is a named application profile that maps to port 22, so it’s self-documenting. If you changed your SSH port, use the port form instead.
How do I open a port to only my IP address?
sudo ufw allow from YOUR_IP allows your IP for everything, and sudo ufw allow from YOUR_IP to any port 22 proto tcp allows it for one port. Use this for SSH and admin panels.
How do I undo a rule I added by mistake?
Run sudo ufw status numbered, find the rule’s number, then sudo ufw delete 2 (or the actual number). You can also delete by the rule text: sudo ufw delete allow 8080/tcp.
Do I still need UFW if I use Docker?
Yes — and you need to know Docker’s published ports bypass it. UFW protects the host’s own services; Docker adds its own rules. Bind containers to 127.0.0.1 or route through a proxy for anything that shouldn’t be public.
How we checked: The commands and defaults in this guide follow the ufw man page for Ubuntu 24.04, Ubuntu Server’s firewall page and the Ubuntu wiki’s UFW page, read on 2026-11-14. We did not run them on every provider’s images; UFW ships with Ubuntu, so cloud images differ little here. Where a provider also runs a network firewall, we say so. How we check.