Skip to content
Latchkey

scp -P: Copy to a Non-Default SSH Port

scp -P <port> copies to or from a server whose SSH listens on a non-default port.

The single most common scp mistake is using lowercase -p for the port. In scp, -P is the port and -p preserves timestamps, the reverse trap from ssh.

What it does

scp -P sets the remote SSH port. The default is 22. The case matters and is opposite to ssh: scp -P is the port, scp -p preserves modification times and modes. ssh -p is the port, ssh has no -P for this.

Common usage

Terminal
scp -P 2222 ./app.tar.gz user@host:/srv/app/
scp -P 2222 -r ./dist user@host:/srv/app/
# preserve timestamps AND set the port (both flags)
scp -P 2222 -p ./app.tar.gz user@host:/srv/app/

Options

FlagWhat it does
-P <port>Remote port for scp (uppercase)
-pPreserve times and modes (not the port!)
-rRecursive directory copy
-i <key>Identity file

In CI

When the SSH server is on a custom port, also scan that port into known_hosts: ssh-keyscan -p 2222 host. A defining ssh config Host block with Port set lets you drop -P entirely, since scp reads ~/.ssh/config too.

Common errors in CI

ssh: connect to host host port 22: Connection refused after passing -p (lowercase) by mistake: scp ignored it as the timestamp flag and still used port 22. "Connection timed out" usually means a firewall on the custom port. "Host key verification failed" means known_hosts was seeded on a different port than the one scp uses.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →