SSH to Port Other Than 22: Secure Your Connections with Alternate Ports

SSH to Port Other Than 22

When managing remote servers, it’s essential to have a secure connection. By default, Secure Shell (SSH) listens on port 22, but utilizing this common port can expose servers to various security threats. To protect our systems, we recommend ssh on specific port Linux, thereby enhancing security by taking advantage of lesser-known ports and making it difficult for potential attackers to target our server.

To achieve this extra layer of safety, we’ll show you how to ssh with port number in Linux. Changing the default port plays a crucial role in reducing the risk of unauthorized access and improving protection against brute force attacks. By setting up SSH on a different port, we can minimize the likelihood of our server being targeted by automated scripts scanning for open ports.

Now that we understand the importance of using a custom port for SSH, we’ll guide you through the process of configuring SSH to listen on a non-default port. We’ll also discuss how to connect to your server using ssh with port command. With these steps, you’ll be well on your way to securing your server and enhancing its protection against potential threats.

Understanding SSH and Custom Ports

Secure Shell (SSH) is a widely-used protocol for secure remote access and administration of servers, routers, and other network devices. By default, SSH listens on port 22. However, it’s possible to configure SSH to listen on different ports for improved security or to avoid conflicts with other services. In this section, we’ll explain the benefits of using custom ports for SSH and how to connect to an SSH server on a specific port in Linux.

One reason to change the default SSH port is to reduce the chance of unauthorized access attempts. Often, attackers scan for open SSH ports by launching automated attacks on the default port. By using a custom port, we can make it less likely for these attacks to target our systems. Moreover, customizing the SSH port can help resolve conflicts with other services that might also be running on port 22.

To connect to an SSH server using a custom port, we must specify the desired port number when initiating the connection. For instance, if we want to use ssh with port 2222, the command would look like this:

ssh -p 2222 username@hostname

In this example, -p is an option that indicates the port number. Replace 2222 with the desired port number, username with the appropriate user account on the target server, and hostname with the server’s IP address or domain name.

To configure ssh on specific port Linux, follow these steps:

  1. Edit the SSH configuration file, typically located at /etc/ssh/sshd_config, using a text editor like vi or nano.
  2. Search for the line that starts with Port, and change the number to the desired custom port. If the line is commented out with a #, remove the # to enable the custom port setting.
  3. Save and exit the file.
  4. Restart the SSH service using the following command:
sudo systemctl restart ssh

Now, the SSH server will listen on the new custom port. Remember to update any firewall rules or security group settings to allow traffic on the new port.

  • SSH on custom ports offers improved security
  • Reduces the chance of unauthorized access attempts
  • Resolves conflicts with other services

In summary, using a custom SSH port can enhance the security of our systems and avoid conflicts with other services running on the default port. By understanding how to configure ssh with port number and connect using a specific port, we can implement this security measure effectively.

Configuring SSH to Use a Different Port

Switching the default SSH port can enhance security, as attackers often target default ports. We’ll show you how to configure your SSH server to use a different port other than the default port 22 in a few simple steps. This guide focuses on ssh on specific port Linux configurations, but the process is similar on other systems as well.

Step 1: Update the SSH Configuration File

The first step to using ssh with port number other than 22 is modifying the SSH configuration file on your server. In most Linux distributions, you’ll find the SSH configuration file at /etc/ssh/sshd_config. To update the file, simply open it using your preferred text editor.

sudo nano /etc/ssh/sshd_config

Once the file is open, locate the line that begins with #Port 22. Remove the hash symbol # to uncomment the line, then replace 22 with your desired port number. For example:

Port 2222

Save and close the file.

Step 2: Adjust Firewall Rules

Before you can successfully ssh with port change, you’ll need to adjust your firewall rules. This ensures the firewall allows connections to the new SSH port. For instance, if you’re using ufw (Uncomplicated Firewall) on Linux, you can run the following commands:

sudo ufw allow 2222/tcp
sudo ufw delete allow 22/tcp

Replace 2222 with your preferred port number.

