Skip to content
Tech Atlas, home
Search
Esc
  • All roadmapsBrowse every learning path
  • ExploreFind a technology area
  • ProjectsGuided projects and builds
  • My AtlasYour learning dashboard
Lesson 5 of 6 · Lab · 25 min · revision 3

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

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 (755 or 750 are fine).
  • ~/.ssh: not writable by group or others; 700 is the recommended mode.
  • ~/.ssh/authorized_keys: not writable by group or others; 600 is recommended. A readable 644 is accepted. Upstream OpenSSH refuses a group-writable 664; 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), but 600 works everywhere.
  • The private key on the client: mode 600. If others can read it, the ssh client prints WARNING: UNPROTECTED PRIVATE KEY FILE!, ignores that key, and the login usually ends in Permission 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.

  1. 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.
Terminal
mkdir -p ~/.ssh && chmod 700 ~/.ssh
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_lab -C "linux-cli-lab"
Illustrative output
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]-----+
Terminal · check the modes
(cd ~/.ssh && stat -c "%A %n" id_ed25519_lab*)
Expected output
-rw------- id_ed25519_lab
-rw-r--r-- id_ed25519_lab.pub
  1. 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.
Terminal
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519_lab
Illustrative output
Agent pid 4177805
Enter passphrase for /home/learner/.ssh/id_ed25519_lab:
Identity added: /home/learner/.ssh/id_ed25519_lab (linux-cli-lab)
  1. Install the public key with ssh-copy-id: it logs in once with your password and appends the key to ~/.ssh/authorized_keys on the target. This is your first connection, so ssh asks about the host key. Before you type yes, run ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub on the target (for localhost, in a second terminal) and check that the SHA256: fingerprints match. After the 1 key(s) remain line, you are asked for your account password (learner@localhost's password:).
Terminal
ssh-copy-id -i ~/.ssh/id_ed25519_lab.pub localhost
Illustrative output (long lines wrapped)
/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.
  1. Give the host a short name. A Host entry in ~/.ssh/config stores address, user and key once, and ssh, scp and rsync all read it. IdentitiesOnly yes offers only this key, not every key in the agent. BatchMode=yes forbids password prompts, so if the test succeeds, the key did the work.
Terminal
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'
Expected output
key login works

A 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.

Terminal
ssh lab 'uname -sr; uptime'
Illustrative output
Linux 6.8.0-138-generic
 01:10:00 up 28 days, 12:38,  1 user,  load average: 3.75, 4.96, 5.07
  1. Make a small folder and copy it with rsync, dry run first: it prints the plan (even created directory), ends with (DRY RUN), and changes nothing. Then copy for real, change one file and sync again; the last run sends only index.html. site-copy/ has no leading /, so it lands in your home directory on the host.
Terminal
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/
Illustrative output (byte counts and speeds differ)
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
  1. Finish the round trip: pack the copy into a tar archive on the host, pull it back with scp, and list it locally. tar -tzf reads the whole archive, so a clean listing also shows that the download is complete. -q hides the progress meter that scp prints in a terminal, whose speed differs on every run, and sort makes the order the same on every filesystem.
Terminal
ssh lab 'tar -czf site.tar.gz -C site-copy .'
scp -q lab:site.tar.gz .
tar -tzf site.tar.gz | sort
Expected output
./
./css/
./css/style.css
./index.html

You 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

Practice check · 3 questions

SSH keys, host keys and rsync check

Three questions on key permissions, host keys and rsync.

Q1. Key login to `[email protected]` fails with `Permission denied (publickey)`. On the server, `ls -ld /home/deploy/.ssh` shows mode `0777`. Why does sshd refuse the key?
Q2. Your first `ssh` to a new VM prints `The authenticity of host ... can't be established` and a `SHA256:` fingerprint. What should you do before typing `yes`?
Q3. Why is `rsync -a` preferred over `scp -r` for an hourly sync of a large uploads directory to a backup server?
Practice only: not graded, not saved, not evidence.

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 Host entry with User, IdentityFile and IdentitiesOnly yes for each server you use often.
  • Once key login works, set PasswordAuthentication no in 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 with sudo sshd -t, then apply it with sudo systemctl reload ssh (the unit is sshd on Fedora and RHEL): the running server reads its configuration only at start or reload. sudo sshd -T | grep -i passwordauthentication shows 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 lab must end in Permission denied (publickey) without asking for a password (if the list in brackets still shows password or keyboard-interactive, the reload did not happen or KbdInteractiveAuthentication is still on), and ssh -o BatchMode=yes lab true from 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.

Sign in to mark reading complete

Prove this skill

8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.
Take the skill check