I generated an OpenSSH private key using PuTTYgen (and exported it in OpenSSH format).
How can I put a password on this existing key (I know how to generate a new key with a password)?
I generated an OpenSSH private key using PuTTYgen (and exported it in OpenSSH format).
How can I put a password on this existing key (I know how to generate a new key with a password)?
Try the following command:
ssh-keygen -p -f keyfile
From the ssh-keygen man page
-p Requests changing the passphrase of a private key file instead of
creating a new private key. The program will prompt for the file
containing the private key, for the old passphrase, and twice for
the new passphrase.
-f filename
Specifies the filename of the key file.
Example:
ssh-keygen -p -f ~/.ssh/id_rsa
$ ssh-keygen -p -f /Users/sigjuice/.ssh/id_rsa
? This might help people who don't know how to tell the difference between a public and a private key, and help them get their feet wet faster. –
Expurgate Proc-Type: 4,ENCRYPTED
header, which is incompatible with some applications checking for a passphrase. After trying several ways to get it to work, the easiest way to workaround it was just do this same thing inside a Docker container running Ubuntu and then copying the key back to my Mac. –
Tierza Use the -p option to ssh-keygen. This allows you to change the password rather than generate a new key.
Change the password as sigjuice shows:
ssh-keygen -p -f ~/.ssh/id_rsa
The required password will be the new password. (This assumes you have added the public key ~/.ssh/id_rsa.pub
to your authorized_keys files.) Test with ssh:
ssh -i ~/.ssh/id_rsa localhost
You can have multiple keys with different names for different uses.
ssh-add -D
to remove your cached identity. Then, try connecting again and it will ask you for your password. Use ssh-add -l
to see a list of your cached identities. –
Fogarty You can also use openssl
:
openssl rsa -aes256 -in ~/.ssh/your_key -out ~/.ssh/your_key.enc
mv ~/.ssh/your_key.enc ~/.ssh/your_key
chmod 600 ~/.ssh/your_key
Because you've mentioned "PuTTYgen" and maybe you're using Windows 😉, I'll direct you to the documentation for "PuTTYgen".
Go here [https://the.earth.li/~sgtatham/putty/0.76/htmldoc/Chapter8.html#puttygen-conversions] for "importing" and "exporting" a SSH private key. These are different to using "Load" and "Save" as those options are for loading and saving a Putty specific key file.
And here [https://the.earth.li/~sgtatham/putty/0.76/htmldoc/Chapter8.html#puttygen-passphrase] for changing the passphrase. Same thing you'd do when creating a passphrase for a new private key.
So steps are "import" the SSH key, you don't get asked for a passphrase because you didn't create one. Then change (set) the passphrase and confirm. Then "export" back out to the original private key file.
Hope that helps anyone else wanting to use "PuTTYgen" instead of "ssh-keygen".
© 2022 - 2024 — McMap. All rights reserved.
Bad passphrase
on anid_ed25519
key but the password is correct, then you are probably using a down-levelssh-keygen
to manage it. – Nobukonoby