ssh-keygen
make an SSH keyssh-keygen generates a pair of keys. The public key is copied to servers; the private key never leaves your machine. A server that holds your public key can check that you hold the private one — a login with no password sent at all.
The .pub file can go anywhere. The other one is the secret.
At the terminal
Read the session first, then the notes: each numbered line in the output is answered by the note with the same number.
What the numbered lines are telling you
Pressing Enter accepts the default location, which is where ssh looks for keys.
It encrypts the private key file. Someone who copies the file still cannot use it without the passphrase.
The first is the private key — guard it. The .pub file is the public half, safe to share.
ssh-copy-id appends it to ~/.ssh/authorized_keys on the server. From now on ssh logs in with the key.
Switches
A switch changes how the command behaves. These are the ones used day to day; man ssh-keygen lists the rest. Single-letter switches can usually be joined, so -l -a may be written -la.
-t TYPE- The key type: ed25519 or rsa.
-b BITS- Key size, for RSA.
-C COMMENT- A label stored with the key.
-f FILE- The file to write or read.
-p- Change the passphrase of an existing key.
-l- Show a key’s fingerprint.
-R HOST- Remove a host’s entries from known_hosts.
-y- Print the public key belonging to a private key.
Variations worth knowing
The same tool, pointed at a narrower question. These four cover most of what it is reached for day to day.
cat ~/.ssh/id_ed25519.pubPrints your public key, to paste into GitHub or a server.
ssh-keygen -lf ~/.ssh/id_ed25519.pubShows a key’s fingerprint.
ssh-keygen -p -f ~/.ssh/id_ed25519Changes or adds the passphrase on an existing key.
ssh-keygen -R 192.0.2.10Removes a host from known_hosts, after a server has been rebuilt.
In cybersecurity
A private key is a password that is never typed and never expires — so look after it accordingly.
An unprotected private key works for whoever copies the file. With a passphrase, a stolen laptop or backup does not hand over every server.
Do not copy a private key between machines. Give each device its own key, so one can be revoked without affecting the rest.
Each line in ~/.ssh/authorized_keys is someone who can log in as that user. An unrecognised key is a backdoor.