How to change the SSH port?
Changing the SSH port is security advice that you may have heard of. Changing the default SSH port(22) is a basic step you can take to the road to security.
You will need to sign in your server using SSH. Open your favorite SSH client or type this command in your terminal:
your_server_ip should be replaced with your server IP address.
Changing the SSH port
Once you are logged in to your server, we will need to change the
/etc/ssh/sshd_config file. We recommend making a backup of this file before editing it. Run this command to make a backup of the file:
cp /etc/ssh/sshd_config /etc/ssh/sshd_config_
Open this file with your favorite editor. For this tutorial, we will use vim. Use this command to open the editor:
You will see a screen like this:
You probably see the line where we can specify the SSH port:
If this line is commented(with # before Port 22), please remove the #. If you want to edit the file, press
i. Now you are in editing mode. Make your changes - change the SSH port. You can change it to something else but be careful - do not set port which is already in use. For example, you can set it to 22002. Now press the
[ESC] button and type
:x to save and exit vim.
Restart the SSH service
In order for our changes to apply, we need to restart the sshd service. You can do this with this command:
service sshd restart
NOTICE: Please check if your server has a firewall installed. If yes, check the rules and add your new SSH port to the firewall configuration. Otherwise, you will not connect to your server via SSH again.