Short answer
On Ubuntu 26.04 and 24.04, allow SSH first, then turn the firewall on: run sudo ufw allow OpenSSH, then sudo ufw enable and answer y. Check it with sudo ufw status verbose. Every other incoming port is now closed. Open the ones you need, for example sudo ufw allow 80/tcp and sudo ufw allow 443/tcp.
Tested on Ubuntu 26.04 and 24.04, two separate runs. On 24.04, every step is the same. Everything that differed
Before you start
- An Ubuntu 26.04 or 24.04 server and a user with sudo. If you only have root, create a sudo user first.
- Keep the terminal you are logged in with open until step 4 works. If something goes wrong, that session is how you fix it.
- Your provider’s web console as a way back in, in case you lock yourself out anyway.
ufw is already installed on Ubuntu 26.04 and 24.04, but it is off:
$ sudo ufw statusStatus: inactiveCheck which port SSH listens on. The OpenSSH rule below opens port 22. If you moved SSH to another port, you must allow that port instead (sudo ufw allow 2222/tcp):
$ sudo ss -tlnp | grep ':22 'LISTEN 0 4096 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=1296,fd=3),("systemd",pid=1,fd=68))LISTEN 0 4096 [::]:22 [::]:* users:(("sshd",pid=1296,fd=4),("systemd",pid=1,fd=69))Step 1: Allow SSH
Packages can ship an app profile, a named list of ports. openssh-server ships one called OpenSSH:
$ sudo ufw app listAvailable applications: OpenSSH$ sudo ufw app info OpenSSHProfile: OpenSSHTitle: Secure shell server, an rshd replacementDescription: OpenSSH is a free implementation of the Secure Shell protocol. Port: 22/tcpAllow it. ufw is still off, so this only saves the rule. It adds one rule for IPv4 and one for IPv6:
$ sudo ufw allow OpenSSHRules updatedRules updated (v6)ufw status shows nothing while ufw is off. To see the rules you have saved, use show added:
$ sudo ufw show addedAdded user rules (see 'ufw status' for running firewall):ufw allow OpenSSHStep 2: Check the default policy
Many guides start with sudo ufw default deny incoming and sudo ufw default allow outgoing. On both releases these are already the defaults, so you can skip them. DROP means incoming connections are ignored unless a rule allows them:
$ grep '^DEFAULT_.*POLICY' /etc/default/ufwDEFAULT_INPUT_POLICY="DROP"DEFAULT_OUTPUT_POLICY="ACCEPT"DEFAULT_FORWARD_POLICY="DROP"DEFAULT_APPLICATION_POLICY="SKIP"Step 3: Turn the firewall on
ufw warns you that SSH could be cut off. Type y only if sudo ufw show added listed your SSH rule:
$ sudo ufw enableCommand may disrupt existing ssh connections. Proceed with operation (y|n)? yFirewall is active and enabled on system startupThe terminal you typed this in keeps working: ufw lets connections that are already open carry on. “Enabled on system startup” means it comes back after a reboot (proved below).
Step 4: Check it from a new terminal
Open a NEW terminal on your computer and log in again with ssh. If that works, the SSH rule works, and you can close the old session. Then look at the whole setup:
$ sudo ufw status verboseStatus: activeLogging: on (low)Default: deny (incoming), allow (outgoing), disabled (routed)New profiles: skip To Action From-- ------ ----22/tcp (OpenSSH) ALLOW IN Anywhere22/tcp (OpenSSH (v6)) ALLOW IN Anywhere (v6)deny (incoming): everything not listed under “To” is closed. allow (outgoing): the server can still reach the internet, for updates and so on. Logging: on (low) means blocked connections are written to a log, used below.
Step 5: Open the ports your server needs
With the firewall on, a web server on port 80 is still running but no one can reach it. In the test, a request from outside got no answer at all, and ufw logged it. DPT=80 is the port that was asked for and SRC is the address it came from:
$ sudo grep 'DPT=80 ' /var/log/ufw.log | tail -n 12026-11-18T21:51:11.419452+00:00 shb-test kernel: [UFW BLOCK] IN=eth0 OUT= MAC=92:00:0a:87:2f:83:d2:74:7f:6e:37:e3:08:00 SRC=103.73.101.36 DST=203.0.113.10 LEN=60 TOS=0x00 PREC=0x00 TTL=49 ID=50957 DF PROTO=TCP SPT=37718 DPT=80 WINDOW=64240 RES=0x00 SYN URGP=0Open port 80 (HTTP) and 443 (HTTPS) for TCP, the protocol web servers use:
$ sudo ufw allow 80/tcpRule addedRule added (v6)$ sudo ufw allow 443/tcpRule addedRule added (v6)The change works at once, with no reload or restart: the web server answered from outside straight away. Adding a rule that already exists does no harm:
$ sudo ufw allow 80/tcpSkipping adding existing ruleSkipping adding existing rule (v6)Delete a rule
The easiest way is to type the rule again after delete. That removes the IPv4 and the IPv6 rule together:
$ sudo ufw delete allow 443/tcpRule deletedRule deleted (v6)You can also delete by number. status numbered shows the numbers, and IPv4 and IPv6 rules get a number each:
$ sudo ufw status numberedStatus: active To Action From -- ------ ----[ 1] OpenSSH ALLOW IN Anywhere[ 2] 80/tcp ALLOW IN Anywhere[ 3] OpenSSH (v6) ALLOW IN Anywhere (v6)[ 4] 80/tcp (v6) ALLOW IN Anywhere (v6)$ sudo ufw delete 2Deleting: allow 80/tcpProceed with operation (y|n)? yRule deletedThat deleted only the IPv4 rule. Port 80 is still open over IPv6, and every rule below it has a new number. So list the rules again before every numbered delete:
$ sudo ufw status numberedStatus: active To Action From -- ------ ----[ 1] OpenSSH ALLOW IN Anywhere[ 2] OpenSSH (v6) ALLOW IN Anywhere (v6)[ 3] 80/tcp (v6) ALLOW IN Anywhere (v6)Deleting by rule text then removes the IPv6 leftover. The IPv4 rule is already gone, which ufw says:
$ sudo ufw delete allow 80/tcpCould not delete non-existent ruleRule deleted (v6)Rate-limit SSH (optional)
ufw limit slows down password-guessing bots. It turns the SSH allow rule into a limit rule:
$ sudo ufw limit OpenSSHRule updatedRule updated (v6)Behind it are these firewall rules: an address may open 6 new connections in 30 seconds, and the 6th is refused:
$ sudo iptables -S ufw-user-input | grep -- '--dport 22'-A ufw-user-input -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -m recent --set --name DEFAULT --mask 255.255.255.255 --rsource -m comment --comment "\'dapp_OpenSSH\'"-A ufw-user-input -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -m recent --update --seconds 30 --hitcount 6 --name DEFAULT --mask 255.255.255.255 --rsource -m comment --comment "\'dapp_OpenSSH\'" -j ufw-user-limit-A ufw-user-input -p tcp -m tcp --dport 22 -m comment --comment "\'dapp_OpenSSH\'" -j ufw-user-limit-acceptIn the test, a second server opened 5 new SSH connections in a row and all 5 reached the server; the 6th and 7th were dropped. Ten seconds later the address was still refused, because every new try counts, even a refused one. The refused tries are logged as [UFW LIMIT BLOCK]:
$ sudo grep -c 'UFW LIMIT BLOCK' /var/log/ufw.log2To go back to a plain allow rule:
$ sudo ufw allow OpenSSHRule updatedRule updated (v6)The rules survive a reboot
The test server was rebooted at this point. SSH answered again, and ufw came back on its own with every rule:
$ sudo ufw statusStatus: active To Action From-- ------ ----OpenSSH ALLOW Anywhere80/tcp ALLOW AnywhereOpenSSH (v6) ALLOW Anywhere (v6)80/tcp (v6) ALLOW Anywhere (v6)If you turned ufw on before allowing SSH
ufw does not stop you. The warning is the only guard. In the test, ufw enable with no SSH rule worked, and from then on every new SSH login was refused:
$ sudo ufw enableCommand may disrupt existing ssh connections. Proceed with operation (y|n)? yFirewall is active and enabled on system startupDo not close the terminal you are in. It still works, because ufw lets connections that are already open through before it checks any rule. This is the line in ufw’s rules that does it:
$ sudo grep -n 'ctstate RELATED,ESTABLISHED -j ACCEPT' /etc/ufw/before.rules | head -n 125:-A ufw-before-input -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPTIn that same terminal, add the SSH rule. It works at once, and new logins get in again:
$ sudo ufw allow OpenSSHRule addedRule added (v6)If you already closed every session, log in through your provider’s web console (it does not use SSH) and run sudo ufw allow OpenSSH there.
Undo
To turn the firewall off and keep your rules for later:
$ sudo ufw disableFirewall stopped and disabled on system startupTo delete every rule and start again, use reset. It saves a dated copy of each rule file first, and it also turns ufw off:
$ sudo ufw resetResetting all rules to installed defaults. This may disrupt existing sshconnections. Proceed with operation (y|n)? yBacking up 'user.rules' to '/etc/ufw/user.rules.20261118_215733'Backing up 'before.rules' to '/etc/ufw/before.rules.20261118_215733'Backing up 'after.rules' to '/etc/ufw/after.rules.20261118_215733'Backing up 'user6.rules' to '/etc/ufw/user6.rules.20261118_215733'Backing up 'before6.rules' to '/etc/ufw/before6.rules.20261118_215733'Backing up 'after6.rules' to '/etc/ufw/after6.rules.20261118_215733'$ sudo ufw statusStatus: inactiveAfter a reset, start again at step 1. Do not run sudo ufw enable until the SSH rule is back.
What these steps changed on the server
Both runs recorded the server before the first step and after the last (see the Tested on box below). Nothing was installed and no service was enabled: ufw was already there. Only three files in /etc changed, /etc/ufw/ufw.conf (ufw is now on) and the two rule files user.rules and user6.rules. The disk holds about 6 MB more on 26.04 and 7 MB on 24.04, from logs and package lists, not from ufw.
A scan of the 1,000 most common TCP ports from a second server found the same two open ports before and after, 22 (SSH) and 80 (the test web server). The difference is how the other 998 answer. Before the firewall they refuse the connection. After it they give no answer at all, so a scanner has to wait for each one. Lynis, run on the test server before and after, moved from 59 to 61 on 26.04 and from 60 to 61 on 24.04. That is Lynis’s own score for that one server, not a certificate.
Ubuntu 26.04 vs 24.04: what changed
The commands and their output are the same on both releases. Only the versions underneath are different:
| What | Ubuntu 26.04 | Ubuntu 24.04 |
|---|---|---|
| System | Ubuntu 26.04.1 LTS7.0.0-30-generic | Ubuntu 24.04.4 LTS6.8.0-138-generic |
| ufw | 0.36.2-9build1 | 0.36.2-6 |
| iptables | 1.8.11-2ubuntu3 | 1.8.10-3ubuntu2 |
| openssh-server | 1:10.2p1-2ubuntu3.5 | 1:9.6p1-3ubuntu13.18 |
| linux-image-virtual | 7.0.0-30.30 | 6.8.0-138.138 |
ufw rules and output (allow, limit, delete, reset, enable prompt) | same text | same text |
| The rate limit from a second server | 5 connections reach SSH, the 6th and 7th are dropped | the same |
Profiles in ufw app list before anything is installed | OpenSSH only | OpenSSH only |
| Proof | Run log, 53 checks passed | Run log, 53 checks passed |
