Bots try to log in on port 22 of every server, all day. Moving SSH to another port does not make the server secure by itself, but it keeps most of this noise out of your logs. On Ubuntu 24.04 and newer, SSH is started by ssh.socket, and changing the port needs one more command than in older guides. The steps were tested on Ubuntu 26.04 LTS.

⚠ Do not lock yourself out

Keep port 22 open until you have logged in on the new port, and keep your current SSH window open while you work. If you do get locked out, the console in the Client Area or the rescue system gets you back in.


1. Add the New Port

Choose a port between 1024 and 65535 that no other program uses, for example 2222. Create the file /etc/ssh/sshd_config.d/05-port.conf with both ports for now:

Port 22
Port 2222

Check the configuration; it prints nothing when it is fine:

sshd -t

2. Open the Port in the Firewall

If you use ufw (Protect Your Linux Server With a Firewall and Fail2ban), allow the new port before you switch:

ufw allow 2222/tcp

With Fail2ban, tell its SSH jail about the port: in /etc/fail2ban/jail.local, under [sshd], set port = 2222 and run systemctl restart fail2ban.

3. Apply It

systemctl daemon-reload
systemctl restart ssh.socket

Both lines are needed. daemon-reload reads the ports from the SSH configuration into ssh.socket; systemctl restart ssh.socket alone keeps SSH on port 22 only. Check that SSH now listens on both ports:

ss -ltn | grep -E ':22 |:2222 '
ℹ On Debian 13

Debian starts SSH as a normal service, not through ssh.socket. Instead of the two commands, run systemctl restart ssh, here and in step 5. SSH then listens on both ports.

4. Log In on the New Port

In a new window on your computer, keeping the old one open:

ssh -p 2222 root@your-server-IP

In PuTTY, enter the port in the Port field; in FileZilla and WinSCP, in their port field. At the first login on the new port, your SSH program may ask again whether you trust the server: the server is the same, answer yes.

5. Close Port 22

When the login on the new port works, remove Port 22 from /etc/ssh/sshd_config.d/05-port.conf, so that only Port 2222 is left, and apply it again:

systemctl daemon-reload
systemctl restart ssh.socket

With ufw, close the old port: ufw delete allow OpenSSH (or ufw delete allow 22/tcp, as you added it).

To go back to port 22, delete the file 05-port.conf and run the two commands of step 3 again; SSH then listens on port 22 only.


Related Guides

View all Ubuntu and Debian guides

Was this answer helpful? 0 Users Found This Useful (0 Votes)