
The rule for changing the SSH port is a single one: open the new port before you close the old one, and test in a second connection before closing the first. You edit /etc/ssh/sshd_config, add a Port 2222 line (an example), open that port in the firewall, restart SSH and connect through it. Only when it works do you remove port 22. Changing the port is not real security: it clears the noise of automatic probes that only look at 22 and keeps the logs readable. Security comes from keys (creating an SSH key and keeping your VPS secure).
Step by step
|
|
|
|
|
|
|
|
What changes afterwards
| Tool | How to give the port |
| ssh | ssh -p 2222 user@IP |
| scp | scp -P 2222 file user@IP:/destination (capital P) |
| sftp and graphical clients | sftp -P 2222 ...; in a graphical client, the “Port” field |
| rsync | rsync -e "ssh -p 2222" ... (copying with rsync) |
| Tunnels and automatic backups | The port must be updated in every script and configuration file that connects to the server. |
| On recent Ubuntu releases, SSH may be started by a socket. In that case the port the system listens on can come from the ssh.socket unit and not only from sshd_config. That is why the ss -tlnp confirmation step is mandatory: if the new port does not show up, look at systemctl status ssh.socket and Ubuntu’s documentation for your release, and only then close the old one. Depending on your system, the mechanics can vary. |
| Never close the session where you made the change until you have tested in another. It is the most common cause of being locked out. If it happens, the panel console gets in without the network and undoes it: I have lost SSH access to my server. |
| Other services need to know the new port too. If you run fail2ban (installing fail2ban), tell it the new port, or it stops protecting SSH. And if you run SSH on another port because of networks that only let web traffic through, that is a different question: ports and firewall. |
|
Locked yourself out after touching SSH? Tell us the service name and what you did, and we will give you the console. Open a support ticket |
|
SEE ALSO |
RECOMMENDED PRODUCT VPS server with root access Resources of your own, the OS you choose, reinstall whenever you like. from $8.40/mo (3-year plan, with coupon) See plans |
- 0 Users Found This Useful











