SCP Linux Command
scp stands for secure copy. It copies files between hosts over SSH — encrypted, authenticated, and available on any system with OpenSSH installed.
You can copy from local to remote, remote to local, or between two remote servers. It uses the same authentication as SSH: passwords, keys, jump hosts, all of it works the same way.
One thing worth knowing: since OpenSSH 9.0, scp uses the SFTP protocol under the hood. The command syntax has not changed. If you need to sync directories or resume interrupted transfers, rsync is the better tool — there is a comparison at the end of this article.
Contents
- Syntax
- Copy a file to a remote server
- Copy a file from a remote server
- Copy a directory recursively
- Copy multiple files
- Custom SSH port
- Authenticate with an SSH key
- Copy between two remote servers
- Jump host
- Limit bandwidth
- Preserve file attributes
- Verbose mode
- Common errors
- SCP vs rsync
- Practical examples
Syntax
scp [options] source destination
Either source or destination can be remote, in the format user@host:/path.
scp /local/file user@host:/remote/path
scp user@host:/remote/file /local/path
scp user@host1:/path user@host2:/path
Copy a File to a Remote Server
scp /home/user/table.csv [email protected]:/home/jane/
Copy to a specific filename on arrival:
scp backup.tar.gz [email protected]:/home/user/backup-2026.tar.gz
Copy using a hostname:
scp nginx.conf [email protected]:/etc/nginx/nginx.conf
Copy a File from a Remote Server
scp [email protected]:/var/log/nginx/error.log .
The . copies it to your current directory. To specify a destination:
scp [email protected]:/var/log/nginx/error.log /tmp/error.log
Copy a Directory Recursively
Use -r to copy a full directory:
scp -r /var/www/html [email protected]:/var/www/
Pull a directory from a remote server:
scp -r [email protected]:/var/www/html /tmp/site-backup/
Copy a directory and preserve file attributes:
scp -rp sourcedirectory user@host:/path/
-r copies recursively, -p preserves permissions and timestamps.
Copy Multiple Files
Space-separated list:
scp file1.txt file2.txt file3.txt user@host:/home/user/
Using a wildcard:
scp /home/user/*.txt [email protected]:/home/jane/
All files in a folder:
scp /home/user/html/* [email protected]:/home/jane/backup/
Use a Custom SSH Port
Use -P (uppercase) when SSH is not on port 22:
scp -P 2222 backup.tar.gz [email protected]:/home/user/
Note the uppercase -P. This is different from ssh, which uses lowercase -p.
Authenticate with an SSH Key
Use -i to specify a private key:
scp -i ~/.ssh/id_ed25519 backup.tar.gz [email protected]:/home/user/
If you have multiple keys and the default does not match:
scp -i ~/.ssh/deploy_key config.yml [email protected]:/app/config/
For more on setting up SSH key authentication, see Secure SSH access with no password.
Copy Between Two Remote Servers
With scp you can copy files between remote servers without downloading them to your local machine first.
If both servers can reach each other directly:
scp [email protected]:/home/jane/table.csv [email protected]:/home/pete/
If the servers are on separate networks with no route between them, use -3 to route the transfer through your local machine:
scp -3 user@server1:/path/to/file user@server2:/path/to/folder/
You issue the command on your local machine, which must have SSH access to both servers.
Copy Through a Jump Host (Bastion)
Use -J to proxy the connection through a bastion server:
scp -J [email protected] file.txt user@internal-server:/home/user/
This is the modern way to reach servers behind a firewall. See also SSH tips and tricks.
Limit Bandwidth Usage
Use -l to cap bandwidth in Kbits per second. Useful when copying large files on a busy server:
scp -l 8000 large-backup.tar.gz [email protected]:/backups/
8000 Kbits/s is approximately 1 MB/s. Adjust to what your server can spare.
Combine with compression (-C) to save bandwidth on slow links:
scp -C large-file.tar [email protected]:/backups/
Preserve File Attributes
The -p flag preserves modification times, access times, and permissions:
scp -p script.sh [email protected]:/usr/local/bin/
Without -p, the destination file gets the current timestamp and the default umask permissions.
Verbose Mode for Troubleshooting
-v shows what scp and the SSH layer are doing:
scp -v backup.tar.gz [email protected]:/home/user/
For more detail:
scp -vv backup.tar.gz [email protected]:/home/user/
Common SCP Errors
Permission denied
The remote user does not have write access to the destination, or the SSH key is not accepted.
scp: /root/backup.tar.gz: Permission denied
Check that the destination directory is writable by the user, and verify your key is in ~/.ssh/authorized_keys on the remote host.
Connection refused
SSH is not running, or the port is wrong.
ssh: connect to host 192.168.1.10 port 22: Connection refused
Check that sshd is running: systemctl status sshd. If using a custom port, add -P.
Host key verification failed
The server's fingerprint changed — common after reinstalling a server.
WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
Remove the old key from your known_hosts:
ssh-keygen -R 192.168.1.10
Then reconnect and accept the new fingerprint.
No such file or directory
The source path does not exist, or the destination directory does not exist on the remote host.
scp: /home/user/missing.txt: No such file or directory
If the destination directory does not exist, create it first:
ssh [email protected] 'mkdir -p /home/user/newdir'
scp file.txt [email protected]:/home/user/newdir/
SCP vs rsync
| | scp | rsync |
|---|---|---|
| Copies files over SSH | ✓ | ✓ |
| Skips unchanged files | ✗ | ✓ |
| Resumes interrupted transfers | ✗ | ✓ |
| Syncs deletions | ✗ | ✓ (with --delete) |
| Bandwidth limiting | ✓ (-l) | ✓ (--bwlimit) |
| Preserves attributes | ✓ (-p) | ✓ (-a) |
| Dry run | ✗ | ✓ (-n) |
Use scp when you are copying one or a few files once. Use rsync when syncing directories, running backups, or when transfers may be interrupted.
Practical Examples
Copy a database dump with today's date to a backup server:
scp /var/backups/mysql/db-$(date +%F).sql user@backup01:/var/backups/mysql/
Copy files to multiple servers with a loop:
for server in web01 web02 web03; do
scp nginx.conf user@$server:/etc/nginx/nginx.conf
done
Copy, then delete source after successful transfer:
for file in backup-*.tar.gz; do
scp "$file" user@backup01:/backups/ && rm "$file"
done
Pull logs matching a date pattern:
scp 'user@web01:/var/log/nginx/access-2026-05-*.log' /tmp/logs/
Copy with custom port, key, and bandwidth limit combined:
scp -P 2222 -i ~/.ssh/deploy_key -l 4000 app.conf [email protected]:/etc/app/
Copy a directory through a jump host:
scp -J [email protected] -r /var/www/html user@internal:/var/www/
Move a file between two remote servers routing through local:
scp -3 user@server1:/exports/data.csv user@server2:/imports/
Related: rsync · SSH login without password · SSH tips and tricks · Run commands on a remote server via SSH
Originally published: June 8, 2007 · Last updated: May 29, 2026
By: Guillermo Garron