SSH Keys & ssh-agent
ssh & scp showed how to log in with a key. On servers, keys are the only way in (passwords get switched off in the next lesson), so it is worth knowing them properly: which key type, how to protect the private half, how to restrict what a key may do on the server, and how the agent keeps a passphrase-protected key usable.
Generating a key
Use Ed25519 unless something old forces RSA (then -t rsa -b 4096). Always set a passphrase for keys that humans use; it encrypts the private key file, so a copied laptop disk doesn't hand out your servers.
ssh-keygen -q -t ed25519 -C "ada@laptop-2026" -N "correct horse battery" -f id_ed25519
ls id_ed25519*
stat -c '%a %n' id_ed25519 id_ed25519.pub
cut -d' ' -f1,3 id_ed25519.pub
head -1 id_ed25519
ssh-keygen -y -P "wrong" -f id_ed25519 2>&1
ssh-keygen -y -P "correct horse battery" -f id_ed25519 | cut -d' ' -f1id_ed25519
id_ed25519.pub
600 id_ed25519
644 id_ed25519.pub
ssh-ed25519 ada@laptop-2026
-----BEGIN OPENSSH PRIVATE KEY-----
Load key "id_ed25519": incorrect passphrase supplied to decrypt private key
ssh-ed25519
- The private key is created with mode
600. SSH refuses to use a private key that others can read ("UNPROTECTED PRIVATE KEY FILE!"), andsshdignores anauthorized_keysfile or home directory that is writable by others (StrictModes yes). - The public key is one line: type, base64 key, comment. The comment is only a label; make it say whose key it is and where it lives.
ssh-keygen -yderives the public key from the private one, which only works with the right passphrase.
The fingerprint (ssh-keygen -lf id_ed25519.pub, SHA256:…) identifies a key in logs and in ssh-add -l; it is different for every generated key, which is why the sessions don't print it.
authorized_keys: one line per allowed key, with options
On the server, ~/.ssh/authorized_keys of the target account lists the public keys that may log in. Each line can start with options that restrict that key:
# a human: plain key
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIK0x... ada@laptop-2026
# the backup server: only from its address, only one command, nothing else
from="10.0.0.30",command="/usr/local/bin/backup-receive",restrict ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPq9... backup@vault1
# CI: may only open a tunnel to the app port
restrict,port-forwarding,permitopen="127.0.0.1:8000" ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIM3c... ci@runner| Option | Effect |
|---|---|
restrict |
turn off everything optional (PTY, forwarding, agent, X11, ~/.ssh/rc); re-enable single features after it |
command="…" |
always run this command, whatever the client asked for (the request is in $SSH_ORIGINAL_COMMAND) |
from="10.0.0.0/24,!10.0.0.9" |
only accept the key from these addresses |
expiry-time="20261231" |
the key stops working after this date |
permitopen="host:port" |
limit port forwarding to these destinations |
Automation keys (backup, deploy, CI) should always have restrict plus a from= and, where possible, a command=. A stolen backup key then only lets the thief upload a backup from the backup server's address.
ssh-copy-id -i id_ed25519.pub admin@web1 appends a key with the right permissions; in automation you manage the whole file (or use SSH certificates and no authorized_keys at all).
ssh-agent: type the passphrase once
The agent holds decrypted keys in memory and signs login challenges for SSH, so the private key never leaves it and you type the passphrase once per session. Desktop environments and macOS start one automatically; on a plain shell you start it yourself. Here it runs for real (the agent only talks over a local Unix socket):
ssh-keygen -q -t ed25519 -C "ada@laptop-2026" -N "" -f id_ed25519
eval "$(ssh-agent -s)" > /dev/null
ssh-add -t 1h id_ed25519 2>&1
ssh-add -l | awk '{print $1, $3, $4}'
ssh-add -D 2>&1
ssh-add -l
ssh-agent -k > /dev/null && echo "agent stopped"Identity added: id_ed25519 (ada@laptop-2026)
Lifetime set to 3600 seconds
256 ada@laptop-2026 (ED25519)
All identities removed.
The agent has no identities.
agent stopped
-t 1h makes the agent forget the key after an hour: a good default for admin keys. ssh-add -l lists key sizes, fingerprints and comments; "The agent has no identities." means it is running but empty.
Agent forwarding vs ProxyJump
To reach a server behind a bastion (jump host), you might be tempted to log in to the bastion and SSH onward from there, with ForwardAgent yes so the bastion can use your keys. Don't: while you are connected, root on the bastion can use your forwarded agent to log in anywhere your key works. ProxyJump instead tunnels your connection through the bastion; authentication to the inner server happens on your laptop, end to end.
ssh -G prints the client's effective configuration for a host without connecting, which is the way to check a ~/.ssh/config:
mkdir -p .ssh && chmod 700 .ssh
cat > .ssh/config <<'EOF'
Host bastion
HostName bastion.example.com
User ada
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
Host web? db?
HostName %h.internal.example.com
User admin
ProxyJump bastion
ForwardAgent no
Host *
ServerAliveInterval 30
HashKnownHosts yes
EOF
ssh -G web1 2>/dev/null | grep -E '^(hostname|user|proxyjump|forwardagent|serveraliveinterval) '
echo "---"
ssh -G bastion 2>/dev/null | grep -E '^(hostname|user|identitiesonly|proxyjump) 'user admin
hostname web1.internal.example.com
serveraliveinterval 30
forwardagent no
proxyjump bastion
---
user ada
hostname bastion.example.com
identitiesonly yes
The first matching value wins for each option, so put specific Host blocks before Host *. ssh -J bastion admin@web1 does the same jump ad hoc.
Test yourself
Why should humans protect their private keys with a passphrase?
- Servers require it
- The private key file is encrypted, so a copied disk or backup doesn't give access to your servers
- It makes logins faster
- It changes the fingerprint daily
Which authorized_keys options make an automation key least dangerous if stolen?
- none
restrict,from=with the client's address, and a fixedcommand=permit-ptyagent-forwarding
Why is ProxyJump safer than agent forwarding to reach servers behind a bastion?
- It is faster
- Your keys never become usable on the bastion; the connection is only tunnelled through it
- It disables encryption on the bastion
- It works without keys