Sub topic 4
Generating a key pair with ssh-keygen
This chapter assumes you can use a Linux or macOS shell and have logged in to a server with ssh and a password. You should know what the Unix permission bits 600 and 700 mean. Here you will create the key pair. Installing the public key on the server comes in a later chapter.
What ssh-keygen produces
ssh-keygen is part of OpenSSH. It creates, inspects, and modifies key pairs. A key pair is two files that are mathematically linked:
- The private key stays on your machine and proves your identity. Anyone who holds it, and its passphrase if it has one, can log in as you.
- The public key can be copied anywhere. A server uses it to check proofs made with your private key, but cannot use it to impersonate you.
The basic command
The command below creates an Ed25519 key pair. The previous chapter explained why Ed25519 is a good default.
ssh-keygen -t ed25519 -C "alice@laptop" -f ~/.ssh/id_ed25519Each option does one job:
| Option | Meaning |
|---|---|
| `-t ed25519` | Key type (algorithm). Other values include `rsa`, `ecdsa`, and `ed25519-sk`. |
| `-C "alice@laptop"` | Comment stored in the public key. It is a label for humans. |
| `-f ~/.ssh/id_ed25519` | Path of the private key file. The public key gets the same path plus `.pub`. |
If you leave out -f, ssh-keygen asks for a path and offers the default, such as ~/.ssh/id_ed25519. If you leave out -C, the comment defaults to user@hostname.
A typical session looks like this:
Generating public/private ed25519 key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/alice/.ssh/id_ed25519
Your public key has been saved in /home/alice/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:q3Zk0m1v0Qx8yS2c9n5l0n0Zr0s6uYb2cX0rC4m7uXk alice@laptop
The key's randomart image is:
+--[ED25519 256]--+
| .o+==o. |
| . =+o+. |
+----[SHA256]-----+The fingerprint and randomart are short summaries of the public key. They help you compare keys without reading the whole key. The values above are illustrative, and yours will differ.
What the files contain
List the results:
ls -l ~/.ssh/id_ed25519 ~/.ssh/id_ed25519.pubThe private key should show mode -rw------- (600). ssh-keygen sets this itself. The public key is normally -rw-r--r-- (644), which is fine because it is not secret.
The public key is a single text line with three fields: the algorithm name, the base64-encoded key, and the comment.
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExampleOnlyNotARealKeyxxxxxxxxxxxxxxxxxxxx alice@laptopThis is the line that later ends up in the server's authorized_keys file. Copy it as one line. A line broken by an editor or a chat tool will not work.
The private key file begins with -----BEGIN OPENSSH PRIVATE KEY-----. Current OpenSSH writes this format for all key types. Older tutorials show BEGIN RSA PRIVATE KEY, which is the older PEM format. Both work with modern clients, but the OpenSSH format supports a stronger passphrase derivation.
Choosing options deliberately
Key type and size
Ed25519 keys have a fixed size, so no -b option is needed. Use it unless a system you must connect to does not support it.
For older systems that lack Ed25519 support, generate RSA with an explicit size:
ssh-keygen -t rsa -b 4096 -C "alice@laptop-rsa" -f ~/.ssh/id_rsa_legacyThe -b option sets the size in bits. Current OpenSSH defaults RSA to 3072 bits. A 4096-bit key is slightly slower to use but is a common choice. Do not go below 3072 for new RSA keys.
The comment
The comment is not part of authentication. The server never checks it, and you can edit it without changing the key's identity. Use something that tells you which machine and purpose the key belongs to, such as alice@laptop-2026. When you later find ten keys in an authorized_keys file, good comments tell you which to remove.
You can change a comment on an existing key:
ssh-keygen -c -f ~/.ssh/id_ed25519If the key has a passphrase, you are prompted for it, and then for the new comment.
The passphrase
The passphrase prompt appears during generation. A passphrase encrypts the private key file on disk, so a stolen file is not directly usable. The next chapters cover passphrase choice and ssh-agent. For now, enter a passphrase when prompted instead of pressing Enter.
OpenSSH derives the encryption key from your passphrase with bcrypt, run for a number of rounds. The -a option raises that number. The default is 16, and each extra round makes brute-force guessing slower for an attacker:
ssh-keygen -t ed25519 -a 100 -C "alice@laptop" -f ~/.ssh/id_ed25519The cost is a short delay each time you unlock the key. A value of 100 is a common choice and a delay of under a second is typical.
Using a separate file per purpose
Giving each key its own file keeps your options open. You can use different keys for work and personal servers, or for one automated job. Choose distinct names with -f:
ssh-keygen -t ed25519 -C "alice@work-2026" -f ~/.ssh/id_ed25519_work
ssh-keygen -t ed25519 -C "alice@personal-2026" -f ~/.ssh/id_ed25519_personalOpenSSH tries only a few default names automatically (id_ed25519, id_rsa, and others). Keys with custom names must be selected with ssh -i or with an IdentityFile line in ~/.ssh/config, which a later chapter covers.
Existing files and overwriting
If the target file exists, ssh-keygen stops and asks:
/home/alice/.ssh/id_ed25519 already exists.
Overwrite (y/n)?Answering y replaces the private key permanently. Every server that has the old public key will then reject you, because the old private key is gone. Before you answer, check what the file is:
ssh-keygen -lf ~/.ssh/id_ed25519.pubThe -l option prints the key's size, fingerprint, comment, and type. If you are unsure, choose a different -f name instead of overwriting.
Inspecting and repairing keys
A few ssh-keygen options are useful after generation.
Show the fingerprint of a public key, for example to compare it with one shown by a hosting service:
ssh-keygen -lf ~/.ssh/id_ed25519.pubRecreate a lost public key from the private key. This works because the public key can be derived from the private one:
ssh-keygen -y -f ~/.ssh/id_ed25519 > ~/.ssh/id_ed25519.pubThe recreated file has no comment, so add one afterwards or accept the empty field.
Change or add a passphrase on an existing key without creating a new key:
ssh-keygen -p -f ~/.ssh/id_ed25519Non-interactive generation
Scripts and provisioning tools sometimes need to generate keys without prompts. Use -N to pass the passphrase and -q to suppress output:
ssh-keygen -q -t ed25519 -N "" -C "backup-job@host1" -f ~/.ssh/id_ed25519_backupAn empty -N "" creates a key with no passphrase. That is sometimes needed for unattended jobs, but anyone who reads the file gains access. Restrict such keys on the server side, for example to one command and one source address. Passing a real passphrase with -N puts it in your shell history and in the process list while the command runs, so prefer the interactive prompt for personal keys.
Hardware-backed keys
OpenSSH 8.2 and later can create keys tied to a FIDO2 security key, using types ed25519-sk or ecdsa-sk:
ssh-keygen -t ed25519-sk -C "alice@yubikey" -f ~/.ssh/id_ed25519_skThe file you get is a handle, not the full secret. Using it requires the physical device, and usually a touch. Both the client and the server must run a version that supports these types. The rest of this tutorial uses ordinary software keys.
Common mistakes
- Sharing the wrong file. Only the
.pubfile is ever copied to servers or pasted into web forms. If a file does not end in.pub, do not send it anywhere. - Wrong path. With
-f ~/.ssh/mykey, the public key is~/.ssh/mykey.pub. Check the path in the output before continuing. - Missing directory. If
~/.sshdoes not exist and you give a path inside it,ssh-keygenmay report that it cannot save the key. Create it withmkdir -p ~/.ssh && chmod 700 ~/.ssh. - Empty passphrase by habit. Pressing Enter twice is the fastest way to make a key that is easy to steal. Use a passphrase, and let
ssh-agentremove the inconvenience. - Copying keys between machines. Copying a private key to another computer multiplies the places it can leak. Generate a separate key pair per client machine.
Exercise
You run ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_work and then ssh-keygen -lf ~/.ssh/id_ed25519_work.pub. Which files exist after the first command, which one is safe to share, and what does the second command show?
Show answer
The first command creates ~/.ssh/id_ed25519_work (the private key, mode 600) and ~/.ssh/id_ed25519_work.pub (the public key). Only the .pub file is safe to share. The second command prints the key size (256 bits), its SHA256 fingerprint, its comment (by default user@hostname), and the type (ED25519). It does not modify anything.
Exercise
You deleted ~/.ssh/id_ed25519.pub by accident but still have the private key ~/.ssh/id_ed25519. How do you get the public key back, and why is this possible?
Show answer
Run ssh-keygen -y -f ~/.ssh/id_ed25519 > ~/.ssh/id_ed25519.pub. If the key has a passphrase, you are prompted for it. This works because the public key is mathematically derived from the private key. The recreated file has no comment, which ssh-keygen -c -f ~/.ssh/id_ed25519 can restore.