Remote work with ssh, scp and rsync

Intermediate · 11 min

Run commands on another machine with ssh, copy files with scp, and sync directories with rsync. Trailing slashes, dry runs, --delete, and what I could not test.

Before thisArchives with tar and gzipPermissions and chmodSurviving logout: nohup, disown and tmux

312 school · 11 min

Remote work with ssh, scp and rsync

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by 312 schoolReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • ssh runs a shell or a single command on another machine over an encrypted connection and authenticates with a key pair. The private key stays on your machine, with mode 600.
  • scp copies named files. rsync copies only what differs, can resume, and can exclude patterns or mirror a directory with --delete, so it is the better tool for repeated transfers.
  • In rsync, a trailing slash on the source means copy the contents of this directory. Without it the directory itself is copied inside the destination.
  • Preview with rsync -avn before anything destructive. --delete removes destination files that are gone from the source, so it should never be a first run.
  • Remote commands run in a non-interactive shell with their own quoting layer. A command line is parsed once locally and once remotely.

Why three tools

Sooner or later the files you want are on another machine: a server, a build box, a backup disk. You need to get a shell there, copy a file over, and keep two directories in step. One wire protocol covers all of it, SSH, which encrypts the connection and checks who is who. Three commands sit on top.

  • ssh gives you a shell on the other side, or runs one command there.
  • scp copies named files over that connection.
  • rsync compares two directory trees and sends only the differences.

Here is the limit of my testing, up front. ssh localhost on the Mac I used was refused (Connection refused, no SSH server enabled), and I would not change its settings. So no real ssh session or remote copy ran. Everything about login, remote commands and user@host: paths comes from documentation and the video, not from my terminal. What I did run: rsync and scp between two local directories, ssh -G (which prints settings without connecting), and ssh-keygen into a scratch folder.

Keys, not passwords

You log in with a key pair. The public half goes on the server, the private half stays with you. I made a throwaway pair in a scratch directory, never in ~/.ssh:

$ ssh-keygen -q -t ed25519 -N '' -C demo@lesson -f ./id_demo
$ ls id_demo id_demo.pub
id_demo  id_demo.pub
$ ssh-keygen -lf id_demo.pub
256 SHA256:<hash> demo@lesson (ED25519)
$ ls -l id_demo | awk '{print $1}'
-rw-------@

The hash is abbreviated. The private key was created with mode -rw------- (600), which is permissions ssh insists on. I used an empty passphrase (-N '') only because it is a throwaway. A real key should have one.

What ssh would use

ssh -G prints the configuration ssh would use for a destination without connecting. With -F /dev/null it ignores any config file:

$ ssh -F /dev/null -G -p 2222 -l deploy build.example.com | grep -E '^(hostname|user|port) '
user deploy
hostname build.example.com
port 2222

The same settings come from ~/.ssh/config with a Host block, so ssh build could mean all of that. ssh user@host 'command' runs one command and returns its output, which makes it work in pipelines. Quote it carefully: your local shell parses the line first, and the remote shell parses it again, so a * or $VAR expands on whichever side you did not quote against.

scp

scp copies like cp with a remote path written user@host:path:

scp report.txt me@server:/srv/reports/
scp -r me@server:/var/log/app ./logs

I ran only the local form, which copies one file to another path, and it did:

$ scp -q src/a.txt copy.txt && cat copy.txt
changed

That says the command works and nothing about network transfers. It copies everything every time.

rsync

The tool for repeated transfers. I ran these on macOS, where rsync is openrsync, a reimplementation that reports “rsync version 2.6.9 compatible”. Its options are a subset of the GNU/Samba rsync you will meet on Linux, so some flags may differ there.

The first thing to learn is the trailing slash on the source:

$ rsync -a src dst/; find dst | sort
dst
dst/src
dst/src/a.txt
dst/src/skip.log
dst/src/sub
dst/src/sub/b.txt
$ rsync -a src/ dst/; find dst | sort
dst
dst/a.txt
dst/skip.log
dst/sub
dst/sub/b.txt

No slash: the directory comes along as dst/src. Slash: its contents land in dst. -a is archive mode, which recurses and keeps permissions and times. Running the second form twice shows why rsync wins for backups:

$ rsync -av src/ dst/
Transfer starting: 5 files

sent 193 bytes  received 20 bytes

It listed no file names, because nothing had changed. After I edited a.txt:

$ rsync -avn src/ dst/
Transfer starting: 5 files
a.txt

-n is a dry run: it shows what would be sent and sends nothing. Get in the habit of -avn first. The dangerous option is --delete, which makes the destination an exact mirror by removing files that no longer exist in the source. I deleted sub/b.txt from the source and previewed it:

$ rsync -avn --delete src/ dst/
Transfer starting: 4 files
deleting sub/b.txt

Mistype the source and an unreviewed --delete empties the destination. Exclusions use patterns:

$ rsync -av --exclude='*.log' src/ ex/
./
a.txt
sub/

skip.log stayed behind. Over SSH the form is rsync -av src/ me@server:/backup/src/. I tried a localhost target and ssh refused the connection, so that path is untested here. Compare with tar: tar bundles a snapshot, rsync updates a copy.

For long transfers

A transfer over a flaky link is where tmux or nohup earn their keep. Because rsync compares before sending, rerunning it after a drop picks up roughly where it stopped.

About the video

“Copying files remotely over SSH with SCP and Rsync” is a 10-minute 46-second video from 312 school, published June 2021. Its timeline lists: copying files remotely over SSH, scp (0:40), rsync (7:09) and rsync versus scp (9:41). The description says it covers syntax, demo examples and when to use which. I could not read a transcript. The channel is small and English-language. It does not teach key setup, which this lesson touches on only briefly. Veronica Explains’ rsync video on everyday flags is linked as a second view.

Where this fits

That finishes the everyday tools. The last group is about scripts that other people, and you in six months, can rely on, starting with portability between POSIX sh and Bash.

Check yourself

  1. 01What is the difference between rsync -a src dst/ and rsync -a src/ dst/?

    The first creates dst/src with everything inside. The second copies the contents of src straight into dst.

  2. 02You want to know what rsync --delete would remove before running it. What do you do?

    Add -n (--dry-run) and -v, for example rsync -avn --delete src/ dst/. It lists the files that would be deleted and changes nothing.

  3. 03Why is rsync better than scp -r for a second backup run?

    It compares the two sides and sends only the files that changed. My second local run listed no files, where scp -r would copy everything again.

  4. 04ssh complains about permissions on your private key. What is wrong?

    The key file is readable by others. Run chmod 600 on it, since ssh refuses keys other users could read.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark