Why should you change default SSH port in Linux?
The main reason to change the default SSH Port from 22 to something else is to prevent server from SSH attacks. Port 22 is default SSH port and that is the first thing it would be tried. Changing default port will prevent your server from brute-force and malicious attacks.
How to change SSH port in Linux
To change SSH port you will need to have root access to the system. The first Step would be choosing the port. You should always avoid ports from 0-1023 as they are system default ports.
Open file /etc/ssh/sshd_config
[root@server ~]# vi /etc/ssh/sshd_config
Looks for the line below
Now remove comment and change the port, so the line should look something like this.
Now you will need to restart sshd service so the changes would be reflected.
[root@server ~]# systemctl restart sshd
Important Note: Before you change port please make sure you have whitelisted the new port in the firewall, else you will be locking your self out.
Testing the Changes
Well, the golden rule is to verify all the changes you do and make sure it works. So before logging out the current session, you will have to make sure that you are able to access the server using new port.
[root@server ~]# ssh -p 5867 username@your-server-ip
Done, you have successfully updated SSH port. Please do always remember to allow new port in firewall and to test new port before closing the current session – so that you don’t lock out.