An SSH server that listens away from port 22 will not answer a normal ssh user@host connection. Supplying the alternate port sends the client to the actual sshd endpoint, which separates a reachable login service from a network failure before authentication starts.
The OpenSSH client accepts the port on the command line for one connection, or from a Port directive inside a matching Host block. A saved alias in ~/.ssh/config keeps the hostname, user, port, and optional key together so repeated commands do not depend on remembering -p each time.
The server must already listen on the alternate TCP port, and every firewall, security group, or NAT rule in the path must allow that port. A first connection to the same hostname on a new port can still show a host-key prompt because OpenSSH tracks non-default endpoints as [host]:port entries.
Related: How to change the SSH server port
Related: How to run SSH server on multiple ports
Steps to connect to an SSH server on a different port:
- Confirm that the server and network path are intended to use the alternate SSH port.
The client-side command does not move the server listener. Configure sshd and any firewall rule first, then use this page to connect to that published endpoint.
- Connect with the alternate port on the command line.
$ ssh -p 2222 user@host.example.net whoami user
Omit whoami to open an interactive shell instead of running a one-command login test.
- Save the alternate port in a per-user SSH client alias when the same endpoint is used regularly.
Host host-alt-port HostName host.example.net User user Port 2222 IdentityFile ~/.ssh/id_ed25519Omit IdentityFile when the default key or ssh-agent already supplies the right identity.
Tool: SSH Config Snippet Generator - Show the resolved client configuration for the alias.
$ ssh -G host-alt-port host host-alt-port user user hostname host.example.net port 2222 ##### snipped ##### identityfile ~/.ssh/id_ed25519
ssh -G prints the final client settings after matching Host blocks and defaults are applied, without opening a network session.
Related: How to show SSH client configuration - Connect through the saved host alias.
$ ssh host-alt-port whoami user
The same alias can be reused with scp and sftp so those clients inherit the configured hostname, user, port, and key.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.