An SSH server, by default, listens to port 22, and you don't have to manually specify the port number on your SSH client if you're connecting to the server's default port.

You can specify the port number when connecting to non-standard SSH ports using the -p option or adding the port information in your SSH client's configuration file.

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

  1. Check the port that the SSH server runs on.
  2. Test if the port that the SSH server listens to is reachable from the client host (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. Specify the port to connect to using -p option.
    $ ssh remoteuser@remotehost -p 2022
  4. Add port directive to SSH client configuration file for persistence.
    $ cat .ssh/config
    Host remotehost
        user remoteuser
        port 2022
  5. Connect again using SSH client with just the Host name without providing port number as parameter.
    $ ssh remotehost
Discuss the article:

Comment anonymously. Login not required.