This example is based on an Ubuntu distribution.
-
Log into the system
Log into the system with an account that has administrator privileges.
-
Open the SSH configuration file
Open the SSH configuration file using a text editor. This file is usually located at
/etc/ssh/sshd_config. You can usenano,vi, or any other text editor of your choice.bash
-
sudo nano /etc/ssh/sshd_config -
Find the Port directive
Find the line that starts with "Port". This specifies the port used for SSH. It is usually 22.
-
Port 22 -
Change the port
Change the port value to the one you want to use. It is generally recommended to choose ports within the range of 1024-49151 that are not used by other services.
For example:
-
Port 2222 -
Save and close the file
Save the changes by pressing
Ctrl + X, thenY, and thenEnter(for the nano editor). If you are using a different text editor, follow its instructions for saving changes. -
Restart the SSH service
After making changes, restart the SSH service to apply the new settings.
bash
sudo service ssh restart
or
bash
-
sudo systemctl restart ssh
Now SSH will be listening on your new port. Remember the new port to be able to connect to the server. After changing the port, you will need to specify it explicitly when connecting:
bash ssh -p 2222 user@your_ip_address
Do not forget to configure the appropriate firewall rules, if used, to allow access to the new port.