What it does
scp works like cp, but one side (or both) can be on another system: user@host:path. All data is encrypted by SSH, and it uses the same login (password or key) as ssh.
New OpenSSH versions (also on RHEL 9 and 10) use the SFTP protocol under the hood by default; the command stays the same. For big or repeated copies, rsync is better: it copies only what changed.
How to write it
scp [OPTIONS] SOURCE... TARGET
scp [OPTIONS] [USER@]HOST:PATH LOCAL_PATH
scp [OPTIONS] LOCAL_PATH [USER@]HOST:PATHOptions 22
Most used
Nothing matches this filter.
Examples 12
$ run as a normal user# run as root (or with sudo)
1Copy a file to a server
scp report.pdf student@server2:/tmp/The file keeps its name in /tmp on server2.
2Copy a file from a server
scp student@server2:/var/log/dnf.log ./server2-dnf.logThe remote side comes first now.
3Copy a folder
scp -r ~/website student@server2:/var/www/-rfor folders.4Use another SSH port
scp -P 2222 backup.tar.gz admin@backup01:/srv/backups/Capital P in scp (small p in ssh).
5Keep times and permissions
scp -p config.yaml server2:/etc/myapp/The copy has the same time stamps.
6Copy through a bastion host
scp -J admin@bastion.example.com app.jar admin@10.10.0.15:/opt/app/One command, even when the target is in a private network.
7Copy between two remote servers
scp -3 server1:/srv/data.sql server2:/srv/Data goes through your computer; the servers do not need keys for each other.
8Limit the bandwidth
scp -l 8000 big.iso server2:/tmp/About 1 MB/s, so the copy does not fill the office link.
9Use a deploy key in a pipeline
scp -i ~/.ssh/ci_deploy -o StrictHostKeyChecking=accept-new dist.tar.gz deploy@web01:/srv/releases/Typical step in a CI job.
10Copy many files with a wildcard on the remote side
scp 'server2:/var/log/httpd/*.log' ./logs/Quotes stop your local shell from expanding the
*.11Copy a file with spaces in the name
scp "server2:/srv/share/Q3 report.xlsx" .Quote the remote path.
12Batch mode in a script
scp -B -q backup.tgz backup@nas:/backups/ || echo "copy failed"Never hangs on a password prompt.
Nothing matches this filter.
Watch out
scp file host:(with nothing after the colon) copies to the home folder;scp file host(no colon) makes a local copy named "host".- scp overwrites existing target files without asking.
- For large trees or repeated copies use rsync (it can resume and skips unchanged files).
Tips to remember
- Remote =
user@host:path.-rfolders,-Pport,-pkeep times.