Chapter 10: Secure remote access
Copying files and saving connection settings
Once you can log in, you will want to move files between machines and to stop typing the same long ssh -i ... user@host over and over. SSH carries file transfers inside the same secure connection, and a small configuration file turns every server into a short name.
- Copy files and directories in both directions with
scp,sftpandrsync. - Explain when
rsyncis the better choice, and use-a,-nand the trailing slash correctly. - Define host aliases in
~/.ssh/config.
Host aliases: ~/.ssh/config
Instead of repeating options, give the server a nickname in ~/.ssh/config (mode 600). Each Host block applies to connections to that name:
[student@workstation ~]$ cat ~/.ssh/config
Host opsa
HostName servera
User ops
IdentityFile ~/.ssh/id_ops
IdentitiesOnly yes
[student@workstation ~]$ ssh opsa 'echo hello from $(hostname) as $USER'
hello from servera.lab.example.com as ops| Keyword | Means |
|---|---|
Host | The nickname you type |
HostName | The real name or address |
User | The account to log in as |
IdentityFile | Which private key to use |
IdentitiesOnly yes | Offer only that key (not every key in the agent) |
Port | A non-standard port |
The same alias works for every tool below, because they all use SSH.
scp: simple copies
scp copies like cp but between hosts. A remote path is written HOST:PATH; an empty path after the colon means the remote home directory. The first argument is the source, the last is the target:
[student@workstation ~]$ scp inventory.txt opsa: # upload to ~ops
[student@workstation ~]$ scp -r reports opsa:/tmp/ # a directory (-r)
[student@workstation ~]$ scp opsa:/etc/hostname ./servera-hostname.txt # download
[student@workstation ~]$ cat servera-hostname.txt
servera.lab.example.comscp is silent on success when not run on a terminal that shows progress. It overwrites without asking, so mind the target.
sftp: browse, then copy
sftp opens an interactive session with commands like ls, cd, get, put and bye, which is handy when you are not sure of the exact file name:
[student@workstation ~]$ sftp opsa
Connected to opsa.
sftp> cd /tmp
sftp> ls reports
reports/a.txt reports/b.txt
sftp> get reports/a.txt
Fetching /tmp/reports/a.txt to a.txt
sftp> byersync: copy only what changed
rsync compares source and target and sends only the differences, so it is the right tool for repeated copies and for backups.
[student@workstation ~]$ rsync -av reports/ opsa:rsyncdir/
sending incremental file list
created directory rsyncdir
./
a.txt
b.txt
sent 191 bytes received 88 bytes 558.00 bytes/sec
total size is 6 speedup is 0.02
[student@workstation ~]$ rsync -av reports/ opsa:rsyncdir/
sending incremental file list
sent 117 bytes received 19 bytes 272.00 bytes/sec
total size is 6 speedup is 0.04The second run transferred nothing. Key options:
| Option | Does |
|---|---|
-a | Archive: recurse and preserve permissions, owners, times and links |
-v | Verbose: list the files |
-n | Dry run: show what would happen, change nothing |
--delete | Remove files at the target that are gone at the source (mirror) |
-z | Compress during transfer |
--progress | Show progress for large files |
The trailing slash matters. rsync -av reports opsa:dir/ creates dir/reports/... (the directory itself), while rsync -av reports/ opsa:dir/ puts the contents directly in dir/. Always rehearse a destructive mirror with -n first:
[student@workstation ~]$ rsync -avn --delete reports/ opsa:rsyncdir/Choosing a transfer tool: Select a tool to see when it fits.
Try it
Check your understanding
- Which command copies the file notes.txt to ops's home directory on servera?
- What does rsync do that scp does not?
- In rsync, what is the difference between reports and reports/ as the source?
- What is ~/.ssh/config used for?
- Which command starts an interactive file transfer session?
- rsync -avn --delete a/ host:b/ does what?