ubuntu 20 change ssh port

admin3 April 2024Last Update :

Mastering Security: How to Change the SSH Port in Ubuntu 20.04

ubuntu 20 change ssh port

Welcome to a comprehensive guide on enhancing your server’s security by changing the default SSH port in Ubuntu 20.04. As cyber threats evolve, it’s crucial for system administrators and tech enthusiasts alike to fortify their defenses against unauthorized access. One effective measure is altering the SSH port from its default setting. This article will delve into the reasons behind this change, provide a step-by-step tutorial, and address common questions related to the process.

Understanding the Importance of Changing the SSH Port

Secure Shell (SSH) is an encrypted protocol used to manage servers remotely. By default, SSH listens on port 22, which is well-known and often targeted by automated attacks. Changing the SSH port can reduce the noise from these attacks, making real threats easier to spot and adding an extra layer of obscurity to your server’s security measures.

The Benefits of Altering Your SSH Port

  • Reduced attack surface: Moving away from the default port can help shield your server from automated scans and brute-force attempts.
  • Better resource allocation: Fewer malicious login attempts mean less processing power wasted on handling them.
  • Enhanced monitoring: With fewer false alarms, genuine threats are more conspicuous in logs and monitoring tools.

Step-by-Step Guide to Changing the SSH Port in Ubuntu 20.04

Before proceeding with the steps below, ensure you have administrative privileges on the Ubuntu 20.04 system you’re working with. It’s also wise to backup your current SSH configuration as a precaution.

Step 1: Selecting a New SSH Port

Choose a new port number between 1024 and 65535 to avoid conflicts with other standard services. Ensure that the new port is not already in use by another service on your system.

Step 2: Configuring the Firewall

Adjust your firewall settings to allow traffic on the new SSH port. For systems using UFW (Uncomplicated Firewall), the command would be:

sudo ufw allow [new_port]/tcp

Step 3: Modifying the SSH Configuration File

Edit the /etc/ssh/sshd_config file with your preferred text editor, such as nano or vim, and locate the line that specifies the port number:

sudo nano /etc/ssh/sshd_config

Find the following line:

#Port 22

Remove the comment symbol (#) and change “22” to your chosen port number:

Port [new_port]

Step 4: Restarting the SSH Service

Apply the changes by restarting the SSH service:

sudo systemctl restart sshd

Step 5: Testing the New Configuration

Without closing your current SSH session, open a new one to test connecting to the server using the new port:

ssh -p [new_port] user@your_server_ip

If the connection is successful, the port change has been implemented correctly.

Best Practices and Considerations

While changing the SSH port can improve security, it should be part of a broader security strategy. Employ additional measures such as key-based authentication, fail2ban installation, and regular updates.

Choosing a Secure Port Number

Select a port that isn’t commonly used by other applications to minimize potential conflicts and maintain service stability.

Updating Existing Scripts and Services

Remember to update any scripts, cron jobs, or monitoring tools that rely on the default SSH port to reflect the new configuration.

Frequently Asked Questions

Will changing the SSH port affect performance?

No, changing the SSH port does not impact the performance of SSH or the server itself. It merely helps in reducing unwanted traffic.

Is changing the SSH port enough to secure my server?

While it’s a helpful step, relying solely on changing the SSH port is not sufficient. Implement multiple layers of security for robust protection.

Can I use any port number?

You should choose a non-standard port above 1024 and below 65535 that is not already in use by another service.


Altering the SSH port on your Ubuntu 20.04 server is a simple yet effective way to enhance security. By following the steps outlined in this guide, you can achieve an additional layer of defense against automated attacks. Remember, this is just one aspect of a comprehensive security plan, and ongoing vigilance is essential in maintaining a secure server environment.


Leave a Comment

Your email address will not be published. Required fields are marked *

Comments Rules :

Breaking News