Using SCP on a Custom Port (and Avoiding the -p vs -P Mix-Up)
If SSH listens on a nonstandard port, SCP needs to connect to that same port. The easy-to-miss detail is capitalization: use uppercase -P with scp , not lowercase -p . SCP uses the SSH port SCP doesn't have a separate network port. It connects through SSH, which uses TCP port 22 by default. If a server's SSH service listens on port 2222 , SCP must connect to 2222 too. For a one-time upload,…
When using SCP (Secure Copy) on a custom port, it's important to use the correct capitalization for the port flag. The uppercase -P flag specifies the port for SCP, while lowercase -p is used for preserving file permissions and timestamps. SCP relies on the SSH port, which defaults to TCP port 22. If a server's SSH service listens on a different port, SCP must connect to that same port.
For example, if the SSH service listens on port 2222, you would use the command "scp -P 2222 report.txt deploy@example.com:/var/tmp/" to upload or download files.
Capitalization is crucial when switching between SSH and SCP commands. SSH uses the lowercase -p flag for the port, while SCP uses the uppercase -P flag. To avoid confusion, it's recommended to double-check the capitalization before troubleshooting the server. For hosts you frequently use, you can add their connection details to the SSH config file (~/.ssh/config). This allows you to use an alias for either SCP or SSH commands, and the configured port is applied without repeating the -P flag.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.