How do I change my VPS's SSH port? Print

  • 0

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.

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 a 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 to allow access to the SSH via the new port.
Important Note. Do note restart your server or ssh service at this point of time.

AlmaLinux

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

Ubuntu


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>

 

 

 


Was this answer helpful?

« Back