How to Change SSH Port in Linux.

On Linux, changing the SSH port is a simple process; changing the default SSH port is necessary to stop many automated attacks, and it is a bit harder to guess which SSH port is accessible on the server.

Please follow the steps below on how to change the SSH Port in the Linux

1. Log into your server as the root user.

root@testserver:~#

2. Open the SSHD configuration; the SSH server configuration file is located at /etc/ssh/sshd_config. Open the file using your preferred text editor. For example, for nano editor users, run the following command:

root@testserver:~# nano /etc/ssh/sshd_config

The default port here is 22; you can change it to your preferred port, Here I am using 2124:

Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

The final results will be as shown below;


Port 2124
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

3. After making the necessary changes. , quit and save the sshd_conf file. Now, all you have to do to restart the SSH server is run the command below. You must use your new port, in this case, 2124, the following time you want to log in via SSH:

root@testserver:~# systemctl restart sshd

4. We’ll use the netstat command in Linux to verify what port our SSH server is listening for connections on. Run the following command on your system.

root@testserver:~# netstat -tlp | grep ssh
tcp        0      0 0.0.0.0:2124            0.0.0.0:*               LISTEN      762/sshd: /usr/sbin
tcp6       0      0 [::]:2124               [::]:*                  LISTEN    762/sshd: /usr/sbin

5. Configure firewall: Run the following command to open the new SSH port if you are using UFW, the firewall setup tool that Ubuntu by default ships with:

root@testserver:~# ufw allow 2124/tcp

Congratulations, you have successfully changed your SSH port.

Was this article helpful?

Related Articles

Leave A Comment?

This site uses Akismet to reduce spam. Learn how your comment data is processed.