How to Enable UFW on Ubuntu 26.04 Without Losing SSH

Aidin

Illustration of a brick wall for a guide on enabling the UFW firewall on Ubuntu Server 26.04

Hi everyone, this time I am continuing the Ubuntu Server 26.04 series by enabling the UFW firewall. The server is the same one from the previous articles: a static IP of 192.168.1.200 and SSH already moved to port 2222 with SSH key login. I ran every step below myself and then tested it from a Windows client PC on the same LAN, so you can see for yourself which ports are open and which ones are actually blocked.

UFW stands for Uncomplicated Firewall, the firewall tool that ships with Ubuntu and uses commands that are easy to read. Behind the scenes UFW still uses iptables rules, but you never have to write long iptables rules yourself. The thing that most often locks people out of their own server is an SSH port that has been changed, and I cover that trap in its own step below.

If your server is not set up to this point yet, follow my guides to set a static IP on Ubuntu Server 26.04 and configure the SSH server on Ubuntu 26.04 with SSH keys first.

Preparation Before Enabling UFW

Warning: Do these steps from the server console directly, or from an SSH session that you keep open until every test is done. If the SSH rule is wrong, new sessions will be refused and you will need console access to fix it.

Check the UFW status

Output of sudo ufw status verbose showing Status inactive on Ubuntu Server 26.04

UFW comes preinstalled on Ubuntu Server 26.04, version 0.36.2, but it is not active yet. Check it with the following command.

sudo ufw status verbose

Status: inactive means the firewall is not filtering anything yet, and every port opened by a service can be reached from outside.

Check the OpenSSH profile

UFW OpenSSH application profile that only opens port 22/tcp on Ubuntu

UFW has application profiles that you can call by name. List the profiles and look at what the OpenSSH profile contains.

sudo ufw app list
sudo ufw app info OpenSSH

You can see that the OpenSSH profile only opens port 22/tcp. This is the trap. A lot of tutorials tell you to run sudo ufw allow OpenSSH, but my SSH server has already moved to port 2222. If I used this profile and then enabled UFW, port 2222 would be closed and SSH would drop right away. So for a custom SSH port, open the port directly instead of going through the profile.

Setting Up the Basic UFW Rules

Set the default policies

Setting the UFW default policies to deny incoming and allow outgoing connections

The default policies decide what happens to any connection that does not match a rule. For a server, deny all incoming connections and allow all outgoing ones.

sudo ufw default deny incoming
sudo ufw default allow outgoing

That way the server can still download updates, but no port can be reached from outside unless you open it yourself.

Allow SSH port 2222

Allowing SSH port 2222/tcp in UFW with the comment SSH

Open the SSH port before UFW is enabled. Add a comment so the rule is easy to recognize later.

sudo ufw allow 2222/tcp comment 'SSH'

You get two Rules updated lines, one for IPv4 and one for IPv6 marked (v6). That is because UFW on Ubuntu automatically creates rules for both IP versions.

Enabling UFW

Turn on the firewall

Output of sudo ufw enable showing Firewall is active and enabled on system startup

Once the SSH port is open, enable UFW.

sudo ufw enable

The message Firewall is active and enabled on system startup means UFW is now active and will start again automatically every time the server boots. I ran it from the console, so there was no extra prompt. If you run this command over SSH, UFW will ask Command may disrupt existing ssh connections. Only answer y if your SSH port was opened in the previous step.

Check the active rules

UFW status active with an ALLOW IN rule for port 2222/tcp

Check the status again.

sudo ufw status verbose

Now the status is active, logging is on at the low level, the default policy is deny for incoming and allow for outgoing, and there is one rule allowing port 2222/tcp from anywhere.

Testing the Firewall from a Windows Client PC

I ran these tests from a Windows PC on the same network as the server, using PowerShell. To have a port to test besides SSH, I started a temporary web server on port 8080 on the server with the command python3 -m http.server 8080.

Test SSH and a port that is not open yet

Windows PowerShell test where SSH on port 2222 works and port 8080 returns TcpTestSucceeded False

From PowerShell on the client PC, run the following two commands.

ssh -p 2222 mamangdigital@192.168.1.200 hostname
Test-NetConnection 192.168.1.200 -Port 8080

SSH to port 2222 still works and returns the server name. Port 8080, on the other hand, gives TcpTestSucceeded : False even though the web server on that port is running. That means UFW is already blocking ports that are not allowed.

Open a port for the LAN only

Opening port 8080 in UFW only for the 192.168.1.0/24 subnet

A port does not have to be open to everyone. The command below opens port 8080 only for devices on the 192.168.1.0/24 network. Adjust it to match your own network.

sudo ufw allow from 192.168.1.0/24 to any port 8080 proto tcp comment 'Web LAN'
Port 8080 test from Windows succeeding with TcpTestSucceeded True and status 200

Repeat the test from the client PC.

