Change SSH Port on Ubuntu 20.04 Linux System - Best Method ?

By default, SSH protocol is known to listen to port 22. While it's perfectly OK, in a security sense, it's recommended to change the default port to another port. This greatly minimizes the risks of hackers launching automated brute-force attacks using SSH.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related System SSH queries.

In this context, we shall look into how you can change the default SSH port to something else on Ubuntu Linux System.


How to Change SSH Port on Ubuntu OS ?

To begin, Ensure that you also have an SSH client ( such as Putty ) or even a client Linux system to set the connection to the server using the new port. Then with a sudo privileged user follow the steps below.


1. Check if SSH is running on the default port

Firstly, we need to verify that the SSH service is running on the server. To accomplish this, run the following command:

$ sudo systemctl status ssh

The output is a confirmation that the SSH service is active. Let's even go further and ensure that it is running on the default port. Run the following netstat command:

$ sudo netstat -pnltu | grep 22


2. Select a port and configure SSH

As you might know, TCP ports range from port 0 – 65535. However, the ports from 0 – 1024 are regarded as privileged ports designated for special services such as HTTP ( Port 80 ), FTP ( 21 ), and SMTP ( 25 ) to mention just a few.

To avoid any conflict with privileged ports, we are going to pick a port that’s above port 1024. In this example, we have chosen to change the default port to port 5466. To enable this, we will modify the /etc/ssh/sshd_config SSH configuration file:

$ sudo vim /etc/ssh/sshd_config

Locate the line that starts with #Port 22.

Then, Uncomment and specify the port number.

Thereafter, save the changes made and exit the configuration file. For the changes to persist, restart the SSH service:

$ sudo systemctl restart ssh

To verify that SSH is listening to the newly configured port, run:

$ sudo netstat -pnltu | grep 5466


3. Allow the new SSH port on the UFW firewall

If the UFW firewall is enabled, be sure to open the configured port as shown below:

$ sudo ufw allow 5466/tcp
$ sudo ufw reload

For CentOS / RHEL, open the port as follows:

$ sudo firewall-cmd --add-port=5466/tcp --add-zone=public --permanent
$ sudo firewall-cmd --reload

At this point, you can remotely access the server.


4. Logging in using the new port

To log in to the server, head over to the SSH client. Specify the port number using the -p option followed by the SSH port and the remote server identity:

$ ssh -p 5466 linuxapt@192.168.2.101

As expected, you will be prompted to provide the password after which you will be granted access to the remote server.


[Need assistance in fixing Missing Software Installation packages on Debian System? We can help you. ]

This article covers how to change the ssh port on Ubuntu Linux server. You can easily change the SSH Port for Your Linux server. For better security, consider using SSH passwordless authentication with SSH public/private key pair.

The ssh port defined in sshd_config file. This file located in /etc/ssh/sshd_config location.

To open the new port run the following commands on Fedora/CentOS/RHEL/Oracle Linux using FirewallD:

$ sudo firewall-cmd --permanent --zone=public --add-port=2222/tcp
$ sudo firewall-cmd --reload


How to Restart the sshd service ?

Type the following command on a CentOS/RHEL/Fedora Linux:

$ sudo service sshd restart

OR if you are using CentOS/RHEL/Fedora Linux with systemd:

$ sudo systemctl restart sshd

OR if you are using Ubuntu/Debian/Mint Linux:

$ sudo service ssh restart

OR if you are using Ubuntu/Debian/Mint Linux with systemd:

$ sudo systemctl restart ssh

Or if you are using FreeBSD Unix, enter:

$ sudo service sshd restart

Related Posts