Changing the Default SSH Port in Linux: A Step-by-Step Guide

Changing the Default SSH Port in Linux: A Step-by-Step Guide

Secure Shell (SSH) is a widely used protocol for securely connecting to remote systems. By default, SSH operates on port 22. However, changing the default port can enhance security by reducing the risk of automated attacks. In this guide, we’ll walk through the process of changing the default SSH port in a Linux environment.

Step 1: Open the SSH Configuration File To begin, open the SSH daemon configuration file using your preferred text editor. Typically, this file is located at /etc/ssh/sshd_config. For example:

bash

sudo nano /etc/ssh/sshd_config

Step 2: Locate the Line Defining the Port Within the configuration file, locate the line that specifies the port. By default, it’s usually set to Port 22.

Step 3: Change the Port Number Modify the Port directive to specify the new port number you want to use. Select a port number that is not in use and is not reserved for any other service.

Step 4: Save the Changes After editing the port number, save the file and exit the text editor.

Step 5: Restart the SSH Service To apply the changes, restart the SSH service using the following command:

bash

sudo systemctl restart sshd

Step 6: Update Firewall Rules (If Necessary) If you have a firewall enabled, ensure to allow traffic on the new SSH port. For example, using ufw:

bash

sudo ufw allow 2222/tcp

Step 7: Verify the Changes Verify that you can connect to your server using the new port:

bash

ssh username@your_server_ip -p 2222
Conclusion: Changing the default SSH port in Linux is a simple yet effective way to enhance server security. By following the steps outlined in this guide, you can mitigate the risk of automated attacks and ensure secure remote access to your system. Remember to choose a new port number that is not in use and update your firewall rules accordingly.