Connecting to an SSH server on a port other than 22 is necessary whenever the remote sshd listener is bound to a different TCP port. Using the correct port determines whether the client reaches the login prompt or stops with a network error before authentication begins.

The OpenSSH client connects to port 22 by default. The -p option overrides that default for one connection, while the Port directive in ~/.ssh/config stores the alternate port in a reusable host alias that can later be inspected with ssh -G.

This workflow assumes the server is already listening on the alternate port and that any firewall or NAT rule in the path allows the connection. The first connection to that endpoint can still prompt for host-key confirmation, and using the wrong port usually ends with Connection refused, Connection timed out, or no login prompt at all.

Steps to connect to an SSH server on a different port:

  1. Connect to the server by overriding the default port on the command line.
    $ ssh -p 2222 user@host.example.net whoami
    user

    Omit the trailing whoami to open a normal interactive shell instead of running a one-command login test.

    If the client returns Connection refused or waits until a timeout, the server may not be listening on that port or the network path to it is still filtered.

  2. Add a host alias to ~/.ssh/config when the same destination is used regularly.
    Host host-alt-port
        HostName host.example.net
        User user
        Port 2222

    Add IdentityFile only when that host requires a non-default private key.

  3. Connect through the saved host alias instead of repeating the -p option every time.
    $ ssh host-alt-port whoami
    user

    The same alias can also be reused with scp and sftp so those commands inherit the configured port automatically.

  4. Show the resolved client configuration for the alias and confirm the stored port.
    $ ssh -G host-alt-port
    host host-alt-port
    user user
    hostname host.example.net
    port 2222
    ##### snipped #####

    ssh -G prints the final client settings after the matching Host blocks and built-in defaults are applied.