Ubuntu Change SSH Port: A Comprehensive Guide


Greetings, dear readers! In a digital world where cybersecurity threats constantly loom, it has become imperative to strengthen your system’s defenses. Ubuntu, the popular open-source operating system, offers a multitude of security features, including the ability to change the default SSH port. This article aims to provide you with a comprehensive guide on how to effectively change the SSH port in Ubuntu and bolster your system’s security. So grab your virtual seat belts and let’s dive into this exciting journey!

The Importance of Changing the SSH Port

🔑 SSH (Secure Shell) is a widely-used protocol for establishing secure remote connections with servers. By default, SSH listens on port 22. However, this default setting makes your system an attractive target for malicious actors. Changing the SSH port adds an extra layer of security and makes it harder for attackers to exploit vulnerabilities. Let’s explore the process of altering the SSH port to safeguard your Ubuntu system.

Step 1: Accessing the Terminal

1. Open the Terminal by pressing Ctrl+Alt+T.

2. Once the Terminal is open, enter the following command to log in as the system administrator:

sudo su

3. Hit Enter and provide your password when prompted.

Step 2: Modifying the SSH Configuration

4. Now, you need to open the SSH configuration file by entering the following command:

vi /etc/ssh/sshd_config

5. Press Enter to open the file using the vi text editor.

6. In the text editor, locate the line that reads #Port 22.

7. Remove the # symbol to uncomment the line.

8. Replace 22 with your desired port number (e.g., 5555).

Step 3: Saving and Applying Changes

9. Save the changes you made to the SSH configuration file by pressing the following keys in sequence:

Shift + : and then w and finally q.

10. Restart the SSH service to apply the modifications by entering:

service ssh restart

Step 4: Verifying the SSH Port Change

11. To ensure the SSH port change was successful, attempt to connect using the new port:

ssh username@your_ubuntu_server -p your_new_port_number

12. If you can successfully establish a connection, congratulations! Your SSH port has been changed.

Advantages and Disadvantages of Changing the SSH Port

Let’s delve into the advantages and disadvantages of changing the SSH port in Ubuntu:


1. Enhanced Security: Changing the default SSH port adds an extra layer of security by obfuscating the service’s location and deterring automated attacks.

2. Reduced Attack Surface: By altering the SSH port, you minimize the risk of being targeted by bots and automated scanning tools, reducing the attack surface.

3. Mitigates Brute Force Attacks: Many attackers rely on brute force techniques to crack SSH passwords. Changing the port makes it harder for them to even find the SSH service.

4. Customization: Changing the SSH port allows you to align your server’s configuration with your organizational security policies, providing a more tailored and controlled environment.

5. Streamlined Log Monitoring: With a non-standard SSH port, it becomes easier to identify and filter relevant log entries, aiding in the detection of potential unauthorized access attempts.

6. Foolproof against Script-Kiddies: Changing the SSH port helps thwart inexperienced attackers who predominantly target systems with default configurations.

7. Community Support: As changing the SSH port is a common practice, extensive support and resources are available from the online Ubuntu community to assist with troubleshooting and maintenance.


1. Inconvenience: If you change the default SSH port, you’ll need to remember the new port number when connecting remotely, which can be cumbersome.

2. Increased Complexity: Changing the default SSH port introduces an additional step during server setup and configuration, potentially complicating the deployment process.

3. Security through Obscurity: While changing the port is an effective security measure, it should not be relied upon as the sole defense mechanism. Other robust security practices should be implemented.

4. Firewall Configuration: Modifying the SSH port may require adjustments to your firewall settings to allow incoming connections on the new port. Proper firewall configuration is essential.

5. Potential Conflicts: Beware of conflicts with other services that may also be using the new port. It is crucial to avoid port collisions for seamless system functionality.

6. Human Error: Misconfiguration or forgetting the new SSH port may lock you out of the system, necessitating physical access or alternative recovery methods.

