How to change the SSH port on your Server

Endrit Qerreti

Endrit Qerreti

In this tutorial you will learn how to change the default port 22 of SSH to another port.

Step 1 - Open the sshd configuration file

You can use any text editor you already have installed on your server, for example I'm using vim, type the following command to open the sshd config file via vim

sudo vi /etc/ssh/sshd_config

If you are not familiar with vim, you can also use another text editor, such as : nano for example, to edit the sshd configuration file via nano run the command below

sudo nano /etc/ssh/sshd_config

Step 2 - Change SSH Port

Once you have opened the ssh config file, you need to look up for the entry #port 22 and replace the port 22 with any port you want, the allowed port ranges is 1024 and 65536

For example, if you want to set 2222 as SSH port then simply replace the 22 with 2222, so the entry line should look like this

port 2222

Next, uncomment the line #port 22 and save the configuration file.

If you are using nano to edit the configuration file press CTRL + X and confirm with Yes to save the file

If you are using vim as text editor, then you need to press :w to save the file

Note: Make sure that the port you want to use it's not being used by another app/process, to view the list of the ports run the command below

cat /etc/services

Step 3 - Restart SSH service

Once you have changed the default port, now simply restart the sshd service.


service ssh restart

CentOS 7

systemctl restart sshd

Connect to your server using the new port

Connecting to your ssh server after changing the ssh port is very easy, all you need to use is the -p command to specify the port that you set on the file when you type ssh root@ip

ssh root@ip -p 2222

2222 is the ssh port in this case


By now you should know how to change the ssh port on your server.