Step 3: Restart the SSH Service

After updating the configuration file and adjusting the firewall rules, restart the SSH service for the new port settings to take effect.

sudo systemctl restart sshd

Step 4: Test New SSH Connection

To verify your new SSH configuration, try logging into your server using the ssh on specific port Linux command:

ssh -p 2222 user@your_server_ip

If everything was configured correctly, you should now be connected to your server using the new port. Ensure your existing SSH sessions won’t be affected during the process. It’s best to test the new connection before closing the old sessions.

Key Points to Remember

  • Remember to update your SSH configuration file (/etc/ssh/sshd_config)
  • Adjust your firewall rules to allow.connections to the new SSH port
  • Restart the SSH service and test the new configuration

By following these steps, you can easily configure your SSH server to use a custom port other than the default port 22. This can enhance your server security by reducing the likelihood of automated attacks targeting the standard SSH port.

Securing Your SSH Connection on the New Port

When configuring SSH on a specific port in Linux, it’s essential to ensure the connection is secure. By default, SSH operates on port 22, but it’s common to change the port to minimize the risk of unauthorized access. In this section, we’ll outline some crucial steps to improve security when using SSH with a different port number.

Firstly, update software regularly to keep your server secure. We recommend regularly checking for and installing updates, including security patches and bug fixes. By doing this, you’ll minimize vulnerabilities that can be exploited by potential attackers.

Next, it’s essential to use strong authentication methods, such as public key authentication. Public key authentication relies on a pair of keys – one private and one public, making it more challenging to crack than simply using a password. Here’s how to configure SSH with public key authentication:

  1. Generate a key pair on the client machine using ssh-keygen.
  2. Copy the public key to the server using ssh-copy-id user@server-ip -p new_port.
  3. Ensure the correct permissions are set on the client’s .ssh directory and the authorized keys file.
  4. Edit the server’s sshd_config file, setting PasswordAuthentication no or PubkeyAuthentication yes.

Another essential step is utilizing a strong firewall to restrict incoming connections to the new SSH port. Many Linux distributions come with a built-in firewall like iptables or ufw. Configure the firewall to allow incoming connections only from trusted IP addresses and block all other traffic. This limits the exposure of your SSH service to potential attackers.

Additionally, consider implementing rate limiting by using tools such as fail2ban. This will help protect against brute force attacks by temporarily banning IP addresses that make several failed login attempts.

To further harden your SSH connection, follow these best practices:

  • Disable root login by setting PermitRootLogin no in your sshd_config file.
  • Limit the number of users allowed to use SSH by specifying a list of allowed users with AllowUsers.
  • Use a secure SSH protocol version, which is typically SSH-2, by setting Protocol 2 in the configuration file.

Finally, always remember to restart your SSH service after making any changes to the sshd_config file. This ensures that the changes are applied, and your SSH connection remains secure. By following these steps and best practices, you’ll enhance the security of your SSH with a port number connection, keeping your server safe from potential threats.


By now, we’ve effectively explored the process of setting up and using SSH to connect to a port other than the default port 22. Accessing an SSH server using custom ports not only adds an extra layer of security but also helps in managing multiple servers efficiently.

There are several ways to use SSH on specific ports in Linux, all of which have been discussed in the earlier sections. The key is to first configure the SSH server with a custom port number and then enable access using various methods. Here are the main methods to use SSH with custom port numbers:

  • In the command line, use the -p flag followed by the customs port number while initiating an SSH connection.
  • Configure the SSH client by specifying the custom port in the file ~/.ssh/config.

In summary, our main takeaways are:

  • It’s easy to use SSH on a specific port in Linux by combining configuration and command line options.
  • The process involves editing the server configuration file and using the -p flag or the ~/.ssh/config file.
  • Utilizing custom ports enhances server management and security.

So go ahead and try using SSH with a port number in your future projects, and take advantage of the improved security and management features. Remember to always review server logs and firewalls to ensure your servers remain secure and functional. Happy SSH-ing!

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