This guide will explore how to change the SSH server port. In addition, we will show you how to use the
ssh command to connect to a port other than the default 22.
By default, the SSH server runs on port 22. However, there are occasions when it is run in a different port.
-p <port> option can specify the port number to connect to when using the
ssh command on Linux.
For example, if you want to access a remote server that listens on port 2222, then just type:
ssh -p 2222 user@host
How to Change the SSH Server Port
Although having a strong and secure password is the only cybersecurity step most remote server users take, additional layers of security are always recommended.
A simple but effective step you can take to enhance your server’s security is to change the port SSH uses to another than 22. However, you can set the server’s port number according to your requirements.
To achieve it, follow these simple steps:
Log in to the server as root and open the
/etc/ssh/sshd_config file with your preferred text editor. Locate the following line:
ssh to port other than 22 on your server, you need to change specific parameters within the
First, you need to uncomment the line by removing the number-sign (
#) and changing
22 to the new port number that you want to use. Next, save the changes and then exit the text editor.
That’s all. Now, restart the SSH service.
systemctl restart sshd
Changing the port will stop automatic attacks against your SSH server and make it more unlikely to be hit with a random scan. In other words, now it is more difficult to find your server, and you avoid being seen by random scans.
This guide showed you how to use the ssh command to connect to a port other than the default 22.
We have also discussed how to change the default SSH port on your Linux machine to safeguard your server.
Remember to use the new private port range to keep your server stable and avoid disrupting running services.
If you’re curious, here is the story behind how the SSH port became 22.