This page explains how to change the SSH (Secure Shell) port from the default of 22. Using port 22 doesn’t make your system insecure, nor will changing the port from 22 provide any significant variance in security. However, changing the default SSH port will help stop many automated attacks by making it a bit harder to guess which port the secure shell (SSH) is accessible from. In other words, security though obscurity.
1. Connect to your server via SSH
Connect to your server using an SSH client such as PuTTY (for Windows) or OpenSSH for Mac/Linux
2. Edit the /etc/ssh/sshd_config file using root permissions
To edit the file using ‘nano’:
1 2 3 |
sudo nano /etc/ssh/sshd_config |
3. Locate the line that looks like one of the following:
1 2 3 |
Port 22 |
1 2 3 |
# Port 22 |
4. Edit the line to remove the # and change 22 to the SSH port number you’d like to use.
For example:
1 2 3 |
Port 59522 |
Save and exit. If using nano, press Control-X, then Y, then hit Enter.
5. Restart the sshd service on your server
Type the following command:
1 2 3 |
sudo service ssh restart |
You should see something like this:
1 2 3 4 |
ssh stop/waiting ssh start/running, process 21223 |
6. Ensure that you’re able to connect using the new port.
Before disconnecting, we recommend verifying that you’re able to connect using the new port. If you can’t, try repeating the above steps with another port or switch back to 22.
Use your your preferred SSH client (see Step #1) or type the following from your existing shell, replacing 59522 with the port number chosen in Step #4:
1 2 3 |
ssh username@example.com -p 31522 |