Lab 16: SSH — Key Auth & Config

Time: 30 minutes | Level: Practitioner | Docker: docker run -it --rm ubuntu:22.04 bash


Overview

SSH key authentication is the gold standard for secure remote access. In this lab you will generate ed25519 keypairs, configure the SSH client config file, understand authorized_keys, manage known_hosts, and simulate scp file transfers — all verified in a live Docker container.

Prerequisites: Docker installed, Labs 01–15 completed.


Step 1: Install SSH Tools & Generate an ed25519 Key

ed25519 keys are shorter, faster, and more secure than RSA-2048.

docker run -it --rm ubuntu:22.04 bash
apt-get update -qq && apt-get install -y -qq openssh-client

Generate a key non-interactively:

ssh-keygen -t ed25519 -N "" -f ~/.ssh/id_ed25519 -C "[email protected]"
Flag
Meaning

-t ed25519

Key type (ed25519 = Edwards-curve DSA)

-N ""

Empty passphrase (use a passphrase in production!)

-f ~/.ssh/id_ed25519

Output file path

Comment (typically user@host)

ls -la ~/.ssh/
cat ~/.ssh/id_ed25519.pub
ssh-keygen -l -f ~/.ssh/id_ed25519.pub

📸 Verified Output:

💡 Always use ed25519 for new keys. If you must use RSA for compatibility, use at least 4096 bits: ssh-keygen -t rsa -b 4096. Never use DSA or ECDSA-256.


Step 2: Understand Key Pair Structure

A keypair has two files:

📸 Verified Output:

Critical permissions:

File
Required Permission
Breaks SSH if wrong?

~/.ssh/

700 (drwx------)

✅ Yes

~/.ssh/id_ed25519

600 (-rw-------)

✅ Yes

~/.ssh/id_ed25519.pub

644 (-rw-r--r--)

❌ No

~/.ssh/authorized_keys

600 (-rw-------)

✅ Yes

💡 SSH is strict about permissions. If ~/.ssh/ is world-readable, SSH will refuse to use your key with "bad permissions" error. Fix with chmod 700 ~/.ssh && chmod 600 ~/.ssh/id_ed25519.


Step 3: Configure authorized_keys (Server Side)

authorized_keys is how servers grant access — it lists public keys that may log in.

📸 Verified Output:

ssh-copy-id equivalent:

💡 Multiple keys in authorized_keys: Each line is one public key. You can have many — one per team member, one per device. Comment lines start with #. Prefix keys with options like command="backup.sh" to restrict what a key can do.


Step 4: Create ~/.ssh/config for Host Aliases

The SSH config file saves you from typing long commands every time.

📸 Verified Output:

Config directives explained:

Directive
Purpose

Host

Alias (what you type in ssh <alias>)

HostName

Real hostname or IP

User

Remote username

IdentityFile

Which private key to use

Port

SSH port (default 22)

ProxyJump

Jump through another host

ServerAliveInterval

Send keepalive every N seconds

💡 With this config, ssh webprod expands to ssh -i ~/.ssh/id_ed25519 -p 22 [email protected] — much less typing. Use ssh -G webprod to see all resolved options for a host.


Step 5: SSH Agent — Managing Keys in Memory

ssh-agent caches your decrypted private keys so you don't retype passphrases.

📸 Verified Output:

Agent commands:

Command
Action

ssh-add ~/.ssh/id_ed25519

Add key to agent

ssh-add -l

List loaded keys

ssh-add -d ~/.ssh/id_ed25519

Remove specific key

ssh-add -D

Remove all keys

ssh-add -t 3600

Add with 1-hour expiry

💡 AddKeysToAgent yes in ~/.ssh/config automatically adds keys when first used, so you don't need to manually run ssh-add. On macOS, also set UseKeychain yes to store passphrases in the system keychain.


Step 6: known_hosts — Preventing MITM Attacks

known_hosts records server fingerprints so you can detect if a server's identity changes.

📸 Verified Output:

💡 StrictHostKeyChecking yes (set in ~/.ssh/config Host * block) refuses connections to unknown hosts instead of prompting. Use this in scripts and automation to catch MITM attacks. Use ssh-keyscan to pre-populate known_hosts before automation runs.


Step 7: SCP — Secure File Copy

scp uses SSH to copy files between hosts. With your ~/.ssh/config, the aliases work here too.

📸 Verified Output:

💡 Prefer rsync over scp for anything more than a single file. rsync skips unchanged files, supports resuming interrupted transfers, and preserves permissions. scp -3 routes through your local machine when copying between two remote hosts, which is slower than direct rsync.


Step 8: Capstone — Build a Complete SSH Setup Script

Scenario: You're onboarding a new server. Automate the entire SSH security setup.

📸 Verified Output:

💡 Security hardening checklist: Disable password auth (PasswordAuthentication no in /etc/ssh/sshd_config), disable root login (PermitRootLogin no), restrict to key auth only, use AllowUsers to whitelist users, and change the default port from 22 to reduce automated scanning noise.


Summary

Concept
Command / File
Purpose

Generate key

ssh-keygen -t ed25519 -N "" -f ~/.ssh/id_ed25519

Create keypair

Deploy key

ssh-copy-id user@host

Append pubkey to authorized_keys

authorized_keys

~/.ssh/authorized_keys

Server: allowed public keys

SSH config

~/.ssh/config

Client: host aliases & options

SSH agent

eval $(ssh-agent -s); ssh-add

Cache decrypted keys

known_hosts

~/.ssh/known_hosts

Server fingerprint verification

Secure copy

scp file user@host:/path/

Copy files over SSH

View config

ssh -G hostname

Show resolved SSH options

Scan fingerprint

ssh-keyscan -H host

Get host's public key

Remove host

ssh-keygen -R hostname

Remove from known_hosts

Last updated