SCP Commands: A Comprehensive Guide

scp (Secure Copy) is the tool I reach for whenever a build artifact, log file, or backup needs to move between my machine and a remote server. It uses the same authentication as SSH, which means everything you already configured for SSH — keys, custom ports, config aliases — works for SCP too. This guide covers the syntax, the real examples, and the errors that trip people up (including one that is unique to SCP: -P vs -p).

SCP secure copy file transfer between local and remote machines using SSH

Understanding SCP Infrastructure

SCP runs entirely over an SSH connection. The remote machine must be running an SSH server (sshd), and your user account must be allowed to log in. All data — including your password, if you use one — is encrypted in transit, so SCP is safe to use over untrusted networks.

Prerequisites

SCP Syntax

scp [options] source destination

A remote path looks like user@host:/path/to/file. A local path is just a normal path. The position of the remote path determines the direction: source means "copy from", destination means "copy to".

Common Options

OptionMeaning
-rCopy directories recursively
-P portCustom SSH port (uppercase P — unlike ssh's lowercase -p!)
-i keyfileUse a specific private key for authentication
-pPreserve modification times and permissions
-CEnable compression for slow links
-qQuiet mode; suppress the progress meter

1. Push a Local File to a Remote Host

scp myfile.txt username@remote-server:/path/to/destination/

If the destination directory does not exist or is not writable, SCP fails with No such file or directory — create it first with ssh username@server "mkdir -p /path/to/destination".

2. Pull a Remote File to Your Local Machine

scp username@remote-server:/var/log/app.log /home/rinas/logs/

The command prints a progress bar and, when finished, returns to the shell prompt:

app.log                        100%   24MB  12.3MB/s   00:02

The columns are: filename, transfer percentage, total size, average speed, elapsed time. A clean 100% line with no error message means the transfer completed.

3. Copy a Directory Recursively

scp -r build/ username@remote-server:/opt/myapp/

Without -r, copying a directory fails with scp: ...: not a regular file.

4. Use a Custom SSH Port

This is the classic SCP gotcha: ssh uses -p for the port, but scp uses -P. Using the wrong one gives a confusing scp: illegal option -- p error.

scp -P 2222 myfile.txt username@remote-server:/tmp/

5. Use a Specific SSH Key

scp -i ~/.ssh/id_ed25519 backup.tar.gz deploy@server:/srv/backups/

If the key is protected by a passphrase, SCP asks for it before starting the transfer. See SSH key authentication if you need a refresher on generating keys.

Verifying Your File Transfers

# Check the local copy
ls -la /local/path/

# Check the remote copy over SSH
ssh username@remote-server "ls -la /path/to/destination/"

# Compare checksums to be certain nothing was corrupted
md5sum file.txt
ssh username@remote-server "md5sum /path/to/destination/file.txt"

Common Problems

Problem 1: Permission denied (publickey,password)

Cause: your key is not in the remote ~/.ssh/authorized_keys, or your account does not allow password login.

Solution: verify you can log in with ssh first — SCP reuses exactly the same authentication. If SSH works, SCP will work. If not, fix SSH first, then add your public key with ssh-copy-id user@host.

Problem 2: Connection refused

Cause: the SSH daemon is not running on the remote host, or you used the wrong port.

Solution: check the remote service with ssh user@host systemctl status ssh or, if you can reach it, use nc -zv host port to test the port.

Problem 3: WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED

Cause: the remote host's key changed — for example after a reinstall or a new server taking over the same IP.

Solution: if you are sure the server change is legitimate, remove the old entry: ssh-keygen -R server-ip, then retry.

Problem 4: scp: illegal option -- p

Cause: you used -p for the port. That is the ssh flag; SCP uses uppercase -P.

Solution: scp -P 2222 .... Lowercase -p in SCP means "preserve timestamps", which is why the clash exists.

Best Practices

Conclusion

SCP answers one question simply: how do I move a file between machines securely? Everything works the way your SSH does — the only real trap is the uppercase -P. If you hit an error, first confirm that plain SSH login works; SCP inherits all of SSH's behavior, so an SSH problem is almost always the root cause.

Related Articles

A.M. Rinas

Written by A.M. Rinas

Full-Stack Software Engineer working on web applications, DevOps automation, and databases. More about me on the about page, or say hi through the contact page and LinkedIn.

← Back to all articles