20.2. Managing User Authentication
Public-key authentication replaces a shared secret the server stores (passwords) with a split one: the client proves possession of a private key, the server holds only the matching public half. This section builds that from key creation through revocation, and adds the agent that keeps passphrase-protected keys usable without typing the passphrase for every connection.
20.2.1. Creating Modern Key Pairs and Protecting Private Keys
The key pair generated in the chapter setup is an Ed25519 key - the default recommendation for new keys. The types OpenSSH accepts today:
| Key type | Characteristics | When to choose it |
|---|---|---|
ed25519 |
Small keys, fast signatures, no parameter choices to get wrong | The default for new keys on OpenSSH-to-OpenSSH systems |
RSA (rsa-sha2-256/rsa-sha2-512) |
Larger keys and signatures; 3072 bits is the ssh-keygen default, and 1024 its hard minimum |
Older or third-party servers that reject Ed25519 |
ecdsa (NIST curves) |
Small and fast, but NIST-curve trust questions and a history of implementation pitfalls | Rarely preferable to Ed25519 |
ed25519-sk / ecdsa-sk |
The private key lives in a FIDO2 hardware token (Section 20.3.4) | Keys that cannot be copied off a device |
OpenSSH 10 removed DSA keys entirely; any ssh-dss key still in use is
dead weight to retire, not something to keep accepting.
The setup command deserves a second look now that the parts matter. Run
with ~/.ssh as the working directory,
ssh-keygen -t ed25519 -a 64 -C "john@sshadmin" -f id_ed25519 is the
command from the chapter setup, and its transcript appears there. The
options:
ssh-keygen option |
Description |
|---|---|
-t type |
Key type: ed25519, rsa, ecdsa, or the -sk hardware variants. |
-a rounds |
KDF rounds for the passphrase. The default 16 is fast; 64 makes each wrong-passphrase guess correspondingly more expensive. |
-C comment |
Comment stored with the public key - a label for humans. |
-f file |
Key file name; without it, ssh-keygen prompts with a default. |
-N passphrase |
Supply the passphrase non-interactively; used by scripts, not recommended for personal keys (it lands in shell history). |
-p -f file |
Change a key's passphrase interactively. |
Two files result, with modes worth checking:
$ stat -c "%a %U:%G %n" ~/.ssh ~/.ssh/id_ed25519 ~/.ssh/id_ed25519.pub
700 john:john /home/john/.ssh
600 john:john /home/john/.ssh/id_ed25519
644 john:john /home/john/.ssh/id_ed25519.pub
The private key is 600 - readable by john alone - and the .ssh
directory 700. These modes are not cosmetic: sshd refuses
world-accessible key material server-side (Section 20.2.2), and other
users of the workstation could read anything looser.
The passphrase is not a login password; it encrypts the private key file
itself. Reading the key without it fails, as ssh-keygen -y - which
prints the public half from a private key - demonstrates with standard
input redirected from /dev/null so there is nothing to read instead of
a prompt:
$ ssh-keygen -y -f ~/.ssh/id_ed25519 < /dev/null
Enter passphrase for "/home/john/.ssh/id_ed25519": Load key "/home/john/.ssh/id_ed25519": incorrect passphrase supplied to decrypt private key
The key refused to load. Without the redirection the same command sits
at a passphrase prompt instead; with it, the attempt simply fails - the
behavior a script or a stolen key file faces. An attacker who obtains
id_ed25519 still has to defeat the KDF before the key is usable, which
is what -a 64 bought. To change or add a passphrase later, run
ssh-keygen -p -f ~/.ssh/id_ed25519; it prompts for the old passphrase
(if any) and the new one, and rewrites the file in place.
20.2.2. Installing Public Keys and Checking File Permissions
A server accepts a key when the public half appears in the target
account's ~/.ssh/authorized_keys, one key per line. The natural way to
install one is ssh-copy-id, which uses existing credentials to append
the new public key. Create a second key for the exercise, this time
without a passphrase (press Enter at both passphrase prompts):
$ ssh-keygen -t ed25519 -C "john@sshadmin-new" -f ~/.ssh/id_ed25519_new
Generating public/private ed25519 key pair.
Your identification has been saved in /home/john/.ssh/id_ed25519_new
Your public key has been saved in /home/john/.ssh/id_ed25519_new.pub
The key fingerprint is:
SHA256:WM5vsr1EtTsWweRDHNQBnFpIVXKj/8M6cceM9pB85nk john@sshadmin-new
The key's randomart image is:
+--[ED25519 256]--+
| ..*O*=. |
| .==+.. |
| . oB |
| = .. = |
| . S . o..= |
| o +Bo*|
| . + +.oO+|
| * . o..E|
| . o . .. .|
| . o. .. .|
+----[SHA256]-----+
The randomart figure exists so a human can compare two fingerprints at a glance; its slightly uneven rows are exactly as ssh-keygen printed them.
Then push the public half to sshserver. The obvious form, without options, quietly installs nothing here:
$ ssh-copy-id -i ~/.ssh/id_ed25519_new.pub sshserver
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/john/.ssh/id_ed25519_new.pub"
/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: WARNING: All keys were skipped because they already exist on the remote system.
(if you think this is a mistake, you may want to use -f option)
Without -f, ssh-copy-id first tries to log in with the new key and
treats a successful login as proof that the key is already installed.
The try is an ordinary ssh connection, so the agent's keys are offered
alongside the named one - and the agent already holds a key sshserver
accepts. The probe therefore succeeded, the tool concluded the new key
was present, and nothing was written. Repeat with -f, which skips the
probe and appends directly:
$ ssh-copy-id -f -i ~/.ssh/id_ed25519_new.pub sshserver
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/john/.ssh/id_ed25519_new.pub"
Number of key(s) added: 1
Now try logging into the machine, with: "ssh -i 'sshserver'"
and check to make sure that only the key(s) you wanted were added.
The key was written by this attempt; the probe above wrote nothing. The advice in the last line is the right next step: verify that exactly the intended key works, and nothing else changed:
$ ssh -i ~/.ssh/id_ed25519_new sshserver hostname
sshserver
The named key was offered first and accepted. On sshserver as john, the result looks like this:
$ stat -c "%a %U:%G %n" ~/.ssh ~/.ssh/authorized_keys
700 john:john /home/john/.ssh
600 john:john /home/john/.ssh/authorized_keys
sshd enforces those modes through its StrictModes default: it refuses
to honor .ssh or authorized_keys that is writable by anyone but the
owner, because a group- or world-writable file would let any local user
grant themselves a key. The failure is instructive. Loosen the
directory on sshserver:
$ chmod 777 ~/.ssh
The command prints nothing. Back on sshadmin, the same login that just worked is refused - to test the key in isolation, the probe below skips the client configuration and the agent so the only credential offered is the key under test:
$ ssh -F /dev/null -o IdentitiesOnly=yes -o IdentityAgent=none -o ProxyJump=john@192.168.220.30 -i ~/.ssh/id_ed25519_new sshserver hostname
john@sshserver: Permission denied (publickey).
Each option in that probe has a job, and the pattern recurs wherever this
chapter needs to prove a single credential's behavior: -F /dev/null
ignores the client configuration so its IdentityFile cannot answer as a
fallback; IdentitiesOnly and IdentityAgent=none keep the agent out of
the offer; ProxyJump spelled out replaces the route the configuration
would have supplied. In ordinary use none of this is typed - but without
it here, the agent's working key would mask the failure being tested.
Restore the mode on sshserver - silent again:
$ chmod 700 ~/.ssh
Then, from sshadmin, confirm the key works again:
$ ssh -i ~/.ssh/id_ed25519_new sshserver hostname
sshserver
The permission check runs on the server, in the client's name: sshd
verified that the path to john's authorized_keys is owned properly
before honoring it, and refused everything while it was not.
20.2.3. SSH Agents and Agent Forwarding Boundaries
A passphrase-protected key is inconvenient to type at every connection,
and handing the decrypted key to every process is worse. The agent solves
both: it holds decrypted keys in memory and signs challenges on request,
over a UNIX socket named by SSH_AUTH_SOCK. The setup step
eval "$(ssh-agent)" started one and exported the variable; ssh-add
loaded the key after asking for its passphrase once. Desktop sessions
normally start an agent automatically, so the explicit form is needed
mainly on servers and minimal systems. List what the agent holds:
$ ssh-add -l
256 SHA256:qyMz9bqiKx9uB05niCGdzTH6kGsN8K/Kge030pr7zig john@sshadmin (ED25519)
ssh-add option |
Description |
|---|---|
-l / -L |
List loaded keys' fingerprints / full public keys. |
-d file |
Remove one key from the agent. |
-D |
Remove all keys from the agent. |
-t lifetime |
Load a key for a bounded time only. |
The agent never gives the private key out; it only signs. ssh clients find the agent through the environment variable, offer its keys during authentication, and the passphrase never leaves the moment of loading. That property is also the agent's limit: anyone who can reach the socket while it holds a usable key can ask it to sign. Two consequences follow in a networked workflow.
First, agent forwarding is off by default. With -A, the remote host
receives a socket that relays signing requests back to the real agent:
$ ssh -A sshserver ssh-add -l
256 SHA256:qyMz9bqiKx9uB05niCGdzTH6kGsN8K/Kge030pr7zig john@sshadmin (ED25519)
Without it, the remote side has no agent at all:
$ ssh sshserver ssh-add -l
Could not open a connection to your authentication agent.
Second, a forwarded socket is exactly the reach-anyone-can-sign problem
moved onto another host: root on sshserver, or any compromise of
sshserver while the session is open, can ask john's agent to sign
authentications - which is why forwarding belongs only on hosts trusted
as much as the workstation itself, and why it should end when the session
does. The jump-host pattern of this chapter exists partly to avoid it:
with ProxyJump, john's client authenticates directly to the final
server through an encrypted relay (Section 20.5.2), and no agent
material ever touches the intermediate host. Reach a host through a
jump; forward an agent only when there is no such path and the
destination is fully trusted.
20.2.4. Revoking Keys and Rotating Credentials
Keys accumulate: one per host role, per automation job, per former
colleague. Two operations keep the set honest - removing a key's
authorization, and replacing one key with another. They differ in one
important way: removal from authorized_keys is per-host, while an
sshd key revocation list (KRL) lets one file reject a key or a
certificate everywhere it is deployed.
| Method | Where it acts | What it covers | Notes |
|---|---|---|---|
Delete the authorized_keys line |
One account on one host | That key on that host | The immediate, obvious fix; fine for one server |
RevokedKeys KRL |
Every host pointed at the KRL file | Listed keys, or certificate serials for a CA | One distribution reaches the fleet; the key is refused even if still listed in authorized_keys |
Watch both work. First, a key whose revocation will be demonstrated:
create it and install its public half on sshserver the Section 20.2.2
way (ssh-copy-id -i ~/.ssh/id_ed25519_revoke.pub sshserver), then
confirm it authenticates:
$ ssh-keygen -t ed25519 -C "john@sshadmin-revoke" -f ~/.ssh/id_ed25519_revoke
Generating public/private ed25519 key pair.
Your identification has been saved in /home/john/.ssh/id_ed25519_revoke
Your public key has been saved in /home/john/.ssh/id_ed25519_revoke.pub
The key fingerprint is:
SHA256:lCJ373MvMgEwfnvGciSM2/fpWEK/ljwjstkrVGqJE2I john@sshadmin-revoke
The key's randomart image is:
+--[ED25519 256]--+
| |
| o . |
| ..o=+ |
| Eo++=.o |
| . . *SXo |
| + OoB. |
| + *+++o |
| oo+*Xo |
| o++Bo+. |
+----[SHA256]-----+
$ ssh -i ~/.ssh/id_ed25519_revoke sshserver hostname
sshserver
Build a KRL naming it:
$ ssh-keygen -k -f ~/debian-handbook/ssh/revoked-keys.krl ~/.ssh/id_ed25519_revoke.pub
Revoking from /home/john/.ssh/id_ed25519_revoke.pub
A KRL is a compact binary list; ssh-keygen -k creates or extends one.
Copy it to sshserver (any trusted channel; Section 20.5.3 shows the
SSH-path way) and point a test instance of sshd at it. The practice
instance runs on port 2222 with its own configuration and pid file, so
nothing here touches the working service on port 22 - the same
second-instance discipline Section 20.4.4 formalizes. Its configuration:
# /etc/ssh/sshd_config.ch20 (on sshserver)
Port 2222
AddressFamily inet
ListenAddress 0.0.0.0
HostKey /etc/ssh/ssh_host_ed25519_key
PidFile /run/sshd-ch20.pid
LogLevel VERBOSE
PermitRootLogin no
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no
UsePAM no
AuthenticationMethods publickey
AllowUsers john
AuthorizedKeysFile .ssh/authorized_keys
RevokedKeys /etc/ssh/ch20_revoked_keys
Subsystem sftp internal-sftp
Validate it and start the instance, on sshserver in the root shell:
# /usr/sbin/sshd -t -f /etc/ssh/sshd_config.ch20
A clean validation prints nothing. Start the daemon:
# /usr/sbin/sshd -f /etc/ssh/sshd_config.ch20
Also silent. Confirm the listener:
# ss -ltnp | grep :2222
LISTEN 0 128 0.0.0.0:2222 0.0.0.0:* users:(("sshd",pid=16060,fd=6))
Now the revoked key is refused. Back on sshadmin, the isolation probe from Section 20.2.2 offers only that key:
$ ssh -p 2222 -F /dev/null -o IdentitiesOnly=yes -o IdentityAgent=none -o ProxyJump=john@192.168.220.30 -i ~/.ssh/id_ed25519_revoke sshserver hostname
john@sshserver: Permission denied (publickey).
While the agent's key - still listed, not revoked - still works:
$ ssh -p 2222 sshserver hostname
sshserver
Rotation is the sibling operation: replace an old key with a new one
deliberately, rather than because it leaked. Generate a replacement and
install its public half (ssh-copy-id -i ~/.ssh/id_ed25519_rotated.pub
sshserver):
$ ssh-keygen -t ed25519 -C "john@sshadmin-rotated" -f ~/.ssh/id_ed25519_rotated
Generating public/private ed25519 key pair.
Your identification has been saved in /home/john/.ssh/id_ed25519_rotated
Your public key has been saved in /home/john/.ssh/id_ed25519_rotated.pub
The key fingerprint is:
SHA256:Sp7b/4pGmZP7rUjqfll7YQjPphe0/TeFX9YQ+cHyeBQ john@sshadmin-rotated
The key's randomart image is:
+--[ED25519 256]--+
| .E.|
| .o+ |
| =o.|
| . . ..o.|
| . SO + .o.|
| o o* O + . =|
| +..B + o oo|
| =*oo.. ..o|
| o=o++== ..|
+----[SHA256]-----+
$ ssh -i ~/.ssh/id_ed25519_rotated sshserver hostname
sshserver
Remove the old line on sshserver - in the root shell, deleting the
john@sshadmin-new entry added in Section 20.2.2 (silent on success):
# sed -i '/john@sshadmin-new/d' /home/john/.ssh/authorized_keys
From sshadmin, the old key no longer authenticates:
$ ssh -F /dev/null -o IdentitiesOnly=yes -o IdentityAgent=none -o ProxyJump=john@192.168.220.30 -i ~/.ssh/id_ed25519_new sshserver hostname
john@sshserver: Permission denied (publickey).
And the rotated one still does:
$ ssh -i ~/.ssh/id_ed25519_rotated sshserver hostname
sshserver
Order matters in rotation: install and verify the new credential, then
withdraw the old. Reversed, there is a window with no working key.
When a key is merely being replaced, deleting the line suffices; when a
key may have leaked, add it to a KRL as well - deletion does not help on
hosts whose authorized_keys you cannot reach, and the KRL travels
(Chapter 26 treats the incident-response side).