TT Lab
Blog Get started
Learn Learning paths Courses

SSH and File Transfer

scp, rsync and sftp — When to Use Which

Continue in TT Lab

In one line

All three tools run over an SSH channel. The difference is whether they send everything at once (scp), send only the differences (rsync), or exchange interactively (sftp).

Why this exists

There is a reason there are three tools for moving one file. The qualities you need differ when you send one 100MB file for the first time, when you synchronize a 10GB directory every day, and when you look at the file list on a server and pick what to download.

How it works

scp — simple copy

scp -P 2222 -i ~/.ssh/id_ed25519 ./app.tar.gz root@127.0.0.1:/tmp/
scp -r ./dist root@host:/srv/app/
scp -p ./config.yaml root@host:/etc/app/       # mtime/권한 보존
scp root@host:/var/log/app.log ./              # 내려받기

Two points to note.

scp cannot resume. If it is cut at 90%, you start over from the beginning. So it is unsuitable for large transfers.

rsync — sending only the differences

rsync -aHAX --numeric-ids --delete --dry-run /srv/data/ backup@host:/backup/data/
rsync -avz -e 'ssh -p 2222 -i ~/.ssh/id_labhub' ./dist/ root@127.0.0.1:/srv/app/

You need to know the exact meaning of the options.

And the trailing slash rule. This is the spot people get wrong most often in rsync.

rsync -a /srv/data  /backup/    # /backup/data/... 가 만들어진다
rsync -a /srv/data/ /backup/    # /backup/... 에 내용이 바로 들어간다

A slash at the end of the source path means "copy the contents of this directory", and without it means "copy this directory itself".

--delete is destructive. If it runs while the source is empty or the mount has come off, it deletes all the data on the target. It is an incident that really happens often in practice. Be sure to put a guard in scripts.

set -euo pipefail
[ "$(ls -A "$SRC" | wc -l)" -gt 0 ] || { echo "source empty, abort" >&2; exit 1; }
rsync -aHAX --numeric-ids --delete "$SRC/" "$DST"

You make generational backups cheaply with hard links.

rsync -aHAX --delete --link-dest=/backup/daily.1 /srv/data/ /backup/daily.0/

Instead of copying unchanged files, --link-dest makes hard links to the files in the specified directory. Each generation looks like a full snapshot, but the disk is occupied only by the changes.

sftp — interactive/batch

sftp -P 2222 -i ~/.ssh/id_labhub root@127.0.0.1
sftp -b /root/batch.txt -P 2222 root@127.0.0.1

If you write cd, put, get, ls, and mkdir in a batch file, it runs non-interactively. It is still widely used in automation for exchanging files with partner companies. Being able to create a transfer-only account with no shell with the combination ChrootDirectory + ForceCommand internal-sftp is also a strength of sftp.

What it looks like in the field

The incident where a generational backup is copied with a tool that does not know about hard links, and the size triples. The space saved with --link-dest is lost in a single copy. -H is not included in -a, so you must specify it separately.

A backup that eats all the bandwidth and disrupts the service. Lower the priority with the combination --bwlimit=50M and ionice -c 3 nice -n 19. If the backup does not disturb production, you actually gain room to shorten the interval.

When a transfer fails, the cause is usually outside the transfer tool

scp, sftp, and rsync all talk to a program on the remote side in a binary protocol over the channel ssh opened. That remote program is often started through the user's shell, and the shell reads its startup files as it starts. If someone puts a greeting at the top of .bashrc "so you can see which server it is", those characters slip into the position of the protocol's first bytes. sftp reads the first four bytes as the message length, so it produces an absurd number like Received message too long 1027416178; convert that number to hexadecimal and the first four characters of the greeting come out as they are. In the same situation, rsync asks "is your shell clean?". The place to fix is not the transfer options but the startup file, so that it prints only when a person opens a shell.

A host key warning is not a transfer tool problem either. known_hosts is my side's memory that "the server with this name has this key", and when the server is reinstalled, that memory and the server disagree. The prescription of turning off the warning here (StrictHostKeyChecking no, memory sent to /dev/null) throws away the only mechanism that distinguishes a reinstall from a man in the middle. The right procedure is to compare the fingerprint the server presents with the fingerprint received through an out-of-band path (the infrastructure team's list, the console), and only when they match, delete the old line and trust the new key.

The configuration file has a trap too. In ssh configuration, the first value obtained wins. If the block of a decommissioned machine remains above with the same name in it, the new block below is read but not used. The habit of looking at the values that will actually apply with ssh -G, rather than reading the file by eye, reduces these accidents.

Questions like "rsync sends everything every day even though nothing changed" are of the same kind. By default rsync looks only at size and modification time and moves on (the quick check). If you do not preserve times, the copy's time becomes "the moment it was sent" every time, so the next comparison always disagrees; -I turns that comparison off altogether; and a generator that rewrites the source every time changes the source's time even when the content is the same. Adding -c brings the second transfer to 0, but it reads every file on both sides to the end every time. The eleven characters of itemize tell you the reason per file: uppercase T means the time could not be set, lowercase t means the time differed, and a line with only dots means the file is sent even though nothing differs.

What you will do in the next lab

You do two labs in a row. First you send files back and forth with scp and sftp, confirm integrity with checksums, and then tell apart three upload jobs that failed for different reasons using their error lines and the actually applied configuration. Then you build, with rsync, the trailing slash difference, a --delete guard, and a --link-dest generational backup, and fix three mirror jobs that resend everything every day using itemize.