Why you need it
To check a password, the server has to receive it, so the password must travel. Anyone who sees it on the way, or at a fake server, can copy it and log in as you. A key pair lets a server check you without anything secret leaving your laptop.
Think of it like this
Your signature at the bank. The clerk keeps a sample of it to compare, and anyone may see that sample. But seeing it does not let them sign as you: only your hand can make a new signature.
Where you’ll find it
- OpenSSH:
ssh-keygenmakes the two keys, andsshuses them to log in to servers with no password. - GitHub: You give GitHub your public key, and your laptop proves it holds the private one every time you send code.
- AWS: A new server is reached with an EC2 key pair: AWS puts the public key on the server, you keep the private key.
- Passkeys: Passkeys use the same idea to log in to websites: the phone keeps the private key, the website keeps the public one.
What to know first
Logging in to a server. A developer types a command like ssh ben@server-1 to work on a server from a laptop.
Signature. A mark made from a number with the private key. Only that key can make it; the public key can check it.
How it works
- A password must travel. Ben types his password and it goes to the server to be checked. In the animation a stranger on the way copies it: stolen. Real SSH scrambles the trip, so in real life a password is usually copied at the far end, by a fake server or one someone broke into. Either way, it left the laptop. (Real terminals also do not show the password as you type it.)
- Make two keys. Ben runs
ssh-keygenand gets a pair. The private key stays on his laptop and is never sent. The public key he gives away: to the server, and even to the stranger, because it is no use for logging in. - Sign a number. To log in, the server sends a number. Ben's private key signs it, and the signed number goes back. The server checks the signature with Ben's public key. It matches, so Ben is in, and nothing secret travelled.
- The number is new each time. The server picks a fresh number for every login. So a signed number the stranger copied yesterday is useless today: it is a signature of the wrong number.
- A public key cannot sign. The stranger has the public key and saw everything that travelled, but cannot sign the new number, so the server answers
Permission denied. Only a stolen private key would let him in: press the button in the animation to see that.
An example
Ben logs in to 3 servers with the password tulip9. One of them was broken into, and the password is copied there.
Now the stranger can log in to all 3 servers as Ben, until Ben changes the password everywhere.
With a key pair, each server holds only Ben's public key. The broken server learns nothing it can use on the other 2, and Ben's private key never left his laptop.
Further reading
- What is public key authentication?, SSH Academy. A short, plain walk through logging in with a key instead of a password. Start with this one.
- How does public key encryption work?, Cloudflare Learning. The same key pair, as the web uses it, with no maths.
- RFC 4252, section 7: public key login, IETF. The real rule SSH follows: what gets signed, and how the server checks it.