Short answer
On Ubuntu 26.04 and 24.04, ssh.socket (systemd) holds the SSH port, so editing Port and running systemctl restart ssh changes nothing. Put the new port in /etc/ssh/sshd_config.d/, allow it in the firewall, then run sudo systemctl daemon-reload && sudo systemctl restart ssh.socket. Test a new login before you remove port 22.
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
- A sudo user you are logged in as over SSH. Keep this session open until the last step: it keeps working while you change the port.
- A free port above 1024 that nothing else uses. This guide uses
2222. - If your provider has a firewall in its own panel (outside the server), allow the new port there too. That panel is not part of this test.
Step 1: See who listens on port 22
$ systemctl list-sockets --no-pager | grep ssh0.0.0.0:22 ssh.socket ssh.service[::]:22 ssh.socket ssh.service/run/ssh-unix-local/socket sshd-unix-local.socket -The port belongs to ssh.socket, not to the SSH daemon. systemd listens on it and starts ssh.service when someone connects. This is socket activation, the default since Ubuntu 22.10, and it is why the old instructions no longer work.
Step 2: Add the new port next to 22
Write the ports in a drop-in file instead of editing /etc/ssh/sshd_config. Ubuntu’s sshd_config reads every .conf file in /etc/ssh/sshd_config.d/, and a package update never asks about a file of your own. When one Port line appears, sshd stops using the default 22, so list both:
$ printf 'Port 22\nPort 2222\n' | sudo tee /etc/ssh/sshd_config.d/10-port.confPort 22Port 2222Check the configuration before using it. sshd -t prints nothing when it is valid, and the error with its line number when it is not:
$ sudo sshd -tStep 3: Allow the new port in the firewall
$ sudo ufw allow 2222/tcpRules updatedRules updated (v6)Do this even if ufw is not turned on yet (a fresh server has it off): the rule is saved and applies the moment you enable it, so turning ufw on later cannot lock out the new port.
Why `systemctl restart ssh` does not change the port
Most guides end here with a restart of the SSH service. On the test server it ran without an error:
$ sudo systemctl restart ssh$ sudo ss -tlnp 'sport = :22 or sport = :2222'State Recv-Q Send-Q Local Address:Port Peer Address:Port ProcessLISTEN 0 4096 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=2567,fd=3),("systemd",pid=1,fd=69))LISTEN 0 4096 [::]:22 [::]:* users:(("sshd",pid=2567,fd=4),("systemd",pid=1,fd=70))Still only port 22, and nothing on 2222. The socket owns the port (systemd with PID 1 in the Process column), and it was not touched. A reboot would pick the change up, which is why some guides say a reboot is needed. It is not.
Step 4: Apply it the socket way
$ sudo systemctl daemon-reload && sudo systemctl restart ssh.socket$ sudo ss -tlnp 'sport = :22 or sport = :2222'State Recv-Q Send-Q Local Address:Port Peer Address:Port ProcessLISTEN 0 4096 0.0.0.0:2222 0.0.0.0:* users:(("sshd",pid=2898,fd=3),("systemd",pid=1,fd=79))LISTEN 0 4096 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=2898,fd=5),("systemd",pid=1,fd=81))LISTEN 0 4096 [::]:2222 [::]:* users:(("sshd",pid=2898,fd=4),("systemd",pid=1,fd=80))LISTEN 0 4096 [::]:22 [::]:* users:(("sshd",pid=2898,fd=6),("systemd",pid=1,fd=82))Now ports 22 and 2222 both listen, on IPv4 and IPv6, and your open session was not cut. daemon-reload runs Ubuntu’s sshd-socket-generator, which reads the ports from sshd_config and writes them into a setting for ssh.socket; restarting the socket makes it listen on them. You can read what it wrote:
$ cat /run/systemd/generator/ssh.socket.d/addresses.conf# Automatically generated by sshd-socket-generator [Socket]ListenStream=ListenStream=0.0.0.0:2222ListenStream=[::]:2222ListenStream=0.0.0.0:22ListenStream=[::]:22The empty ListenStream= clears the socket’s built-in port 22, and the lines after it are your ports. The file lives under /run, so it is written again at every boot from your drop-in: the change survives a reboot without you editing the socket unit. Do not edit ssh.socket itself or this file; change the drop-in and run step 4 again.
Step 5: Test a new login on the new port
From your own computer, open a second terminal and log in with -p (use your user and server address). The test server logged in to itself the same way:
$ ssh -p 2222 deploy@localhost echo logged in on 2222logged in on 2222If the login on 2222 hangs, a firewall in between drops it: check sudo ufw status and your provider’s firewall. If it says Permission denied, the port works and the problem is the login itself.
Step 6: Remove port 22
$ sudo sed -i '/^Port 22$/d' /etc/ssh/sshd_config.d/10-port.conf && sudo systemctl daemon-reload && sudo systemctl restart ssh.socket && sudo ss -tlnp 'sport = :22 or sport = :2222'State Recv-Q Send-Q Local Address:Port Peer Address:Port ProcessLISTEN 0 4096 0.0.0.0:2222 0.0.0.0:* users:(("systemd",pid=1,fd=79))LISTEN 0 4096 [::]:2222 [::]:* users:(("systemd",pid=1,fd=80))Only 2222 is left. From now on connect with ssh -p 2222. If ufw allowed SSH on port 22 (OpenSSH), that rule can go once nothing listens there; see the ufw guide.
Does a different SSH port make the server safer?
It cuts the noise: most automated login attempts go to port 22, so the logs get quieter. It does not stop anyone who scans every port. Key-only login and fail2ban are what keep a server safe; a new port is a small extra on top. See SSH key login and fail2ban.
Troubleshooting
- The port did not change after
systemctl restart ssh: the socket owns the port. Runsudo systemctl daemon-reload && sudo systemctl restart ssh.socket. Details. - “Changing the SSH port needs a reboot”: it does not; a reboot only reruns the generator, which
daemon-reloaddoes too. Details. - The new login hangs: a firewall drops the port, on the server (
ufw) or at the provider. Details. sshd -tprints an error: the drop-in has a typo; it names the file and line. Fix it before step 4. Details.
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): one new file, /etc/ssh/sshd_config.d/10-port.conf, the ufw rule files, and port 2222 listening. No package was added.
Ubuntu 26.04 vs 24.04
The same check ran on a fresh 26.04 server and on a fresh 24.04 server, each with its own run log. Every command and result was the same, including the generator’s file. One output had an extra line:
| What | Ubuntu 26.04 | Ubuntu 24.04 |
|---|---|---|
| System | Ubuntu 26.04.1 LTS7.0.0-43-generic | Ubuntu 24.04.5 LTS6.8.0-147-generic |
| openssh-server | 1:10.2p1-2ubuntu3.8 | 1:9.6p1-3ubuntu13.20 |
| openssh-client | 1:10.2p1-2ubuntu3.8 | 1:9.6p1-3ubuntu13.20 |
| systemd | 259.9-0ubuntu0.1 | 255.4-1ubuntu8.18 |
| ufw | 0.36.2-9build1 | 0.36.2-6 |
systemctl list-sockets | grep ssh | ssh.socket on 22, plus sshd-unix-local.socket (a local socket, not a network port) | ssh.socket on 22 only |
| Proof | Run log, 14 checks passed | Run log, 14 checks passed |
