Security and hardening · Beginner · 11 min
How to Set Up a UFW Firewall on Ubuntu 26.04
By Padding · Updated 15 Nov 2026
Ubuntu 26.04.1 LTS · UFW
You will turn on UFW with a deny-by-default policy, allow SSH before you enable it so you stay connected, open only the web ports your server needs, and learn to list, remove and limit rules.
On this page
A firewall decides which network connections reach your server. Without one, every program that listens on a port is reachable from the whole internet, including the ones you forgot about, such as a database. UFW, the Uncomplicated Firewall, is Ubuntu's front end for the kernel's packet filter. It turns short commands into the rules the kernel enforces.
In this tutorial you will set the default policy to deny incoming traffic, allow SSH, allow the ports a web server needs, turn the firewall on, and learn the commands for changing rules later. Read the SSH step before you turn anything on: the order of the steps is what keeps you connected. The recovery options are covered in /tutorials/harden-ssh-on-ubuntu-26-04, which also explains the console password a key-only account needs.
Step 1 · Check that UFW is installed and off
UFW is installed on Ubuntu Server by default. Check its state:
sudo ufw statusThe reply is Status: inactive. While it is inactive, the rules you add are stored but not enforced, so you can set everything up first. If the command is not found, install it with sudo apt install ufw.
Step 2 · Set the default policy
The defaults say what happens to traffic no rule mentions. The safe pair is to refuse everything that arrives and allow everything that leaves:
sudo ufw default deny incoming
sudo ufw default allow outgoingEach command confirms the new default, for example Default incoming policy changed to 'deny'. With these two lines, your server can still fetch updates and call APIs, but nothing can connect to it unless you add a rule.
Step 3 · Allow SSH before you enable the firewall
Do this step before the next one. If you enable a deny-by-default firewall without an SSH rule, your own connection is cut off and you cannot log back in over SSH. You would need your provider's web console to fix it.
The OpenSSH package registers an application profile called OpenSSH. Allow it:
sudo ufw allow OpenSSHThe reply is Rule added and Rule added (v6): one rule for IPv4 and one for IPv6. The profile covers TCP port 22. If you run SSH on another port, allow that port by number instead, for example sudo ufw allow 2222/tcp.
To slow down password guessing without extra software, use limit instead of allow. It still lets you in, but refuses an address that opens six or more connections within 30 seconds:
sudo ufw limit OpenSSHRunning limit for a profile you already allowed updates that rule from allow to limit, so you end up with one rule for it. Choose either allow or limit for SSH. For serious brute-force protection, add Fail2ban, covered in /tutorials/block-brute-force-logins-with-fail2ban.
Check it worked
List the rules that are waiting to be enforced:
sudo ufw show addedThe output starts with Added user rules (see 'ufw status' for running firewall): and includes a line such as ufw limit OpenSSH (or ufw allow OpenSSH). If you do not see an SSH rule, do not go on.
Step 4 · Allow the ports your server uses
Open only what you serve. A web server needs HTTP on port 80 and HTTPS on port 443:
sudo ufw allow 80,443/tcpA rule that names several ports must also name the protocol, which is why it ends in /tcp. If you installed Nginx from Ubuntu's own package, its profiles Nginx HTTP, Nginx HTTPS and Nginx Full are available (sudo ufw app list shows what is registered). The Nginx you install from nginx.org, as in /tutorials/install-nginx-on-ubuntu-26-04, does not register profiles, so use the port numbers.
Never open a port for a database, a cache or an admin panel to the whole internet. To let one machine reach a database port, allow that address only:
sudo ufw allow from 203.0.113.5 to any port 5432 proto tcpReplace the address with your app server's, and the port with your service's. The rule says: this one address may reach port 5432 over TCP, and nobody else may.
Step 5 · Turn the firewall on
Check the rule list one more time, then enable the firewall:
sudo ufw enableUFW warns Command may disrupt existing ssh connections. Proceed with operation (y|n)?. Type y. Because you allowed SSH first, your session stays open. UFW also starts at every boot from now on.
Check it worked
In a second terminal, open a new SSH connection to the server. It should succeed. Then look at the active rules:
sudo ufw status verboseThe output looks like this:
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip
To Action From
-- ------ ----
22/tcp (OpenSSH) LIMIT IN Anywhere
80,443/tcp ALLOW IN Anywhere
22/tcp (OpenSSH (v6)) LIMIT IN Anywhere (v6)
80,443/tcp (v6) ALLOW IN Anywhere (v6)The exact layout varies a little between versions. What matters is Status: active, the deny (incoming) default and one rule per port you allowed. From a computer outside the server, try a port you did not open. The connection should hang or be refused.
Troubleshooting
- You are locked out after
ufw enable: open your provider's web console, log in, and runsudo ufw allow OpenSSHorsudo ufw disable, then reconnect over SSH. ERROR: Could not find a profile matching 'OpenSSH': theopenssh-serverpackage registers it. Install it, or allow the port withsudo ufw allow 22/tcp.- A web page does not load after the firewall is on: confirm that ports 80 and 443 appear in
sudo ufw status, and that your cloud provider's own network firewall (a security group) allows them too. UFW and a provider firewall are two separate layers.
Step 6 · Change rules later
Never delete the SSH rules while you are connected over SSH, and remember that each SSH rule has an IPv4 and an IPv6 copy in the numbered list. Removing the one that matches your own connection drops your session the moment UFW applies it. Open a second session first, and only delete rules for services other than SSH.
Number the rules, then delete one by its number. Replace <number> with the number shown for the rule you want gone:
sudo ufw status numbered
sudo ufw delete <number>UFW asks you to confirm. Numbers shift after each deletion, so list them again before you remove another one. Deleting by rule text avoids that:
sudo ufw delete allow 80,443/tcpRules are matched from the top, and the first match wins. Turn logging on to see blocked connections, written to /var/log/ufw.log:
sudo ufw logging onTo start over, sudo ufw reset disables UFW and deletes all rules. Run it with your SSH session open, then repeat the steps in order: defaults, SSH, other ports, enable.
One thing UFW does not control
Ports that Docker publishes with -p bypass UFW's rules, because Docker writes its own rules into the kernel ahead of UFW's. A container started with -p 5432:5432 is reachable from the internet even when UFW denies 5432. Bind such ports to the loopback address instead (-p 127.0.0.1:5432:5432) or put the service on a Docker network without publishing it.
Next steps
- Make SSH itself harder to attack:
/tutorials/harden-ssh-on-ubuntu-26-04. - Ban addresses that keep guessing passwords:
/tutorials/block-brute-force-logins-with-fail2ban. - Audit what the server exposes:
/tutorials/audit-a-server-with-lynis.
Related tutorials
- How to Do the Initial Setup of a New Ubuntu 26.04 ServerYou will take a fresh Ubuntu 26.04 server from a root-only login to an updated machine with a daily-use account, your SSH key on it, a firewall and a known clock.
- How to Install Nginx on Ubuntu 26.04You will install the current stable Nginx 1.30 from the official nginx.org repository on Ubuntu 26.04, serve its test page, and learn where its files live and how to reload it safely.
- How to Log In to a Linux Server with SSH KeysYou will create an Ed25519 SSH key, install its public half on a server, log in without a password, and save the connection as a short name.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials