How to Change Default SSH Port on Ubuntu

Change Default SSH Port on Ubuntu

Are you looking to increase the security of your Ubuntu server? Changing the default SSH port is a simple yet effective way to do so. By default, SSH (Secure Shell) servers listen on TCP port 22. However, this port is often targeted by attackers. To help prevent unauthorized access and secure your server, it’s recommended that you change the SSH port.

In this article, I’ll walk you through the steps to change the SSH port on Ubuntu. To do this, you’ll need to have a user account with sudo privileges on your server. Once you’ve changed the SSH port, you’ll need to use the new port number whenever you connect to your server via SSH. So, let’s get started on changing the SSH port on Ubuntu.

Why Change the Default SSH Port on Ubuntu?

When you install OpenSSH Server on Ubuntu, it automatically listens on the default SSH port, which is 22. This port is well-known and is used by attackers to target your server. Changing the default SSH port to a random number above 1024 can provide an additional layer of security, making it more difficult for attackers to identify which port SSH is listening on. In this section, I’ll explain why you should consider changing the default SSH port on Ubuntu.

1. Mitigate Automated Attacks

The internet is filled with automated bots that continuously scan IP addresses looking for common ports, especially for an open SSH port running on default port 22. This makes your server more prone to brute force attacks, where an attacker tries to guess the username and password combination for your system.

By changing the default SSH port to a different, random port number, you can make it more difficult for automated bots to discover your SSH port. This is by no means a foolproof way of securing your system, but it does help to mitigate the risk of automated attacks.

2. Reduce Risk of Brute Force Attacks

As mentioned before, having the default SSH port open can make your server an easy target for brute-force attacks. These attacks try to guess your username and password by running through a list of possible combinations. When the default SSH port is changed to a random number, it adds an extra layer of security, making it more difficult for attackers to gain access to your system.

3. Avoid Log Clutter

When your SSH port is the default, your system logs will be filled with numerous login attempts from bots trying to automatically access your server. This can create a cluttered log system, making it difficult to identify legitimate login events.

By switching to a less common port number that is not as susceptible to automated attacks, you can minimize the amount of noise in the system logs, making it easier to monitor and identify real login attempts.

In conclusion, changing the default SSH port on Ubuntu is a simple, yet powerful, security measure that can improve your systems’ security posture. While it’s not a guarantee against sophisticated attacks, it can help you to reduce the risk of automated attacks, prevent brute force attacks and avoid clutter in the system logs. By following good security practices, such as keeping the SSH service up to date and enforcing strong passwords, your Ubuntu server can be further protected from potential exploits.

How to Check the Current SSH Port on Ubuntu?

Before changing the default SSH port on Ubuntu, you need to check the current SSH port. If you’re not quite sure what port your SSH service is currently using, you can quickly find out by following these simple steps:

  1. Open a terminal window on your Ubuntu system.
  2. Type the following command:
    sudo grep -i port /etc/ssh/sshd_config
    

    This command will search the sshd_config file for any line containing the keyword ‘port’, regardless of capitalization.

  3. Press the Enter key.The output of this command will show you which port SSH is currently configured to listen on. Typically, the default port for SSH service is 22. If you see a different port number, then this is the port you will need to use to connect to your system via SSH.

It’s important to note that changing the SSH port is a good security practice and can help to deter potential attacks from hackers or bots. However, it’s not a foolproof way of preventing unauthorized access, and there are several other security measures that you should take to secure your SSH service.

How to Change the Default SSH Port in the SSH Configuration File?

By default, the Secure Shell (SSH) protocol uses port 22 to establish a secure connection between two computers. This makes it easy for hackers to launch attacks against this well-known port and gain unauthorized access to your system. Therefore, it is recommended to change the default SSH port to add an extra layer of security to your server.

In this section, I will guide you through the steps to change the default SSH port in Ubuntu.

Step 1: Login as Root User

First, you need to log in as the root user on your Ubuntu server. You can do this by running the following command:

sudo -i

Step 2: Modify SSH Configuration File

Next, you need to modify the SSH configuration file by running the following command:

nano /etc/ssh/sshd_config

This will open the SSH configuration file in the Nano text editor.

Step 3: Change SSH Port

To change the SSH port, you need to locate the line that reads “# Port 22” in the configuration file. Remove the “#” sign and replace 22 with the new desired port number. For instance, if you want to change your SSH port to 8888, you should change the line to:

Port 8888

Step 4: Save and Close the SSH Configuration File

After modifying the port number, save and close the SSH configuration file by pressing “CTRL+X”, then “Y” to save changes, and “Enter” to confirm.

Step 5: Restart SSH Service

Finally, you need to restart the SSH service for the changes to take effect. You can do this by running the following command:

sudo systemctl restart sshd 

Now, your SSH server is listening on the new port number and you can establish a connection using it. For example, if you want to connect to your server using SSH on port 8888, run the following command:

ssh -p 8888 user@server_ip_address

In conclusion, it is highly recommended to change the default SSH port to add an extra layer of security to your server. By following the steps above, you can easily change the SSH port in Ubuntu and minimize the risk of unauthorized access to your system.

How to test the new SSH Port?

After successfully changing the default SSH port on your Ubuntu server, it’s important to test the new port to ensure that the configuration changes have been made correctly. Here’s a step-by-step guide on how to test the new SSH port:

  1. Open a terminal window on your local machine and try to SSH into your server using the new port.

ssh <username>@<server-ip> -p <new-port>

  1. If the connection is successful, you should be prompted to enter your user credentials. Once you’ve entered the correct credentials, you should be able to access your server.
  2. If the connection fails, double-check that you’ve entered the new port correctly and that the firewall rules have been configured to allow traffic through the new port. Try pinging the server to see if it’s reachable:

ping <server-ip>

  1. If the ping is successful, but the SSH connection still fails, it’s possible that your ISP is blocking the new SSH port. Try connecting to the server from a different location or using a VPN.
  2. Finally, it’s a good idea to test the new port by performing a port scan. This will help you determine if the new port is open and accessible from outside your network.

You can perform a port scan using the Nmap tool:

nmap -p <new-port> <server-ip>

If the new SSH port is open, you should see something like this:

PORT     STATE SERVICE
<new-port>/tcp open  ssh

If the new SSH port is closed, you’ll see something like this:

PORT     STATE  SERVICE
<new-port>/tcp closed ssh

How to Troubleshoot SSH Connectivity Issues After Changing the Default SSH Port?

Changing the default SSH port on Ubuntu is one step you can take to improve the security of your network. However, after changing the default SSH port, you may encounter issues connecting to your server. In this section, I’ll outline some common SSH connectivity issues and how to troubleshoot them.

1. Check Firewall Settings

Firewall settings can prevent you from connecting to your server, even after changing the default SSH port. Make sure your firewall allows inbound connections to the new SSH port. To check UFW firewall settings on Ubuntu, use the following command:

sudo ufw status

If UFW is enabled, it will show the status of your firewall along with the allowed ports. If the new SSH port is not allowed, enable it using the following command:

sudo ufw allow <new port>/tcp

2. Check SSH Configuration Settings

After changing the default SSH port, it’s important to update the SSH configuration settings to reflect the new port. First, open the SSH configuration file using the following command:

sudo nano /etc/ssh/sshd_config

Find the line that says “Port 22” and change it to “Port <new port>”. Save the changes and exit the editor. To apply the changes, restart the SSH service:

sudo systemctl restart sshd

3. Verify SSH Connection

If you’re still unable to connect to your server using SSH, verify that SSH is listening on the new port. Use the following command to check the SSH service status:

sudo systemctl status sshd

Look for the line that says “listening on” followed by the new SSH port number. If SSH is not listening on the new port, verify that the changes were made to the SSH configuration file and restart the service.

4. Check Network Connectivity

Issues with network connectivity can cause SSH connection problems. Make sure your server is properly connected to the network and that the port forwarding rules are set up correctly. Verify that you can ping the server’s IP address from another computer on the network.

Marshall Anthony is a professional Linux DevOps writer with a passion for technology and innovation. With over 8 years of experience in the industry, he has become a go-to expert for anyone looking to learn more about Linux.

Related Posts