The default SSH port on centOS 7 (32-bits / 64-bits) server is 22. But for security reasons, it is recommended to change the default SSH port number. Here is how you can change your centOS 7 server / VPS SSH port number.
Login to your server as root via SSH.
Backup the configuration file before editing it.
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.backup
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 #
Like if we want to make 2121 as new ssh port number.
Save the change using: Ctrl + O
Exit nano editor using: Ctrl + X
Allow the new port in firewall. If you use any third party firewall like csf firewall, add the new port in allowed port list.
If you are using default OS firewall, use following command to allow new port.
Below command will allow port 2121.
firewall-cmd --add-port 2121/tcp --permanent firewall-cmd --add-port 2121/tcp
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 you have done something wrong. You should consider reverting all changes made in step 3.