The scp Command: Copy Files Between Servers over SSH

The scp command copies files between computers over an encrypted SSH connection. It is part of OpenSSH, so it is already available on Linux, macOS and Windows 10/11 and needs nothing but SSH access to the server.
Syntax
scp [options] source destination
A remote path is written as user@server:path. If the path does not start with /, it is relative to the user’s home directory.
Basic examples
Upload a file to a server:
scp backup.tar.gz root@203.0.113.10:/root/
Download a file from a server into the current directory:
scp root@203.0.113.10:/var/log/nginx/error.log .
Copy a whole directory:
scp -r /var/www/site root@203.0.113.10:/var/www/
Copy several files:
scp file1.txt file2.txt root@203.0.113.10:/tmp/
Copy a file under a new name:
scp config.yml root@203.0.113.10:/etc/app/config.yml.new
Useful options
| Option | What it does |
|---|---|
-r |
copy directories recursively |
-P 2222 |
SSH port (capital P) |
-i ~/.ssh/key |
use a specific SSH key |
-p |
preserve modification times and permissions |
-C |
compress data in transit |
-l 8000 |
limit bandwidth, in kilobits per second |
-q |
hide the progress meter |
-v |
verbose output for troubleshooting |
-J user@jump |
connect through a jump host |
Mind the case: scp takes the port as a capital -P, while lowercase -p preserves file attributes. With ssh it is the other way round — the port is lowercase.
Custom port and key
scp -P 2222 -i ~/.ssh/id_ed25519 backup.tar.gz root@203.0.113.10:/root/
To avoid typing the options every time, describe the server in ~/.ssh/config. The command then shrinks to:
scp backup.tar.gz web:/root/
Copying between two servers
scp root@203.0.113.10:/backup/site.tar.gz root@203.0.113.20:/backup/
By default the data flows through your computer, so it needs access to both servers, while the servers do not need access to each other.
Names with spaces
Put the path in quotes:
scp "root@203.0.113.10:/root/my report.pdf" .
Using scp on Windows
On Windows 10 and 11 scp works in PowerShell and Command Prompt:
scp C:\Users\user\backup.zip root@203.0.113.10:/root/
scp root@203.0.113.10:/root/backup.zip C:\Users\user\Downloads\
If you prefer a graphical client, use WinSCP or FileZilla in SFTP mode.
What changed in recent versions
Since OpenSSH 9.0, scp uses the SFTP protocol by default instead of the legacy SCP protocol. The commands look the same. If you copy files to a very old server or a device without SFTP support and get an error, add -O to fall back to the old protocol:
scp -O firmware.bin admin@192.168.1.1:/tmp/
Common errors
Permission denied— wrong password or key, or the user cannot write to the destination directory.No such file or directory— check the path. The destination directory must exist:scpdoes not create intermediate directories.not a regular file— you are copying a directory without-r.Connection refused— wrong port, or the SSH service is not running.Host key verification failed— the server’s key has changed, for example after a reinstall. Remove the old entry:ssh-keygen -R 203.0.113.10.
When scp is not the best choice
scp is handy for one-off copies. It cannot resume interrupted transfers and sends everything again each time. For large volumes, regular synchronisation and backups, a dedicated tool such as Borg Backup is a better fit.
To keep backups away from your main server, use separate file storage.


