ssh -p: Connect on a Non-Default Port
ssh -p <port> connects to a server listening on a port other than the default 22.
Many hardened VPS hosts move SSH off port 22. Pass -p so the pipeline reaches the right port, and remember scp uses a different flag for the same idea.
What it does
ssh -p sets the destination TCP port. The default is 22. Note the case: ssh uses lowercase -p for the port, while scp and sftp use uppercase -P for the port (lowercase -p in scp preserves timestamps).
Common usage
ssh -p 2222 user@host
ssh -p 2222 -i deploy_key user@host "deploy.sh"
# the same host in ~/.ssh/config avoids repeating the port
# Host prod
# HostName host
# Port 2222Options
| Flag | What it does |
|---|---|
| -p <port> | Port to connect to (ssh; default 22) |
| -P <port> | Port for scp and sftp (uppercase!) |
| Port <n> (config) | Set the port per host in ~/.ssh/config |
In CI
When you run ssh-keyscan to seed known_hosts for a non-22 port, pass -p too: ssh-keyscan -p 2222 host. A key scanned on the wrong port produces a known_hosts entry that never matches and you get host key verification failures.
Common errors in CI
ssh: connect to host host port 2222: Connection refused means nothing is listening there; confirm the port and any firewall. "Connection timed out" points at a network or security-group block. Using lowercase -p with scp silently enables timestamp preservation instead of setting the port, so the connection still tries 22 and fails.