How to change your SSH port

DatacenterThe following commands should work in most Linux distributions. You need to first connect to your Linux server and either use sudo or become the root user to execute these commands.

Locate and edit the configuration file

First, you need to edit the SSH configuration file. You can use nano or your preferred editor to edit the configuration file.

nano /etc/ssh/sshd_config

Change the port for SSH

Second, you can locate the following line near the top part of the file. You will need to edit the number to the port you want the SSH daemon to listen on.

# What ports, IPs and protocols we listen for
Port 22

Make sure to restart SSH

Third, after you have edited the file and saved it. You can restart the SSH daemon using one of the following commands:

CentOS/Fedora:

service sshd restart

Debian:

service ssh restart

Try logging in using the new port

After the SSH daemon restarts you should logout and login using the specified port you put in the SSH configuration file.

Example of SSH login with a custom port and not using the regular/default port. The following command will have your computer connect to the server located at the IP address 127.0.0.1 using port 5000 on the remote server.

ssh 127.0.0.1 -p 5000