On this page
- When is scp the right tool?
- How is the scp command built?
- How do I copy a whole folder?
- What if SSH listens on another port or uses a special key?
- How do I go through a jump host or between two servers?
- Can I limit speed or compress during the copy?
- How do I make sure the file arrived intact?
- Why does scp fail, and how do I fix it?
- Questions
When is scp the right tool?
Reach for scp when you need to move a file or a small folder once and want a single short command. It authenticates exactly like your SSH login, encrypts the transfer and prints a progress bar. For repeated copies of a large tree, rsync is better because it skips unchanged data and can resume. For browsing remote folders interactively, sftp fits. scp sits between those two: quick, scriptable and available everywhere OpenSSH is.
Modern OpenSSH releases (9.0 and later, plus the OpenSSH 8.7 build on Rocky Linux 9 and AlmaLinux 9) run scp over the SFTP protocol under the hood. The command line stayed the same, but quoting of remote file names is now simpler and safer than with the legacy protocol.
How is the scp command built?
The pattern is scp [options] SOURCE DESTINATION, where a remote path is written as user@host:path. A path after the colon without a leading slash is relative to the remote home directory.
# upload one file to the home directory of deploy
scp notes.txt deploy@198.51.100.24:
# upload into a specific folder
scp nginx.conf deploy@198.51.100.24:/tmp/
# download a file into the current local folder
scp deploy@198.51.100.24:/var/log/nginx/error.log .
# several files at once, to one destination
scp a.csv b.csv c.csv deploy@198.51.100.24:/srv/import/Over IPv6, put the address in brackets: scp file deploy@[2001:db8:4f2a::24]:/tmp/. If you created DNS records as in the DNS records tutorial, a host name such as app.example.net works in place of the address.
How do I copy a whole folder?
Add -r. scp then walks the directory tree and recreates it under the destination.
scp -r ./dist deploy@198.51.100.24:/var/www/example.net/
scp -rp deploy@198.51.100.24:/etc/nginx ./nginx-backupThe first line creates /var/www/example.net/dist. The second pulls the whole nginx configuration folder; -p keeps modification times and modes. scp follows symlinks inside the tree and copies their targets, so a folder full of links can grow unexpectedly. For folders with thousands of small files, packing them first is far faster: see the tar archives guide, or stream directly with tar czf - dist | ssh deploy@198.51.100.24 "tar xzf - -C /var/www/example.net".
What if SSH listens on another port or uses a special key?
scp uses a capital -P for the port (lowercase -p already means preserve times), and -i for an identity file.
scp -P 2222 -i ~/.ssh/deploy_ed25519 db.sql.gz deploy@198.51.100.24:/home/deploy/Typing that every time gets old, so define a host alias in ~/.ssh/config; scp, ssh, sftp and rsync all read the same file.
Host vps
HostName 198.51.100.24
User deploy
Port 2222
IdentityFile ~/.ssh/deploy_ed25519After that, scp db.sql.gz vps: is enough. Remember to allow the custom port in the firewall before you change it: sudo ufw allow 2222/tcp on Ubuntu and Debian, or sudo firewall-cmd --permanent --add-port=2222/tcp followed by sudo firewall-cmd --reload on Rocky and AlmaLinux (plus sudo semanage port -a -t ssh_port_t -p tcp 2222 for SELinux).
How do I go through a jump host or between two servers?
Use -J to hop through a bastion: scp -J deploy@198.51.100.24 app.tar deploy@10.0.0.5:/tmp/. The file is tunneled through the first server without being stored there.
To copy from one remote machine to another, give two remote paths. Current scp routes that data through your local computer by default, which works even when the two servers cannot reach each other but uses your own bandwidth twice. Adding -3 makes that behaviour explicit on older clients:
scp -3 deploy@198.51.100.24:/srv/app.tar.zst deploy@app.example.net:/srv/When the servers can talk directly, logging in to one and running scp from there is faster.
Can I limit speed or compress during the copy?
Yes. -l sets a cap in kilobits per second and -C enables SSH compression.
# about 5 MB per second
scp -l 40000 big.iso vps:/srv/iso/
# compress text-heavy data on a slow link
scp -C access.log vps:/tmp/Compression helps with logs, SQL dumps and source code. Video, images, zip files and zstd archives are already compressed, so -C only adds CPU work for them. Every upload and download counts toward the monthly transfer of your plan; the usage tutorial shows how to watch it from inside the server.
How do I make sure the file arrived intact?
Compare checksums on both ends. The SSH transport already detects corruption on the wire, but a checksum also catches a copy that stopped half way or a disk that filled up.
sha256sum backup.tar.zst
ssh vps sha256sum /home/deploy/backup.tar.zstIdentical hashes mean identical files. On macOS use shasum -a 256, and in Windows PowerShell Get-FileHash backup.tar.zst.
Why does scp fail, and how do I fix it?
- Permission denied (publickey): scp is not finding the right key. Test plain
ssh -v vpsfirst; whatever fixes SSH fixes scp. - No such file or directory on upload: the remote folder must exist. Create it with
ssh vps mkdir -p /srv/import. - Permission denied on the remote path: the user cannot write there. Upload to
/tmpand move the file with sudo, or fix ownership. - Remote names with spaces: quote the whole remote argument, for example
scp "vps:/srv/My Files/plan.pdf" . - Need the old protocol for an ancient device without an SFTP server: add
-O.
Questions about Copy files with scp
Does scp work from Windows?
Yes. Windows 10 and 11 include the OpenSSH client, so scp runs in PowerShell or Command Prompt with the same syntax. Local paths can use C:\Users\you\file.txt or forward slashes.
Is scp deprecated?
The old scp protocol is being retired, but the scp command is not. It now uses SFTP underneath by default, so it keeps working and remains safe to use.
Can scp resume a broken transfer?
No. It starts again from zero. For large files on unreliable links, use rsync -P, which keeps partial data and continues where it stopped.
How do I copy as root when root login is disabled?
Upload as your normal user to a temporary folder, then run sudo mv on the server. Keeping root SSH login off, as recommended in disable root login, is worth that extra step.
What is the difference between -p and -P?
Lowercase -p preserves modification times and modes. Uppercase -P sets the SSH port. ssh itself uses lowercase -p for the port, which is why people mix them up.