Skip to main content

Using SCP on a Custom Port and Avoiding the -p vs -P Mix-Up

Learn how to use SCP on non-standard SSH ports, distinguish between -p and -P flags, and configure hosts via ~/.ssh/config.

AI-written
Inewgen
07 Oct 2026Source: Dev.to2 min read (0 views)
Share
Using SCP on a Custom Port and Avoiding the -p vs -P Mix-Up

Stock photo for illustration only, not from the actual event

Font size
  • Use uppercase -P in scp commands to specify custom SSH ports.
  • Lowercase -p is reserved for preserving file modification times and permissions.
  • Configuring ports in ~/.ssh/config eliminates the need to repeat -P.

When an SSH server listens on a nonstandard port, SCP must connect to that exact port. The detail most frequently missed is capitalization: you must use uppercase -P with scp, rather than lowercase -p.

Because SCP does not have a separate network port, it tunnels through SSH, which defaults to TCP port 22. If a server's SSH service listens on port 2222, SCP must target 2222 as well. For a one-time upload, specify the port like this:

scp -P 2222 report.txt deploy@example.com:/var/tmp/

command line terminal screen close up

Stock photo for illustration only, not from the actual event

To download a file instead, place the remote path first, where the final dot (.) represents the current local directory:

scp -P 2222 deploy@example.com:/var/tmp/report.txt .

Switching back and forth between SSH and SCP commands often trips up developers because ssh uses lowercase -p for ports while scp requires uppercase -P. Recognizing this distinction prevents unnecessary troubleshooting loops during server deployments.

SCP reserves lowercase -p specifically for preserving file modification times and permissions. You can combine both flags when you need to specify a custom port and retain file attributes simultaneously:

Never miss the latest news?

Subscribe to get news summaries by email - not often enough to be annoying.

โฆษณา

scp -p -P 2222 report.txt deploy@example.com:/var/tmp/

For hosts you access regularly, storing connection details in ~/.ssh/config streamlines the workflow without repeatedly typing -P:

Host staging
HostName example.com
User deploy
Port 2222

chromebook notebook computer office desk workspace

Stock photo for illustration only, not from the actual event

Once configured, you can simply use the alias for either command, such as scp report.txt staging:/var/tmp/, because SCP reads the same underlying SSH configuration. For transfers between remote hosts utilizing different ports, modern OpenSSH supports dedicated SCP URIs, and you can test port reachability beforehand using nc on Linux/macOS or Test-NetConnection on Windows PowerShell.

Source: Dev.to

Comments

Leave a Comment
0/2000

Found something wrong in this article? Report an issue with this article