How to Change the SSH Port in Ubuntu Linux
Move SSH off port 22 on Ubuntu without locking yourself out: firewall first, the extra step on Ubuntu 22.10 and later, and a test before you close.
3 min readBy Soumya
Reviewed by the AmazingRDP technical team

A server to secure your way. A Linux VPS gives you root access. See Linux VPS from $6.99
Every server on the internet gets a steady stream of login attempts on port 22 from automated scanners. Moving SSH to another port makes most of them miss. It is a small change with one real danger: doing the steps in the wrong order cuts you off from your own server. The order below avoids that.
What it does and does not do
- It does cut the noise in your logs and the load from bots that only try port 22.
- It does not protect against anyone who scans all ports, which takes seconds. Real protection is key login with passwords switched off. Treat the port change as tidiness added on top.
1. Choose a port
Pick a number between 1024 and 65535 that nothing else uses. This guide uses 2222. Check that it is free:
sudo ss -tln | grep 2222
No output means free.
2. Open the new port in the firewall first
sudo ufw allow 2222/tcp
sudo ufw status
If your provider has a firewall in front of the server (in a control panel), open the port there as well. Skip this step and the next ones will lock you out.
3. Change the setting
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
sudo nano /etc/ssh/sshd_config
Find the line #Port 22, remove the #, and change the number:
Port 2222
A careful variant is to list both for the moment, Port 22 and Port 2222 on separate lines, and remove 22 at the end. Check the file for mistakes:
sudo sshd -t
No output means the file is valid.
4. Apply it: this differs by Ubuntu version
Ubuntu 22.10 and later (including 24.04 and 26.04) start SSH through a systemd "socket", which holds the port. Restarting the service alone does not move it. Run:
sudo systemctl daemon-reload
sudo systemctl restart ssh.socket
Ubuntu 22.04 and earlier:
sudo systemctl restart ssh
Not sure which you have? Run both sets; the extra commands do no harm. Then confirm what SSH listens on:
sudo ss -tlnp | grep -E "ssh|:2222"
5. Test before you close anything
Keep your current session open. Open a second terminal on your own computer and connect on the new port:
ssh -p 2222 user@203.0.113.10
Only when that works, go on. If it fails, you still have the first session to undo the change with the backup file.
6. Close port 22
If you listed both ports, remove the Port 22 line and apply as in step 4. Then:
sudo ufw delete allow 22/tcp
sudo ufw delete allow OpenSSH
sudo ufw status
One of the two delete lines will match how the rule was created; the other reports that there is no such rule.
Make connecting easy again
On your own computer, add the server to the file ~/.ssh/config:
Host myserver
HostName 203.0.113.10
User user
Port 2222
Now ssh myserver is enough, and scp, rsync and file programs that read this file follow. In WinSCP or FileZilla, change the port field of the saved site. Tools that connect by themselves (backups, monitoring, git remotes on this server) need the new port too.
If you are locked out
- Open the server's console in the provider's control panel. It works without SSH.
- Log in and look:
sudo ss -tlnp | grep sshshows the port in use;sudo ufw statusshows what the firewall allows. - Open the right port, or restore the backup:
sudo cp /etc/ssh/sshd_config.bak /etc/ssh/sshd_configand apply as in step 4.
Problems
| Sign | Cause |
|---|---|
| SSH still answers on 22 after the change | Ubuntu 22.10 or later and the socket was not restarted (step 4), or a file in /etc/ssh/sshd_config.d/ sets the port again |
| Connection refused on the new port | SSH is not listening there; see the line above |
| Connection timed out on the new port | A firewall drops it: ufw or the provider's |
sshd -t reports an error | A typing mistake in the file; the message gives the line |
What to do next
- Switch to key login and set
PasswordAuthentication no: see SSH keys on Windows or on a Mac. - Install
fail2banto block addresses that keep guessing. - Allow the port for your own addresses only, if they are fixed: see the Ubuntu firewall guide.
On the Red Hat family the firewall part is done with firewalld, and SELinux must be told about the new port: see configuring firewalld. The Windows counterpart is connecting to RDP on a different port. For a server of your own, see our Linux VPS plans.
Questions
How do I change the SSH port in Ubuntu?
Open the new port in the firewall, set Port in /etc/ssh/sshd_config, apply it, and test in a second session before closing the first.
Why does SSH still use port 22 after I changed it?
On Ubuntu 22.10 and later SSH is started by a systemd socket. Run sudo systemctl daemon-reload and sudo systemctl restart ssh.socket.
Does changing the SSH port make the server secure?
It reduces automated login attempts. Real security comes from key login with passwords switched off.
How do I connect to SSH on a different port?
Use ssh -p PORT user@server, or set the port in ~/.ssh/config.