Most servers will retain the default SSH port number as 22. However, in some cases, this may be changed by administrators as a security enhancement. If you wish to change the SSH port to an alternate number, one way of doing so is by using OpenSSH.
How to Change the Default SSH Port in OpenSSH
The process involves locating the server SSH configuration file and amending the port number within that file.
- Step 1. Connect to your server as a root user and open the SSH configuration file for editing. The command you use will vary depending on the text editor used. For this example, we’ll be using Vi. Enter the following command:
$ vi /etc/ssh/sshd_config
- Step 2. Search for the line which specifies your SSH port number, then change the port to a preferred number and remove the # to uncomment the line. Bear in mind that the new port must be opened by the server’s firewall. If it isn’t, connecting to the server via SSH would be impossible. Remember to save the file before exiting.
- Step 3. Once the port number has been changed the SSH service needs to be restarted for those changes to take effect. To restart the service:
For CentOS/Fedora: “service sshd restart”
For Debian/Ubuntu: “service ssh restart”
A message should appear indicating that the SSH service was stopped and restarted correctly.
- Step 4. Verify that the port number change has taken effect by entering the following command:
ps aux | grep sshd
You should see a message indicating the SSH status as well as the new port number for which it is listening to connections.