How do I add a password to an OpenSSH private key that was generated without 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 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
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.
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
see: https://security.stackexchange.com/a/59164/194668