Test-NetConnection 192.168.1.200 -Port 8080
(Invoke-WebRequest http://192.168.1.200:8080 -UseBasicParsing).StatusCode

Now TcpTestSucceeded is True and the web page returns status 200. Devices outside the 192.168.1.0/24 network still cannot reach this port.

Managing UFW Rules

List the rules with numbers

List of numbered UFW rules from the ufw status numbered command

To change or delete a rule, first list the rules along with their numbers.

sudo ufw status numbered

Every rule has a number in square brackets. Notice that the IPv6 rules get their own numbers too.

Rate limit SSH connection attempts

Changing the SSH rule for port 2222 to a limit rule in UFW

Change the SSH rule from allow to limit so password guessing attacks from a single IP get blocked right away.

sudo ufw limit 2222/tcp comment 'SSH'

According to the official ufw manual, a limit rule denies an IP that opens 6 or more connections within 30 seconds.

Test of 8 connections to port 2222 from Windows with connections 6 through 8 blocked

I tested this from the client PC by opening 8 connections to port 2222 in a row.

1..8 | ForEach-Object { "Koneksi $_ : " + (Test-NetConnection 192.168.1.200 -Port 2222 -WarningAction SilentlyContinue).TcpTestSucceeded }

The first five connections succeeded, then connections 6 through 8 failed because the limit rule held them back. After 30 seconds with no new connections, that IP can connect again as usual.

UFW numbered status showing the LIMIT IN action for port 2222

In the numbered status, the action for port 2222 now reads LIMIT IN.

Delete a rule by number

Deleting UFW rule number 2 with the sudo ufw delete 2 command

The test web server on port 8080 is no longer needed, so I deleted its rule. In the list earlier that rule was number 2.

sudo ufw delete 2

UFW shows the rule it is about to delete and asks Proceed with operation. Type y and press Enter. Keep in mind that after a rule is deleted, the numbers of the rules below it shift up. Always run ufw status numbered again before deleting the next rule.

Checking the UFW Block Log

Read the log of blocked packets

UFW BLOCK log entries from IP 192.168.1.2 to port 8080 on Ubuntu

UFW logging is already on at the low level, and the output is saved in /var/log/ufw.log. After deleting the 8080 rule, I tried to reach port 8080 again from the client PC. To keep the log lines easy to read, I only pulled out the important parts.

sudo tail -n 4 /var/log/ufw.log | grep -oE 'UFW [A-Z ]+|SRC=[^ ]+|DPT=[^ ]+' | paste - - -

You can see UFW BLOCK from SRC=192.168.1.2, which is the IP of my client PC, to DPT=8080. SRC is the sender’s IP and DPT is the destination port on the server.

UFW LIMIT BLOCK log entries for connection attempts to port 2222

Connection attempts held back by the limit rule are logged with a different label, UFW LIMIT BLOCK.

sudo grep 'LIMIT BLOCK' /var/log/ufw.log | tail -n 3 | grep -oE 'UFW [A-Z ]+|SRC=[^ ]+|DPT=[^ ]+' | paste - - -

This log is handy for finding out which IPs are trying to get into your server.

Restricting SSH to the LAN Only

Replace the SSH rule with a LAN only rule

UFW limit rule for SSH port 2222 that only allows the 192.168.1.0/24 subnet

The server in this tutorial is only managed from my home network, so SSH does not need to be open to everyone. Add a limit rule that only applies to the LAN subnet.

sudo ufw limit from 192.168.1.0/24 to any port 2222 proto tcp comment 'SSH LAN'
Deleting the UFW limit 2222/tcp rule that was open to anywhere

Then delete the old rule that is open to anywhere. Besides using the number, you can also delete a rule by writing the rule out again.

sudo ufw delete limit 2222/tcp

Check the final result

Final UFW status allowing only SSH port 2222 from the LAN with LIMIT IN
sudo ufw status verbose

In the end only one rule is left: port 2222/tcp with LIMIT IN from 192.168.1.0/24. The IPv6 rule is gone as well because the new rule only applies to that IPv4 subnet. SSH from the client PC still gets in, and every other port is closed.

Note: Do not restrict by subnet like this on a VPS on the internet unless you have a fixed public IP or a VPN. For a VPS, sudo ufw limit 2222/tcp is enough.

Common Problems and How to Fix Them

Locked out of SSH after ufw enable

This is usually because what got opened was the OpenSSH profile or port 22, while SSH runs on a different port. Log in from the server console, then open the correct SSH port or turn UFW off for the time being.

sudo ufw allow 2222/tcp
sudo ufw disable

On a VPS, console access is usually available in the provider’s panel under a name like VNC console or web console.

The rule was added but the port is still closed

Make sure the service is actually running and listening on that port with sudo ss -tlnp. UFW only opens the path, it does not run the service. Also check whether there is a deny rule with a lower number, because UFW reads rules from the top and stops at the first one that matches.

The status goes back to inactive after a server restart

Make sure ufw enable ran without errors and the message said enabled on system startup. Also check with sudo systemctl status ufw, this service has to be enabled.

All rules are gone after ufw reset

The sudo ufw reset command deletes every rule and turns UFW off at the same time. Only use it when you really want to start from scratch, and open the SSH port again before running ufw enable.

At this point the Ubuntu 26.04 server is protected by a firewall: all incoming ports are denied, SSH can only be reached from the LAN with connection attempts rate limited, and blocks can be monitored through the log. If this server is short on RAM, a good next step is adding a swap file on Ubuntu 26.04. If you run into a different error while trying this, drop it in the comments along with the error message and we can work out a fix together.

Leave a Comment