Skip to content
BENIBIX Learn

SSH Keys and ssh-keygen

Level 2 · Keys and shortcuts

How an SSH key pair works: a private key that stays on your laptop and a public key that goes to the server. How to make one and put it in place.

Course contentsLesson 3 of 9

After this lesson you can

  • Make a key pair with ssh-keygen
  • Copy the public key to a server
  • Explain why the private key never leaves your laptop

13 mincovers 5 glossary entries

Worth reading first: SSH Host Keys and known_hosts

How a key pair replaces your password

An SSH key pair is two matching keys made together. The private key stays on your laptop and is never shared. The public key goes on the server, in a file called authorized_keys. When you log in, the server sends a challenge, and only the private key can sign it. No password crosses the line at all.

You make the pair with ssh-keygen. ssh-keygen -t ed25519 makes the modern kind. It writes two files in your .ssh folder: id_ed25519, the private key, and id_ed25519.pub, the public one. The public key is a single line of about 80 letters, starting with ssh-ed25519.

Next, the public key has to reach the server. ssh-copy-id does it in one line. It logs in once with your password and adds your public key to the end of authorized_keys. From then on, ssh uses the key.

This way of logging in is called public key authentication. The server checks the signature with the public key it already holds. Anyone can see the public key without harm. It can check a signature, but it cannot make one.

authorized_keys can hold many lines, one per key. Each laptop you own can have its own pair. If one laptop is lost, you delete only its line, and the others keep working.

sshd is fussy about who may touch these files. If your .ssh folder or authorized_keys can be changed by other users, it ignores them and asks for a password. chmod 700 on the folder and chmod 600 on the file fix that.

A common mistake is to send someone your private key so they can log in. Anyone who holds it is you, as far as the server knows. Ask them for their public key instead, and add it as a new line.

console
$ ssh-keygen -t ed25519 -C "jack@laptop"
Generating public/private ed25519 key pair.
$ ssh-copy-id jack@203.0.113.10
Number of key(s) added: 1
$ ssh jack@203.0.113.10
jack@web1:~$
Make the pair, copy the public half to the server, and log in with no password.
The private key signs on the laptop. The public key checks the signature on the server.

Worked example

Emma gives Jack access

Emma wants Jack to log in to her server. Jack offers to send her his password, and she says no.

She asks for his id_ed25519.pub instead. She pastes the one line into authorized_keys for his user. A minute later, Jack logs in with his key, and his private key never left his laptop.

The words for this lesson5

Check yourself

Which half of a key pair goes on the server?

Finished this lesson?

Marking it complete also marks its 5 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.