Change or remove an SSH key passphrase with ssh-keygen -p
Run ssh-keygen -p -f with the key file, type the old passphrase, then a new one, or press Enter twice to remove it. The public key stays the same.
To change, add or remove the passphrase on an existing private key, run ssh-keygen with -p and name the key file:
ssh-keygen -p -f ~/.ssh/id_ed25519
It asks for the old passphrase, prints the key's comment, then asks for the new passphrase twice:
Enter old passphrase:
Key has comment '[email protected]'
Enter new passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved with the new passphrase.
- To remove the passphrase, press Enter at both new-passphrase prompts. The key is saved unencrypted.
- To add a passphrase to a key that has none, run the same command.
ssh-keygenfirst tries an empty passphrase, so it skips the old-passphrase prompt and goes straight to the new one. - Without
-f, it asks for the file name and offers a default path.
What changes and what does not
Only the private key file is rewritten. The key itself is the same, so the public key (.pub file) is unchanged, and every server that lists it in authorized_keys still accepts it. Nothing needs to change on the servers.
The comment is kept. A key already loaded into a running ssh-agent stays loaded and keeps working; the agent holds the unlocked key, not the file. See The agent and agent forwarding.
The file format can change
-p saves the key in OpenSSH's own format, the one that starts with -----BEGIN OPENSSH PRIVATE KEY-----. An older RSA key that started with -----BEGIN RSA PRIVATE KEY----- (PEM format) is converted. If a tool still needs PEM, add -m PEM:
ssh-keygen -p -m PEM -f ~/.ssh/id_rsa
Changing the passphrase also re-encrypts the key, so it is the moment to change the encryption settings. -a sets the number of key-derivation rounds (default 32; more rounds make each passphrase guess slower) and -Z the cipher (default aes256-ctr). See Key types and formats.
Without prompts
-P gives the old passphrase and -N the new one. An empty -N removes the passphrase:
ssh-keygen -p -f ~/.ssh/id_ed25519 -P 'old phrase' -N 'new phrase'
ssh-keygen -p -f ~/.ssh/id_ed25519 -P 'old phrase' -N ''
Passphrases typed on the command line can end up in your shell history. Use the prompts unless a script needs this form.
If it fails
Failed to load key ...: incorrect passphrase supplied to decrypt private key. The old passphrase was wrong. The file was not changed.Pass phrases do not match. Try again.The two new entries differed. Nothing was saved; run the command again.Failed to load key ...: error in libcrypto(orinvalid format). The file is not a private keyssh-keygencan read, for example a PuTTY.ppkfile or a damaged copy. See SSH Load key "invalid format" and "error in libcrypto".WARNING: UNPROTECTED PRIVATE KEY FILE!thenFailed to load key ...: bad permissions.ssh-keygenapplies the same permission check asssh: runchmod 600on the file first. See SSH "WARNING: UNPROTECTED PRIVATE KEY FILE!" and bad permissions.- You forgot the passphrase.
-pneeds the old one, and there is no way around it. Make a new key pair and replace the old public key on each server. See Authorized keys and permissions.
-p works on a copy as well as the original. Back up the key before you change it, so a typo in a new passphrase does not lock you out.
PuTTY keys
PuTTYgen changes the passphrase of a .ppk file: Load the key, type the new passphrase in Key passphrase and Confirm passphrase, then Save private key. Leaving both boxes empty saves it without a passphrase. To use the key with OpenSSH, see Convert a PuTTY .ppk key to OpenSSH format, and PPK version 3.
Related
- Key types and formats
- The agent and agent forwarding
- SSH Load key "invalid format" and "error in libcrypto"
- Convert a PuTTY .ppk key to OpenSSH format, and PPK version 3
- SSH "WARNING: UNPROTECTED PRIVATE KEY FILE!" and bad permissions