Signup/Sign In

How to Change the SSH Port in Linux


This article discusses how to modify the SSH port number for a server. You may wish to change this for security reasons, for example, or if you utilize a third-party service that needs a certain SSH port number.

Changing the SSH port number

1. To change the port for the SSH server, perform these steps:

  1. Log in to the server as root using SSH.
  2. Open the /etc/ssh/sshd config file in your favourite text editor (nano, vi, etc). (nano, vi, etc.).
  3. Locate the following line: Port 7822

2. Change 7822 to the new port number that you wish to use.

3. Save the modifications to the /etc/ssh/sshd config file, and then quit the text editor.

4. Restart the SSH service using the relevant command for your Linux distribution:

5. For CentOS and Fedora, type:

Copyservice sshd restart

6. For Debian and Ubuntu, type:

Copyservice ssh restart

While still logged in as root, in a new terminal window attempt to log in using the new SSH port number. If the login fails, check your settings. Do not quit your open root session until you are able to log in with the updated settings.

About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.