Sub topic 4
Generating a Key Pair with `ssh-keygen`
This chapter assumes you can use a Linux or macOS command line, understand basic Unix file permissions, and have an SSH account on a remote server. You should also know how to connect with ssh and a password.
An SSH key pair contains two related files:
- A private key stays on your computer. It proves that you control the key pair.
- A public key goes on the server. It can be shared because it cannot be used to impersonate the private key.
The ssh-keygen command creates both files. During login, the server uses the public key to issue a challenge. Your SSH client answers the challenge with the private key without sending the private key across the network.
Choosing a key type
For new keys, use Ed25519 unless you need compatibility with an old SSH implementation. Ed25519 keys are small, fast, and secure with modern OpenSSH versions.
| Key type | Recommended use | Notes |
|---|---|---|
| Ed25519 | New installations | Short keys and strong modern security |
| RSA | Older systems or compatibility needs | Use at least 3072 bits, preferably 4096 |
| DSA | None | Obsolete and disabled by modern OpenSSH |
The key type is independent of the server name or username. You can use one key with several servers, but separate keys make it easier to revoke access for one purpose without affecting others.
Creating an Ed25519 key pair
Run this command on your local computer:
ssh-keygen -t ed25519 -C "alice@laptop" -f ~/.ssh/id_ed25519The options mean:
-t ed25519selects the Ed25519 algorithm.-Cadds a comment to help identify the key. The comment is not used for authentication.-fselects the output file. The public key receives the same name with.pubappended.
The command asks for a passphrase:
Enter passphrase (empty for no passphrase):
Enter same passphrase again:Use a strong passphrase. It encrypts the private key file when it is stored on disk. Anyone who copies the encrypted file still needs the passphrase to use it.
The command creates these files:
~/.ssh/id_ed25519 private key
~/.ssh/id_ed25519.pub public keyThe .pub suffix identifies the public key. Never copy the private key to the server or send it through email, chat, or a ticket.
Checking the generated files
List the files and inspect their permissions:
ls -l ~/.ssh/id_ed25519 ~/.ssh/id_ed25519.pubYou can display the public key because it is intended to be installed on servers:
cat ~/.ssh/id_ed25519.pubA public key is one long line, commonly beginning with ssh-ed25519. Do not edit line breaks into it. The private key should remain readable only by your account. If necessary, set its permissions explicitly:
chmod 600 ~/.ssh/id_ed25519The .ssh directory itself should also be private:
chmod 700 ~/.sshUnderstanding the files
The private and public files are mathematically related, but they have different roles. The client reads the private key to create a digital signature. The server checks that signature with the public key installed in the account's authorized_keys file.
If you need a separate key, choose a different filename rather than overwriting the existing one:
ssh-keygen -t ed25519 -C "deploy@project" -f ~/.ssh/id_ed25519_projectBefore generating a key at an existing path, ssh-keygen warns that the file will be replaced. Stop and choose another filename unless you intentionally want to replace that key.
Exercise
Which file should you copy to a server, and why should the other file remain on your computer?
Show answer
Copy ~/.ssh/id_ed25519.pub. It is the public key used by the server to verify login signatures. Keep ~/.ssh/id_ed25519 private because it creates those signatures and must not be exposed.