DED9

How to connect to SSH server on alternate port

By default, SSH uses port number 22, and many sysadmins change it to avoid the onslaught of bot attacks that try to brute-force logins.

If you need to connect to a server via SSH but to a port other than the default port, use the following command:

ssh -p port_number username@ServerIP

Of course, you have to replace variables like port_number, username, and server IP.

Let me go into detail and show how to add an alternate SSH port and connect to it.

How to Connect to SSH Server Using Alternate Port in Linux

The first step is to connect to the SSH server and check if the port you want to use is already in use.

ssh user@serverIP

Now, let’s check whether port number 2222 is used by the ss command:

 

sudo ss -tulpn | grep ‘:2222

As you can clearly see, port #22 is used by process ID 889 while port #2222 is not, making it perfect for our use cases.

Now let’s start by changing the firewall rules.

Configure the firewall to access SSH through an alternate port

It is always recommended to change the firewall rules before changing the SSH port, especially if you are dealing with a remote server.

As I will add port no. 2222 as an alternative Port for SSH, I should use the given command:

sudo ufw allow 2222/tcp

If you are using SELinux, make sure you allow SSH to run on the configured alternate port:

sudo semanage port -a -t ssh_port_t -p tcp 2222

Add an alternate port to the SSH configuration file

I would keep port 22 and add another port so you can SSH through both.

First, create the SSH configuration file with the following command:

sudo nano /etc/ssh/sshd_config

Then uncomment the port 22 line and add the port you want right below it.

To make these changes, you need to restart the ssh service:

sudo systemctl restart sshd

Connect to SSH using an alternate port

As I mentioned earlier, I’ve kept Port #22 as it is, so if you find an error, you can always troubleshoot the VM via the default port.

You must specify the alternate port with the -p option as shown:

ssh -p 2222 user@ServerIP

And you can always use the old default method (with port 22). For example, I’ve used several terminal windows with default and alternate ports:

Conclusion

This was my take on how you can add an alternate port for SSH access while keeping the default port as it is. I hope this helps, and if you run into any issues, be sure to SSH into their comments.

 

Exit mobile version