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.
Login to your server as root via SSH.
Open following file in any text editor like nano.
Find following code in /etc/ssh/sshd_config file.
Change this line to new port number and remove #
Save the change using: Ctrl + O
Exit nano editor using: Ctrl + X
Restart SSH using below command.
service sshd restart
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.