How to run SSH server on multiple ports

Share this article

SSH is used for logging into a remote machine. It provides secure encrypted communications between two untrusted hosts (your machine and remote machine) over an insecure network. By default, SSH runs on port number 22 but we can also run the ssh server on multiple ports.

Check free port on SSH server

We are checking the free port on the SSH server. Run the following command in the server terminal it will show you all used ports.

Command :

netstat -ntlp 

Command output:

In the above snapshot, we can see all the ports which are being used in our SSH server that means we can not use these ports.

In this tutorial, I will also use port 1080, as it is not being used for any other service.

Change SSH configuration on the SSH server

Now go to the SSH server and open /etc/ssh/sshd_config. Add port 1080 (any port of your preference that is not being used) into the configuration. See the snapshot of the changed configuration.

Change SElinux rule

If you are using CentOS it is important to add the SELinux rule. Use the following command to add the rule. This command will add rule to allow 1080 port to bind with ssh server.

[root@test ~]# semanage port -a -t ssh_port_t -p tcp 1080

Restart SSH server

In order to replicate the changes run the following command to restart the server

Command :

sudo service sshd restart

Command output :

In the snapshot, you can see that server is now listening on two ports 1080 and port 22.

How to connect to the SSH server

Connect to default port

SSH client by default connects to SSH server on port 22. Therefore we will use the same command If we want to connect through port 22.

Command example :

[centos@test ~]$ 
[centos@test ~]$ ssh root@192.168.122.105

Mention port explicitly in the command

You can mention port explicitly in the command for that use -p option in ssh command, command syntax shown as follows

command syntax :

 ssh remoteuser@remotehost -p <port_number>

Command example :

[centos@test ~]$ 
[centos@test ~]$ ssh root@192.168.122.105 -p 1080

Configure ssh client

Create .ssh/config file enter the ssh related configuration into the file. For reference see the following snapshot.

Now to connect the SSH server you have to enter only the name of the server.

Command syntax:

ssh <host_name>

Command example :

[centos@test ~]$ 
[centos@test ~]$ ssh test_server

Summary

In this article, we have seen how to bind more than one port to the SSH server and how to connect ssh on different ports.

Leave a Comment

Your email address will not be published. Required fields are marked *