This post is about how to change the default SSH port in Linux, however, before we how we need to see why.
Why should you change the default SSH port in Linux?
The main reason to change the default SSH port from 22 to something else is to prevent servers from SSH attacks. Port 22 is the default SSH port and that is the first thing it would be tried. Changing the default port will prevent your server from brute-force and malicious attacks.
How to change SSH port in Linux?
To change the 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.
$ vi /etc/ssh/sshd_config
Look for the line below
Now remove the comment and change the port, so the line should look something like this.
Whenever you make any changes in the file
sshd_config, then you should always restart the SSHD service, else your changes won’t be published.
$ 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 yourself 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 of the current session, you will have to make sure that you are able to access the server using new port.
$ ssh -p 5867 username@your-server-ip
Done, you have successfully updated the SSH port. Please do always remember to allow a new port in the firewall and to test the new port before closing the current session – so that you don’t lock out. Also, we need to make sure that the current ssh port 22 is blocked.
Also, If you have liked this page, then you should like to read more about security.