Sub topic 2
How Public Key Authentication Works
This chapter assumes you can use a Linux or macOS command line, have logged in with ssh using a password, understand basic Unix file permissions, and can edit a text file. You also need access to a remote Linux server.
The client and server
SSH, or Secure Shell, creates an encrypted connection between an SSH client and an SSH server. The client is usually your laptop. The server is the remote machine running sshd.
When you run this command, your SSH client asks the server to authenticate you as alice:
ssh [email protected]With password authentication, you type a password. The SSH connection is already encrypted, so the password is not sent as plain text across the network. However, passwords can be guessed, reused, stolen by phishing, or exposed in other ways. A server also needs a password-verification mechanism, such as a password hash, for each account.
Public key authentication replaces the shared password with a key pair:
- The private key is a secret file kept by the client. It proves that you control the identity.
- The public key is a non-secret value installed on the server.
- The server uses the public key to verify a signature made by the private key.
The public key cannot be used to derive the private key with practical computing resources. You can therefore copy the public key to a server, but you must protect the private key.
The authentication exchange
Before key authentication can succeed, the server must find your public key in the target account's ~/.ssh/authorized_keys file. Each line normally contains one public key.
The exchange works roughly like this:
- The client tells the server which user it wants to use and offers a public key.
- The server checks whether that key is allowed for the user.
- The server sends data that is specific to this connection.
- The client signs the data with the matching private key.
- The server verifies the signature with the stored public key.
- If verification succeeds, the server accepts the login.
The private key itself never travels to the server. A captured SSH session does not reveal it.
Passwords and key pairs compared
| Property | Password authentication | Public key authentication |
|---|---|---|
| Secret used by the client | Password text | Private key |
| Secret sent to the server | Used in an authentication exchange | Private key is not sent |
| Server-side record | Password verifier or hash | Public key in `authorized_keys` |
| Main operational risk | Guessing, reuse, and phishing | Loss or theft of the private key |
| Typical protection | Strong unique password and rate limits | File permissions and a passphrase |
Public key authentication is often called “passwordless” SSH. This means the server does not require an account password during login. Your private key can still have a passphrase. The passphrase protects the key file if someone copies it from your computer.
Two different host keys
SSH uses keys in two directions, and confusing them causes mistakes.
Your user key pair proves your identity to the server. You manage it, and the server stores your public key in authorized_keys.
The server's host key proves the server's identity to your client. On a first connection, SSH displays the host key fingerprint and asks you to confirm it. Compare that fingerprint with a trusted value from the server administrator or hosting provider. Accepting an unexpected changed fingerprint can expose you to a man-in-the-middle attack.
A host key check does not authenticate your user account, and your user public key does not verify the server's identity.
A simple key-based connection
After a public key has been installed for alice, connect by specifying the private key:
ssh -i ~/.ssh/id_ed25519 [email protected]If the matching public key is in ~alice/.ssh/authorized_keys and the server permits public key authentication, SSH performs the signature exchange instead of asking for the account password. Later chapters cover key generation, installation, permissions, and troubleshooting.
Exercise
Does the server receive Alice's private key during public key authentication, and what does the server verify instead?
Show answer
No. The client uses the private key to sign connection-specific data. The server verifies that signature with Alice's public key stored in authorized_keys.