Sub topic 5
Protecting your private key with a passphrase
This chapter assumes you have already generated an Ed25519 key pair with ssh-keygen. It also assumes you know the private key is the file that proves your identity, and that you are comfortable with the shell and with ~/.ssh file permissions. Here you will learn what a passphrase protects, how it works, and how to add, change, or remove one. You will also learn when skipping one is a defensible choice.
What a passphrase protects
A private key is a file. Anyone who can read a copy of it can log in as you to every server that trusts the matching public key. The server cannot tell the thief from you, because it only checks a signature made with that key.
A passphrase encrypts the private key file on disk. With a passphrase, a stolen copy of the file is not enough, because the thief must also guess the passphrase.
Files can leak in many ways:
- A laptop is lost or stolen.
- A backup of your home directory ends up on a shared drive or cloud bucket.
- Malware or another local account reads files it should not.
- You copy
~/.sshto a new machine and leave a copy in a temporary location. - A misconfigured permission or a mistaken
git addexposes the file.
A passphrase does not protect you against everything. If malware runs as your user while the key is loaded in memory, it can use the key without knowing the passphrase. The passphrase protects the key at rest, which means while it is stored on disk.
A passphrase is not a password
The two serve different purposes.
| Row | Account password | Key passphrase |
|---|---|---|
| Checked by | The server | Your own computer |
| Sent over the network | Yes, inside the encrypted SSH channel | Never |
| Purpose | Prove identity to the server | Decrypt the key file locally |
| Stops a remote guesser | Only with rate limits | Not applicable, because the file must be stolen first |
| Stops a thief with the file | No | Yes, if strong |
The server never learns your passphrase and never learns whether you have one. Changing the passphrase does not change the public key, so you do not need to update authorized_keys on any server.
How the encryption works
Modern OpenSSH (7.8 and later) stores private keys in its own format, not the older PEM format. When you set a passphrase, ssh-keygen does this:
- It runs the passphrase through
bcrypt_pbkdf, a key derivation function (KDF). A KDF turns a passphrase into an encryption key and is deliberately slow. - It encrypts the private key material with a cipher,
aes256-ctrby default. - It stores the salt, the number of KDF rounds, and the encrypted data in the file.
The slowness is the point. A guesser has to repeat the KDF for every candidate passphrase, so the number of rounds sets the cost of each guess. The default is 16 rounds. You can raise it with the -a option. Each login then takes slightly longer to decrypt the key, which you will hardly notice.
Keys in the old PEM format use a much weaker scheme (a single MD5 step in the key derivation). If you have an old RSA key in the -----BEGIN RSA PRIVATE KEY----- format with a passphrase, convert it by running ssh-keygen -p -o -f on it. You can also generate a new key. New Ed25519 keys always use the modern format.
Setting a passphrase when you generate the key
When you run ssh-keygen, it prompts twice:
ssh-keygen -t ed25519 -C "alice@laptop" -a 100 -f ~/.ssh/id_ed25519Enter passphrase (empty for no passphrase):
Enter same passphrase again:The -a 100 option sets 100 KDF rounds. Type the passphrase twice. Nothing appears on screen as you type, and that is normal.
Pressing Enter at the prompt creates a key with no passphrase. ssh-keygen accepts that without complaint, so it is easy to do by accident. It also refuses passphrases shorter than 5 characters, but that limit is far below what is safe.
Choosing a good passphrase
A passphrase protects against offline guessing, so strength matters more here than for a rate-limited web login. The attacker can try guesses as fast as their hardware allows.
- Use a phrase of four or more unrelated words, or a long sentence you can remember. Length beats clever substitutions.
- Do not reuse your account password or any passphrase you use elsewhere.
- Do not use song lyrics, quotations, or anything else found in published text.
- A password manager can store the passphrase, but then the manager becomes the thing to protect.
If you forget the passphrase, there is no recovery. You must generate a new key pair and install the new public key on every server. This is the reason to record the passphrase somewhere safe.
Adding, changing, or removing a passphrase
The -p option of ssh-keygen changes the passphrase of an existing private key. The public key stays the same.
ssh-keygen -p -a 100 -f ~/.ssh/id_ed25519The command asks for the old passphrase (press Enter if there is none), then the new one twice. It rewrites the file in place, so -a 100 also re-encrypts with 100 rounds.
To remove the passphrase, give an empty new passphrase. Do this only if you have decided it is acceptable, which the next sections discuss.
You can also pass the passphrases on the command line:
ssh-keygen -p -P 'old passphrase' -N 'new passphrase' -f ~/.ssh/id_ed25519This is convenient for scripts, but the passphrases then appear in your shell history and in the process list that other local users can read. Use the interactive prompt for real keys.
Checking whether a key has a passphrase
You can test a key without logging in anywhere. The command below tries to derive the public key from the private key using an empty passphrase:
ssh-keygen -y -P "" -f ~/.ssh/id_ed25519If the key has no passphrase, the command prints the public key. If it has one, you get an error:
Load key "/home/alice/.ssh/id_ed25519": incorrect passphrase supplied to decrypt private keyRunning ssh-keygen -y -f ~/.ssh/id_ed25519 without -P also works. It prompts for the passphrase if one is set, and it prints the public key when you enter it correctly. Use that form to confirm you remember the passphrase before you rely on it.
What you will see when you log in
With a passphrase set, the first connection prompts for it:
Enter passphrase for key '/home/alice/.ssh/id_ed25519':This prompt comes from your local ssh client. It is not the server asking for a password. If you see [email protected]'s password: instead, key authentication has failed and the server has fallen back to password login. That is a different problem, covered in the troubleshooting chapter.
Typing the passphrase for every connection gets tiring, and tired users are tempted to remove it. The next chapter shows how ssh-agent decrypts the key once and holds it in memory for your session. That gives you both protection at rest and a single prompt.
When a passphrase-less key is acceptable
Some uses need a key that works without anyone typing a passphrase: cron jobs, backup scripts, and deployment pipelines. In these cases:
- Use a dedicated key for that one job. Never reuse your personal login key.
- Store the file with mode
600in a directory only the job's account can read. - Restrict what the key can do on the server. Options in
authorized_keyssuch asfrom="203.0.113.5"andcommand="/usr/local/bin/backup"limit where the key can be used from and what it can run. Addrestrictto turn off forwarding and other extras. - Give the remote account the minimum permissions it needs.
For an interactive login key on a laptop, keep the passphrase. The table below compares the choices.
| Situation | Recommendation |
|---|---|
| Personal key on a laptop or desktop | Passphrase plus `ssh-agent` |
| Key on a shared or multi-user machine | Strong passphrase, always |
| Unattended job on one server | Dedicated key, restricted in `authorized_keys`, no passphrase |
| Key on a hardware token | Token PIN acts as the protection (OpenSSH supports `ed25519-sk` keys) |
Common mistakes
- Pressing Enter at the prompt by accident. Check with
ssh-keygen -y -P "" -f ~/.ssh/id_ed25519. If it prints a public key, the file is unprotected. Add a passphrase withssh-keygen -p. - Copying the key to another machine and forgetting the old copies. A passphrase on the new copy does not change the old file. Each copy has its own encryption. Delete copies you do not need.
- Assuming the passphrase stops use of a loaded key. Once the key is in an agent, anyone who controls your session can use it. Lock your screen and keep agent lifetimes short, as the next chapter explains.
- Changing the passphrase and expecting servers to notice. They will not, because the key itself did not change.
- Putting the passphrase in the command line or in a script for a real key. Use the prompt or the agent.
Summary
A passphrase encrypts your private key file with a key derived by the slow bcrypt_pbkdf function. It is checked locally, never sent to the server, and can be changed without touching authorized_keys. Pick a long, unique phrase, raise the round count with -a 100, and verify the result with ssh-keygen -y. Skip the passphrase only for dedicated, restricted keys used by unattended jobs.
Exercise
You run ssh-keygen -y -P "" -f ~/.ssh/id_ed25519 and it prints a line starting with ssh-ed25519. What does that tell you about the key, and what single command would you run to fix it? Do you need to change anything on the servers afterwards?
Show answer
Printing the public key with an empty passphrase means the private key is not encrypted, so anyone who copies the file can use it. Run ssh-keygen -p -a 100 -f ~/.ssh/id_ed25519, press Enter at the old-passphrase prompt, and enter a new strong passphrase twice. Nothing needs to change on the servers, because the public key is unchanged. You should delete any older unencrypted copies of the file, such as backups, and if one may have leaked, generate a new key pair and replace the public key on your servers.