devutils/ATFQ/book ATFQ git

Part III · Shell and commands

29.5 Copying files over SSH: scp, sftp and rsync

alsoscpsftpsshfscopy over sshcopy to serveruploaddownload file

TL;DR

  • Everything that copies over SSH uses the same keys and the same ~/.ssh/config names: if ssh db works, scp file db: works.
  • scp for a quick copy, rsync for folders and anything big (it resumes and only sends differences), sshfs to mount a remote folder.
  • A path without a leading / after host: starts in your home folder there.
scp report.pdf db:/tmp/
scp -r db:logs/ ./logs-db/
rsync -avP project/ db:project/      # resumable, shows progress

Pitfalls

Deep dive 2 questions

What changed in scp?

The old scp protocol ran a shell command on the server and trusted its output, which caused security bugs and odd quoting. Since OpenSSH 9.0, scp speaks the SFTP protocol underneath, with the same command line. scp -O brings back the old protocol for servers without SFTP.

Why is rsync faster on the second run?

It compares sizes and dates, and for changed files, checksums of blocks, then sends only the blocks that differ. Copying a 10 GiB folder again after a few edits sends a few megabytes.

Commands
scp sftp rsync sshfs
See also
28.7 rsync
29.2 ~/.ssh/config, jump hosts and host keys