Sub topic 2
How Public Key Authentication Works
This chapter explains what happens between your ssh command and a shell prompt when you log in with a key. It assumes you can already log in to a Linux server with a password, that you know the basics of the client-server model, and that you can read short shell commands. You won't generate or install keys here. Later chapters cover that. The goal is a working mental model, so that failures in those later steps make sense.
The problem with shared secrets
A password is a shared secret. You know it and the server can check it. Over SSH the password travels inside an encrypted channel, so a network eavesdropper can't read it. The server still receives the plain password and then compares it to a stored hash. This has three weaknesses:
- A malicious or compromised server learns your password. If you reused it elsewhere, those accounts are exposed too.
- Anyone on the internet can guess passwords against your server. Automated scanners do this constantly.
- You must remember the password or store it somewhere, and human-chosen passwords are often weak.
Public key authentication removes the shared secret. You prove you hold a secret without ever sending it.
Two keys, two roles
A key pair has two mathematically linked parts:
- The private key stays on your client machine. It is never copied to the server and never sent over the network.
- The public key can be shared freely. It is installed on every server you want to log in to.
The link between them is a one-way relationship. Data signed with the private key can be verified with the public key. Deriving the private key from the public key is computationally infeasible for properly generated keys. This is the same idea behind digital signatures in general.
So the server can hold your public key, check a signature, and be confident that whoever produced it holds the matching private key. At no point does the server learn anything it could use to impersonate you elsewhere.
The login exchange step by step
The SSH user authentication protocol is defined in RFC 4252. Authentication happens after the encrypted transport is already set up, so everything below is protected by that channel.
- The client connects. Both sides negotiate algorithms and run a key exchange. The server proves its identity with its host key, and both sides derive a session identifier that is unique to this connection.
- The client names the account it wants (
alice) and offers a public key. In this first request it sends only the public key and the algorithm, with no signature yet. - The server looks in that account's
~/.ssh/authorized_keysfile. If the key is listed, it replies that the key is acceptable. If not, it rejects the offer and the client tries its next key or another method. - The client builds a message that includes the session identifier, the username, and the public key. It signs that message with the private key and sends the signature.
- The server verifies the signature using the public key from
authorized_keys. If it is valid, authentication succeeds and the server starts your session.
Step 2 and step 4 are separate on purpose. The first lets the client cheaply test which of its keys the server will take, so it doesn't sign for every key. Signing may require your passphrase or a hardware token, so avoiding needless signatures matters.
Why a captured login can't be replayed
The signed message includes the session identifier. That value is different for every connection, because it is derived from the key exchange. A recorded signature from yesterday is useless today, since the session ID it covers no longer matches. The same binding stops a malicious server from forwarding your signature to a different server. That server would have a different session ID.
Comparing the two methods
| Aspect | Password login | Public key login |
|---|---|---|
| What the server receives | The password itself | A signature, never the private key |
| Guessable by an attacker | Yes, by repeated attempts | Not practically, for a modern key type |
| Secret stored on the server | A password hash | Only public keys |
| Effect of a compromised server | Password may be captured | Private key is not exposed |
| Reuse across servers | Often the same password | One public key can be installed on many servers safely |
| What you protect | A memorized string | A private key file, ideally with a passphrase |
Two separate trust relationships
Beginners often mix up two different checks that happen in one connection:
- Server authentication. Your client checks the server's host key against the entry in
~/.ssh/known_hosts. This protects you from connecting to an impostor. The first-connection prompt about an unknown fingerprint belongs here. - User authentication. The server checks your user key against
authorized_keys. This is the process described above.
These use different key pairs. The host key lives on the server in /etc/ssh/. Your user key lives on your client in ~/.ssh/. A later chapter covers verifying the host. For now, remember that a successful key login says nothing about whether you reached the right server. That is the job of the host key check, which happens first.
What the server stores
The server doesn't store your key as a file you upload to a special folder. It reads one text file per account, ~/.ssh/authorized_keys, with one public key per line. A line has this shape:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExampleOnlyNotARealKeyxxxxxxxxxxxxxxxx alice@laptopThe three fields are the key type, the base64-encoded public key, and a free-text comment. The comment is only a label. The server ignores it when matching. An optional list of restrictions can come before the key type, such as from="203.0.113.5" or no-port-forwarding. These options let you limit what a particular key may do.
Because authorization is just "this key appears in this file for this account," access control is simple to reason about. Add a line to grant access and delete the line to revoke it. The server doesn't care who generated the key or on what machine.
Seeing the exchange yourself
You can watch the protocol steps in verbose client output. The filter below keeps only the lines that matter:
ssh -v [email protected] 2>&1 | grep -E 'Authentications that can continue|Offering public key|Server accepts key|Authenticated to'On a successful key login you will see lines similar to this:
debug1: Authentications that can continue: publickey,password
debug1: Offering public key: /home/alice/.ssh/id_ed25519 ED25519 SHA256:abc123ExampleFingerprint agent
debug1: Server accepts key: /home/alice/.ssh/id_ed25519 ED25519 SHA256:abc123ExampleFingerprint agent
Authenticated to server.example.com ([203.0.113.10]:22) using "publickey"."Offering" is step 2. "Server accepts key" is step 3. "Authenticated ... using publickey" is step 5. If you see "Offering" but never "accepts," the server doesn't have that public key for that account. Compare fingerprints to be sure. This command prints the fingerprint of a public key file:
ssh-keygen -lf ~/.ssh/id_ed25519.pubThe SHA256: value it prints should match the one in the verbose output.
Edge cases and common misunderstandings
The passphrase is not part of the protocol. The server never sees it. A passphrase only encrypts the private key file on your disk. It stops someone who steals the file from using it directly. A key without a passphrase works the same way on the wire but is exposed if the file leaks.
Passwordless is not the same as unauthenticated. You still authenticate every time. The signing just happens automatically, either because the key has no passphrase or because an agent holds the unlocked key.
Leaking a public key is not a breach. You can post it publicly, as GitHub does for user keys. Leaking a private key is a full compromise of every account that trusts it.
The server tries methods in its own order. The line "Authentications that can continue" lists what the server still accepts. If public key fails, the client falls back to the next method, often a password prompt. That fallback can hide a broken key setup, because you get in and don't notice the key was never used. To test the key alone, disable the fallbacks for that one command:
ssh -o PreferredAuthentications=publickey -o PasswordAuthentication=no [email protected]Too many offered keys can lock you out. The client offers each loaded key in turn. The server counts each rejected offer as a failed attempt, and MaxAuthTries defaults to 6. If an agent holds many keys, the server may disconnect with "Too many authentication failures" before your correct key is tried. Restricting which keys a host receives, covered in the client config chapter, avoids this.
The key must be usable by the right account. A key in /home/alice/.ssh/authorized_keys grants access only to alice. Logging in as bob with the same key fails unless the key is also in Bob's file.
Agent forwarding extends trust. If you enable it, programs on the server can ask your local agent to sign on your behalf during the session. They never get the private key, but a root user on that server can use your agent while you are connected. Enable it only for hosts you trust.
Summary of the model
- The private key stays with you. The public key goes to the server.
- Login proves possession by signing data that includes a per-connection session ID.
- The server decides access by finding your public key in the target account's
authorized_keys. - Host verification and user verification are separate checks in the same connection.
- The passphrase protects the file at rest and is invisible to the server.
Exercise
During a login, a client offers a key and the server replies that the key is acceptable, but the client is then asked for a password. Name two things this tells you about the state of the exchange, and one thing you can't conclude.
Show answer
It tells you the server found the offered public key in authorized_keys (step 3 passed), and that the later steps failed or were skipped, so the server fell back to the next method, password. One possible reason is that signing failed or the client could not unlock the key. You can't conclude that the key is installed correctly, because a "key acceptable" reply only means a matching line exists. The signature check, and any server-side restrictions on that line, can still fail. Verbose output and the server log are needed to find out which.
Exercise
Why can't an attacker who records a full successful key login replay the signature to log in later?
Show answer
The signed message includes the session identifier, which is derived from that connection's key exchange and is different for every connection. A replayed signature covers an old session ID, so the server's verification against the new session ID fails.