How to change SSH port of a Linux server

The default port of SSH on a Linux server is 22. But for security reasons, it is recommended to change the default SSH port number. Here is how you can change your Linux server / VPS SSH port number.

Step 1:

Login to your server as root via SSH.

Step 2:

Open following file in any text editor like nano.

nano /etc/ssh/sshd_config

Step 3:

Find following code in /etc/ssh/sshd_config file.

#Port 22

Change this line to new port number and remove #

Port 2121

Save the change using: Ctrl + O
Exit nano editor using: Ctrl + X

Step 4:

Restart SSH using below command.

service sshd restart

Step 5:

Don’t close your current SSH session. Instead start a new SSH session in a new widows with new SSH port number.
If you are able to login using new SSH port number, you have successfully changed your server SSH port number.

If you are unable to login using new SSH number, it means new port number is not open for incoming traffic in firewall. Run below command to allow SSH traffic via new port number.

iptables -A INPUT -p tcp -m tcp --dport 2121 -j ACCEPT

In above code change 2121 to your actual new port number. Now try login using new port number.

Our Recommended VPS Hosting

Leave us a Reply

Your email address will not be published. Required fields are marked *

* *