ssh-keygen
ssh-keygen (SSH key generator) is the OpenSSH tool that creates, manages and converts authentication keys for ssh and sshd. Run without arguments it generates an Ed25519 key pair: a private key and a matching .pub public key. It also prints key fingerprints, changes passphrases, removes entries from known_hosts and creates FIDO security-key keys.
What ssh-keygen creates and which key type to pick
The ssh-keygen manual lists these key types for -t: ed25519 (the default), ecdsa, rsa, and the FIDO types ecdsa-sk and ed25519-sk. Pick Ed25519 unless an old system cannot accept it. Ed25519 keys have a fixed length, so -b is ignored for them. For RSA the manual gives a 1024-bit minimum and a 3072-bit default, and says 3072 is generally considered sufficient.
The default files are ~/.ssh/id_ed25519 (private) and ~/.ssh/id_ed25519.pub (public), and the private file should not be readable by anyone but you. The tool asks for a passphrase; an empty one means no passphrase, and a lost passphrase cannot be recovered. A set passphrase encrypts the key with a key derivation function, and -a sets its rounds (default 32), where higher numbers slow down guessing against a stolen key. The comment defaults to your user and host name, and -C sets your own. If the target file exists, the tool asks before overwriting it.
Everyday commands and how to test a new key safely
These cover most of what you need:
ssh-keygen -t ed25519 -a 100 -C "alice-laptop-2026" -f ~/.ssh/id_ed25519_work
ssh-keygen -l -f ~/.ssh/id_ed25519_work.pub
ssh-keygen -p -f ~/.ssh/id_ed25519_work
ssh-keygen -y -f ~/.ssh/id_ed25519_work
ssh-keygen -F myserver
ssh-keygen -R myserverThe commands create a key, print its fingerprint (SHA256 by default, -E md5 for the old format), change the passphrase, print the public key from a private key, look up a host in known_hosts and remove all entries for a host. In current OpenSSH, -l also accepts an authorized_keys file and prints one line per key, and -R keeps the previous file as known_hosts.old. Use -R after a server was legitimately rebuilt, not to silence a host key warning you do not understand (see known_hosts).
To test a new key, install it with ssh-copy-id, keep your current session open, and from a second terminal run ssh -o IdentitiesOnly=yes -i ~/.ssh/id_ed25519_work myserver. Only when that works should you consider switching password login off.
Mistakes and security pitfalls
The most common mistakes are about handling, not about the algorithm:
- No passphrase. Anyone who copies the private key file, from a stolen laptop or a stray backup, can use it at once. Passphrase plus
ssh-agentis the usual compromise. - Copying the private key to a server. Only the
.pubfile ever leaves your machine. See key-based authentication. - One key everywhere. Make one key per person and per device, with a comment you can recognise, so a lost laptop means removing one line.
- Trusting the comment. It is free text. The fingerprint identifies a key.
- Weak legacy keys. The manual allows RSA down to 1024 bits, so do not choose less than the 3072 default.
- Agent forwarding by habit. See SSH agent forwarding.
Also remember that -C sets the comment only at creation. To change it on an existing key, use -c. If a login fails after you add a key, the Permission denied (publickey) guide walks through the causes.
ssh-keygen in a hardening routine
Good key hygiene is a small routine. Create Ed25519 keys with a passphrase, store each key on one device, and record who holds which fingerprint. When you audit a server, compare the fingerprints from ssh-keygen -l with that record and remove keys nobody can explain, as the SSH hardening guide advises.
On first connection to a new server you can also compare the fingerprint ssh shows with the one printed on the server by the same tool:
ssh-keygen -l -f /etc/ssh/ssh_host_ed25519_key.pubFor a second factor, ssh-keygen -t ed25519-sk -O verify-required creates a key bound to a hardware token. The manual lists the FIDO options (resident, no-touch-required, verify-required and others), and FIDO2 and WebAuthn explains the trade-offs. The SSH two-factor authentication guide shows the rollout. If a private key leaks, remove its public line from every authorized_keys, then follow the breach response guide to check for misuse. The sshd manual documents what the server does with the public key.
Related terms: Key-based authentication, authorized_keys, ssh-copy-id, known_hosts, FIDO2 and WebAuthn.
Frequently asked questions
What does ssh-keygen do?
It generates SSH key pairs and manages them. The same tool prints fingerprints (-l), changes passphrases (-p), removes known_hosts entries (-R) and creates FIDO hardware-backed keys.
Which key type does ssh-keygen create by default?
Ed25519. The current manual states that ed25519 is the default type, and the default files are id_ed25519 and id_ed25519.pub in the .ssh directory.
How do I see the fingerprint of an SSH key?
Run ssh-keygen -l -f followed by the public key file. It prints the SHA256 fingerprint by default, and -E md5 prints the older MD5 form.
Do I need a passphrase on an SSH key?
You should set one. An empty passphrase means anyone who copies the file can use the key, and a lost passphrase cannot be recovered, so you would generate a new key.
What does ssh-keygen -R do?
It removes all keys for a given host name (optionally with a port) from known_hosts. Use it when a server was rebuilt on purpose, and verify the new host key fingerprint before connecting again.
Want to see where your own server stands?
Run the free, read-only server check, or open the Security Lab and watch the detect, contain, recover, verify loop in action.
Get your free server checkOpen the Security Lab