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.
Step 1:
Login to your server as root via SSH.
Step 2:
Backup the configuration file before editing it.
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.backup
Step 3:
Open following file in any text editor like nano.
nano /etc/ssh/sshd_config
Step 4:
Find following code in /etc/ssh/sshd_config file.
#Port 22
Change this line to new port number and remove #
Like if we want to make 2121 as new ssh port number.
Port 2121
Save the change using: Ctrl + O
Exit nano editor using: Ctrl + X
Step 5:
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
Step 6:
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.