How do I change my VPS's SSH Port

Jim K.

Last Update 3 months ago

Affected Operating System (OS)
1) Almalinux 8, 9
2) Unbuntu 22, 24

Introduction

Port 22 is a default SSH port and you will see that there is a-lot of unauthorised brute force attempts to access your VPS.


It is highly recommended to change the SSH port at the least.

Procedure

You can change your VPS’s default SSH port starting by editing its SSH configuration file.


To do this, open the /etc/ssh/sshd_config file in your favourite text editor:


nano /etc/ssh/sshd_config

or


vi /etc/ssh/sshd_config

Look for the line with #Port 22 and remove the comment hash (#) , followed by changing the default port 22 to a port value you want.


For example, change the line to Port 2120 without the (#), this will make your VPS to be accessible only via SSH on port 2120.

to

After save and quit the editor, you will need to update your VPS firewall using the below steps to allow access to the SSH via the new port.

Important. Do note restart your server or ssh service at this point of time

For AlmaLinux OS


For AlmaLinux, replace the port <Port> in the below command and then run the whole command in SSH to unblock your new SSH port at your AlmaLinux firewall.


semanage port -a -t ssh_port_t -p tcp <port>

For example if we want to open port 2120 for SSH, run
semanage port -a -t ssh_port_t -p tcp 2120


After open the firewall port, run the following command for restarting the ssd service to take effect the new port.


sudo systemctl restart sshd.service

For Ubuntu OS


For Ubuntu, replace the port <Port> in the below command and then
run the whole command in SSH to unblock your new SSH port at your Ubuntu firewall.


sudo ufw allow <Port>

After open the firewall port, run the following command for restarting the ssd service to take effect the new port.


sudo systemctl restart sshd.service

Final Step...

With the restarting of SSHD above, your new port can now be used to access SSH.
To test the new port access, you can exit and login to SSH using the following command.


ssh <Server IP/Hostname> -l root -p <new ssh port>

In event that the change fail and you were log out of your VPS's SSH, feel free to contact our friendly support, we will assist you to restore SSH access.

Was this article helpful?

0 out of 0 liked this article

Still need help? Message Us