7. Increased Monitoring: Changing the SSH port requires heightened vigilance in monitoring logs for potential misconfigured or unauthorized access attempts.

Table: Ubuntu Change SSH Port Configuration

Configuration Description
Port The new port number for SSH connections
Protocol Protocol used, typically TCP
PermitRootLogin Specifies if root can log in over SSH
PermitEmptyPasswords Specifies if empty passwords are allowed
PasswordAuthentication Specifies if password authentication is allowed
AuthorizedKeysFile Specifies the file containing authorized keys
UsePAM Specifies if PAM authentication is enabled

Frequently Asked Questions (FAQs)

FAQ 1: How can I find out which port my SSH service is currently using?

To determine the current SSH port, open the SSH configuration file with the command vi /etc/ssh/sshd_config and look for the line that specifies the port number.

FAQ 2: Is it necessary to restart the SSH service after changing the port?

Yes, restarting the SSH service is required for the changes to take effect. You can restart the service by executing the command service ssh restart in the Terminal.

FAQ 3: Can I change the SSH port to any random number?

While you have the flexibility to choose a port number, it is advisable to select a number not commonly associated with other services. Consult the IANA Service Name and Transport Protocol Port Number Registry to avoid conflicts.

FAQ 4: Will changing the SSH port affect existing SSH keys?

No, changing the SSH port does not impact your existing SSH keys. You can continue using your keys as usual after altering the port.

FAQ 5: How can I access my Ubuntu server after changing the SSH port?

After changing the SSH port, you need to connect to your Ubuntu server using the new port number. For example, if the new port is 5555, the command to connect would be ssh username@your_ubuntu_server -p 5555.

FAQ 6: Are there any additional security measures I should implement?

Absolutely! Changing the SSH port is just one step towards securing your system. It is crucial to employ other security measures such as strong passwords, firewall configurations, and regular system updates.

FAQ 7: Can I revert back to the default SSH port?

Yes, if for any reason you want to revert to the default SSH port (port 22), you can modify the SSH configuration file and change the port back to 22. Remember to restart the SSH service for the changes to take effect.

FAQ 8: Can changing the SSH port prevent all types of attacks?

No, changing the SSH port will not protect against all types of attacks. It significantly reduces the risk of automated attacks, but it is essential to implement a comprehensive security strategy involving various measures.

FAQ 9: Can I use a different protocol instead of TCP?

No, SSH primarily operates over TCP (Transmission Control Protocol). It is not recommended to use a different protocol for SSH connections.

FAQ 10: What should I do if I forget the new SSH port number?

If you forget the new SSH port number, you may lose remote access to your server. In such cases, physical access or other recovery methods, such as console access or a remote management interface, may be necessary to regain control.

FAQ 11: Is it necessary to modify my firewall settings after changing the SSH port?

Yes, after changing the SSH port, you must adjust your firewall settings to allow incoming connections on the new port. Failure to do so may result in the SSH service being inaccessible.

FAQ 12: Can I change the SSH port on any Ubuntu version?

Yes, you can change the SSH port on any version of Ubuntu. The process remains the same across different Ubuntu releases.

FAQ 13: Is it recommended to change the SSH port on a production server?

Changing the SSH port on a production server is generally recommended as it strengthens security. However, it requires careful planning and consideration to minimize disruption and ensure a smooth transition.


In conclusion, changing the SSH port on your Ubuntu system is a crucial step in fortifying your system’s security posture. By following the step-by-step guide outlined in this article, you can effectively enhance your system’s resilience against potential cyber threats. Remember, a strong defense is essential in safeguarding your digital assets. Embrace the power of Ubuntu and take charge of your system’s security!

Closing Disclaimer

The information provided in this article is intended for informational purposes only. The steps and recommendations mentioned herein may not suit every system configuration or scenario. It is essential to exercise caution and conduct thorough testing before implementing any changes to your system. The author and publisher disclaim any liability arising directly or indirectly from the use or application of this knowledge. Proceed with caution, and always seek professional advice when needed.