You can configure your SSH server to run on any available ports. Extending the same method, you can configure your SSH server to run on more than one ports.

This could be useful if your SSH server is connected to multiple networks and you require your SSH server to listen to different port on some of the networks.

Run SSH server on more than one ports:

  1. Launch your preferred terminal application.
  2. Check if the ports you plan to assign to your SSH server is not already in use.
    $ ss -tlnp | grep -E "22|2022"
    LISTEN     0      128          *:22                       *:*
    LISTEN     0      128         :::22                      :::*

    SSH server currently runs on port 22, which is expected.

  3. Edit sshd configuration file with your favourite text editor.
    $ sudo vi /etc/ssh/sshd_config
  4. Search for Port option and set the value to the ports that you desire.
    Port 22
    Port 2022

    Notice the multiple declaration of the Port directive where SSH will listen to all the listed ports.

    Make sure the line does not begin with # as it implies the line is commented and will be ignored.

  5. Configure firewall to allow access to the configured ports (optional, if firewall is enabled).
    $ sudo ufw allow 2022/tcp # Ubuntu/Debian
    $ sudo firewall-cmd --add-port=2022/tcp --permanent && sudo firewall-cmd --reload # CentOS / Red Hat
    success
    success

    It is assumed the default port, 22 is already configured with correct firewall configuration. Add if necessary.

  6. Configure selinux to allow SSH to run on the configured port (optional, if selinux is used).
    $ sudo semanage port -a -t ssh_port_t -p tcp 2022

    semanage can be installed on CentOS or Red Hat systems using the following command:

    $ sudo yum install --assumeyes policycoreutils-python

    It is assumed the default port, 22 is already configured with correct selinux policy. Add if necessary.

  7. Restart sshd service.
    $ sudo systemctl restart sshd
  8. Check if sshd is now running on all the configured ports.
    $ ss -tlnp | grep 22
    LISTEN     0      128          *:2022                     *:*
    LISTEN     0      128          *:22                       *:*
    LISTEN     0      128         :::2022                    :::*
    LISTEN     0      128         :::22                      :::*
Leave a comment:
Share!