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

This guide will explore how to change the SSH server port, and we will show you how 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.

The -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@hostCode language: CSS (css)

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:

#Port 22Code language: CSS (css)

To use ssh to port other than 22 on your server, you need to change specific parameters within the sshd_config file.

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.

Conclusion

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.

Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 69%