How to Change SSH Port in Ubuntu

Posted on June 24th, 2019

SSH stands for Secure Shell, It is a protocol using which you can connect to your server via terminal or putty. In Linux based operating systems, The default SSH port is 22. But the good news is, we can change SSH port in Ubuntu to avoid getting unwanted login attacks by bots who attack on default ports.

It’s not that we can completely stop any kind of attack just by changing the SSH port, but it provides some level of security. So, let’s get started with the tutorial. In this tutorial, we will update SSH port from 22 to 2020 in one Ubuntu 16.04 server.

But you can select any number for port from 1 to 65535, just make sure there is no other service running on the same port. If you want to verify that no services are running on your desired port, execute the following command.

lsof -i :PORT -S

Do not forget to replace the PORT with your desired port number. After executing the command, if you do not see any output, you can use the port number for SSH service.

Now, let’s get started with the actual tutorial.

Change SSH port in Ubuntu

It is a very easy task to change port for SSH service in Ubuntu. We just have to change the port number in our SSH configuration file. The SSH configuration file is located at /etc/ssh/sshd_config .

To open the file in edit mode, execute the following command. But make sure you have sudo privileges or you are executing command as a root user.

sudo nano /etc/ssh/sshd_config

In the upper part of the file, you will see a Port directive with value 22. Which means, SSH service is listening to the port number 22 right now. Now, we will replace 22 and set the desired port number. In this case, the updated line should look like this.

Port 2020

Once done, press CTRL+X followed by Y followed by Enter to save the file. Now, execute the following command to restart the SSH service. It is required to update configuration and make SSH listen on the new port.

sudo service ssh restart

Note that if you are using any kind of firewall, allow incoming connections on your desired SSH port before disconnecting with the server. Otherwise SSH won’t listen on port number 22 and server won’t accept connections on desired port.

Congratulations! Now you can connect to your server using SSH on non-default port. Now, if you want to connect to your server via SSH on a non-default port, here is the command that you can use.

ssh user@hostname -pPORT

Do not forget to update user, hostname and port while using this command. So, this is how you can Change SSH port in Ubuntu.

4 Responses to “How to Change SSH Port in Ubuntu”

  1. Misha says:

    Thank you this was helpfull to me!!!

  2. Anuj Sharma says:

    not working on oracle cloud

Leave a Reply