SSH to Port Other Than 22: How to Do It (with Examples)

How do I SSH on a port other than 22? Here's the answer.

SSH to Port Other Than 22

In this guide, we will explore how to change the SSH server port. In addition to, we will show you how to use the ssh command to connect to 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.

The -p <port> option can be used to specify the port number to connect to when using the ssh command on Linux.

For example, if you want to access remote server which listening on port 2222, then just type:

ssh -p 2222 [email protected]

How to Change the SSH Server Port

Although having a strong and secure password is the only cybersecurity step the majority of 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 port number of the server according to your requirements. In order 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:

#Port 22

To be able to use ssh to port other than 22 on your server, you need to change specific parameters within the sshd_config file. You need to uncomment the line by removing the number-sign (#) and change 22 to the new port number that you want to use. Save the changes and then exit the text editor.

That’s all. Now, just restart the SSH service.

systemctl restart sshd

Changing the port will stops automatic attacks against your SSH server and will make more unlikely to be hit it with a random scan. In other words, now is more difficult to find your server and you avoid being seen by random scans.


In this guide, we showed you how to use the ssh command to connect to 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 SSH port became 22.

1 Comment

Leave a Reply

Your email address will not be published.

Latest from Linux Knowledge