While the standard port for SSH servers is 22, there are instances when it may be necessary to use an alternate port. This can be for security purposes or to accommodate other preferences or limitations.

There is no need to specify the port number if connecting to the default port. However, to connect to an SSH server on a custom port, you need to use the -p option or specify the port in the SSH client's configuration file.

Steps to connect to SSH server on ports other than 22:

  1. Determine the port the SSH server uses.
  2. Check if the client host can reach the server's listening port (optional).
    $ nc -zv remotehost 2022
    Connection to remotehost 2022 port [tcp/*] succeeded!
    -v      Produce more verbose output.
    -z      Only scan for listening daemons, without sending any data to
            them.  Cannot be used together with -l.
  3. Use the -p option to specify the port when connecting.
    $ ssh remoteuser@remotehost -p 2022
  4. Add the port directive to the SSH client configuration file for persistent connection option.
    $ cat .ssh/config
    Host remotehost
        user remoteuser
        port 2022
  5. Connect again using the SSH client and the host name, without providing the port number as a parameter.
    $ ssh remotehost
Discuss the article:

Comment anonymously. Login not required.