Remote work over SSH: keys, tar archives, scp and rsync
Log in to a host with an SSH key that has a passphrase, check the host key, run remote commands through a ~/.ssh/config alias, then copy files with rsync (dry run first) and bring a tar archive back with scp.
Contentsand lesson path
On this page
- SSH keys, passphrases and file permissions
- Packing files with tar
- Copying over SSH: scp and rsync
- Lab: key login, a config alias and an rsync round trip
- Check your understanding
- Apply it
- Review prompt
Lessons of Linux CLI
- Finding your way around the filesystem: globs, nano, vim and sudoedit Concept · 20 min
- Who may read this file? Users, groups and mode bits Worked example · 20 min
- Answering questions from an access log with pipelines Lab · 25 min
- Processes, resource usage, signals and the environment a program sees Concept · 20 min
- Remote work over SSH: keys, tar archives, scp and rsync Lab · 25 min
- Check: diagnose a misbehaving host from the shell Knowledge check · 8 questions
What you will learn
- Inspect and control processes and environment variables, and work on remote hosts over SSH with key-based authentication.
01. SSH keys, passphrases and file permissions
You manage almost every Linux server through SSH (Secure Shell), an encrypted connection that gives you a remote terminal. Passwords can be guessed or leaked, so servers use a key pair instead. The private key stays on your machine; the server stores the matching public key in the account's ~/.ssh/authorized_keys. At login the server sends a challenge that only the private key can answer, so the private key never crosses the network. Once key login works, you should switch passwords off (Apply it shows how).
Prefer Ed25519 for new keys: they are short and fast, with strength comparable to RSA-3072 (if you must use RSA, use at least 3072 bits). When ssh-keygen asks for a passphrase, set one: it encrypts the private key on disk, so a copied key file is useless without it. Load the key once per session into ssh-agent with ssh-add; the agent then answers login challenges, so you do not retype the passphrase.
ssh-keygen -f ~/.ssh/id_ed25519_lab writes two files: the private key id_ed25519_lab, which never leaves this machine (no chat, no Git), and the public key id_ed25519_lab.pub, the only part you copy to servers.
OpenSSH checks file modes and ownership on both ends. On the server, StrictModes yes (the default in sshd_config) makes sshd refuse key login if your home directory, ~/.ssh or authorized_keys is not owned by you or is writable by group or others, because another local user could then add their own key:
- Home directory: not writable by group or others (
755or750are fine). ~/.ssh: not writable by group or others;700is the recommended mode.~/.ssh/authorized_keys: not writable by group or others;600is recommended. A readable644is accepted. Upstream OpenSSH refuses a group-writable664; Debian and Ubuntu patch sshd to accept it when the group is your private group with no other members (the default for new users there), but600works everywhere.- The private key on the client: mode
600. If others can read it, thesshclient printsWARNING: UNPROTECTED PRIVATE KEY FILE!, ignores that key, and the login usually ends inPermission denied (publickey).
SSH also checks the server. Every server has a host key, and your client remembers it in ~/.ssh/known_hosts. On the first connection there is nothing to compare with, so ssh shows the key's fingerprint and asks whether to trust it. Typing yes without checking is how a fake server gets trusted: compare the fingerprint with one from the server's administrator, or with ssh-keygen -lf run on the server. If a known host later shows a different key, OpenSSH prints WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! and refuses to connect, because a man-in-the-middle attack looks exactly like that.
02. Packing files with tar
Before you move many files to another machine, pack them into one archive. tar bundles files and directories, with their permissions and timestamps, into one file; -z adds gzip compression (.tar.gz), and GNU tar also has --zstd. -c creates, -x extracts and -t lists the contents; -f FILE names the archive and -v prints each file. -C DIR changes into DIR first: tar -czf logs.tar.gz -C /var/log/app . stores ./access.log rather than the full path, and tar -xzf logs.tar.gz -C /restore extracts into /restore. Listing with tar -tzf reads the whole archive, a cheap check that it is complete.
03. Copying over SSH: scp and rsync
Both tools copy files through SSH, so they use the same keys, ~/.ssh/config aliases and host-key checks as ssh. scp copies whole files every time: fine for one archive. rsync compares both sides first, skips files whose size and modification time match, and for changed files sends only the parts that differ. It must be installed on both machines.
rsync -av is the usual start: -a (archive mode) recurses and keeps links, permissions, timestamps and, where allowed, ownership; -v lists what is sent. -n (--dry-run) shows what would happen without changing anything: use it whenever you are unsure of the paths, and before any --delete, which removes destination files that are missing in the source.
Watch the trailing slash on the source: rsync -a site /backup/ creates /backup/site/, while rsync -a site/ /backup/ copies only the contents of site. For big scheduled jobs, --partial lets the next run resume a broken file and --bwlimit=10000 caps the rate at 10,000 KiB/s.
04. Lab: key login, a config alias and an rsync round trip
You will create a key with a passphrase, install it on a host, accept the host key after checking it, give the host a short alias in ~/.ssh/config, run commands on it, copy a folder with rsync (dry run first), and fetch an archive back with scp.
Your target host. Use a machine you control, never someone else's server. The simplest is your own Linux machine or VM: install the server with sudo apt install openssh-server rsync (Debian or Ubuntu), check it with systemctl status ssh, and the lab connects to localhost as your own user. For a separate VM, use its address instead of localhost and add a User line in step 4.
The outputs were recorded with OpenSSH 8.9p1 (Ubuntu 22.04) and rsync 3.2.7 against a test SSH server on the same machine. Current releases are newer (OpenSSH 10.6 and rsync 3.5 as of November 2026); the commands and options in this lab work the same way there, but the wording of some messages may differ. Fingerprints, host names, paths and timings differ on yours, so those blocks are marked Illustrative output.
- Create a separate key for this lab, so you do not touch any key you already use. Type a passphrase when asked; nothing is shown while you type.
mkdir -p ~/.ssh && chmod 700 ~/.ssh
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_lab -C "linux-cli-lab"Generating public/private ed25519 key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/learner/.ssh/id_ed25519_lab
Your public key has been saved in /home/learner/.ssh/id_ed25519_lab.pub
The key fingerprint is:
SHA256:UOIATSMza6pOY5ba53PLExDxMCTKfeaMjRuRCwCvpv4 linux-cli-lab
The key's randomart image is:
+--[ED25519 256]--+
|+ *=O.. . |
|oo.*+B o |
|.o++ ++ |
| +. % . |
|o. = = S |
|+ . o . |
|o* . . |
|B.. o.o |
|.ooE.ooo |
+----[SHA256]-----+(cd ~/.ssh && stat -c "%A %n" id_ed25519_lab*)-rw------- id_ed25519_lab
-rw-r--r-- id_ed25519_lab.pub- Start an agent for this terminal and give it the key. You type the passphrase once here; later connections from this terminal use the agent.
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519_labAgent pid 4177805
Enter passphrase for /home/learner/.ssh/id_ed25519_lab:
Identity added: /home/learner/.ssh/id_ed25519_lab (linux-cli-lab)- Install the public key with
ssh-copy-id: it logs in once with your password and appends the key to~/.ssh/authorized_keyson the target. This is your first connection, sosshasks about the host key. Before you typeyes, runssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pubon the target (forlocalhost, in a second terminal) and check that theSHA256:fingerprints match. After the1 key(s) remainline, you are asked for your account password (learner@localhost's password:).
ssh-copy-id -i ~/.ssh/id_ed25519_lab.pub localhost/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed:
"/home/learner/.ssh/id_ed25519_lab.pub"
The authenticity of host 'localhost (127.0.0.1)' can't be established.
ED25519 key fingerprint is SHA256:7/7dCdJ43eed3PRHaHzGi5EIUqkHAzmvHbSI2toCFJs.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any
that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now
it is to install the new keys
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'localhost'"
and check to make sure that only the key(s) you wanted were added.- Give the host a short name. A
Hostentry in~/.ssh/configstores address, user and key once, andssh,scpandrsyncall read it.IdentitiesOnly yesoffers only this key, not every key in the agent.BatchMode=yesforbids password prompts, so if the test succeeds, the key did the work.
cat >> ~/.ssh/config <<'EOF'
Host lab
HostName localhost
IdentityFile ~/.ssh/id_ed25519_lab
IdentitiesOnly yes
EOF
chmod 600 ~/.ssh/config
ssh -o BatchMode=yes lab true && echo 'key login works'key login worksA quoted command after the host name runs on the remote host, and its output comes back to you. Single quotes matter: in ssh lab 'echo $HOME' the remote shell expands $HOME; with double quotes your local shell would expand it first.
ssh lab 'uname -sr; uptime'Linux 6.8.0-138-generic
01:10:00 up 28 days, 12:38, 1 user, load average: 3.75, 4.96, 5.07- Make a small folder and copy it with
rsync, dry run first: it prints the plan (evencreated directory), ends with(DRY RUN), and changes nothing. Then copy for real, change one file and sync again; the last run sends onlyindex.html.site-copy/has no leading/, so it lands in your home directory on the host.
mkdir -p ~/ssh-lab/site/css && cd ~/ssh-lab
printf '<h1>Hello</h1>\n' > site/index.html
printf 'h1 { color: navy; }\n' > site/css/style.css
rsync -avn site/ lab:site-copy/
ssh lab 'ls site-copy'
rsync -av site/ lab:site-copy/
printf '<h1>Hello again</h1>\n' > site/index.html
rsync -av site/ lab:site-copy/sending incremental file list
created directory site-copy
./
index.html
css/
css/style.css
sent 153 bytes received 61 bytes 428.00 bytes/sec
total size is 35 speedup is 0.16 (DRY RUN)
ls: cannot access 'site-copy': No such file or directory
sending incremental file list
created directory site-copy
./
index.html
css/
css/style.css
sent 276 bytes received 101 bytes 251.33 bytes/sec
total size is 35 speedup is 0.09
sending incremental file list
index.html
sent 213 bytes received 42 bytes 510.00 bytes/sec
total size is 41 speedup is 0.16- Finish the round trip: pack the copy into a tar archive on the host, pull it back with
scp, and list it locally.tar -tzfreads the whole archive, so a clean listing also shows that the download is complete.-qhides the progress meter thatscpprints in a terminal, whose speed differs on every run, andsortmakes the order the same on every filesystem.
ssh lab 'tar -czf site.tar.gz -C site-copy .'
scp -q lab:site.tar.gz .
tar -tzf site.tar.gz | sort./
./css/
./css/style.css
./index.htmlYou now have a key login without password prompts, an alias every SSH tool understands, and a verified round trip. To tidy up, run ssh lab 'rm -r site-copy site.tar.gz' and delete the line ending in linux-cli-lab from ~/.ssh/authorized_keys on the host.
05. Check your understanding
06. Apply it
Repeat the lab on the practice VM for this skill:
- Use one key per purpose (a laptop key, a separate CI deploy key), with a passphrase unless a machine must use it unattended.
- Add a
Hostentry withUser,IdentityFileandIdentitiesOnly yesfor each server you use often. - Once key login works, set
PasswordAuthentication noin an early drop-in such as/etc/ssh/sshd_config.d/00-no-passwords.conf(sshd keeps the first value it reads). Check the syntax withsudo sshd -t, then apply it withsudo systemctl reload ssh(the unit issshdon Fedora and RHEL): the running server reads its configuration only at start or reload.sudo sshd -T | grep -i passwordauthenticationshows what the files on disk now say. Keep your current session open and, from a second terminal, prove that passwords are refused:ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password labmust end inPermission denied (publickey)without asking for a password (if the list in brackets still showspasswordorkeyboard-interactive, the reload did not happen orKbdInteractiveAuthenticationis still on), andssh -o BatchMode=yes lab truefrom step 4 must still work.
07. Review prompt
Without looking back: which file holds the public key on the server, and which holds the server's host key on your machine? What does the passphrase protect, and what does ssh-agent save you from? What does BatchMode=yes prove in step 4? Explain why rsync -avn is worth running first, and what the trailing slash in site/ changes.
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.