如何为没有密码生成的OpenSSH私钥添加密码?

时间:2010-09-29 05:11:43

标签: ssh

我使用puttygen生成了一个OpenSSH私钥(并以OpenSSH格式导出)。如何在现有密钥上设置密码(我知道如何使用密码生成新密钥)?

3 个答案:

答案 0 :(得分:287)

尝试命令ssh-keygen -p -f keyfile

来自ssh-keygen手册页

 -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.

示例:

ssh-keygen -p -f ~/.ssh/id_rsa

答案 1 :(得分:31)

对ssh-keygen使用-p选项。这允许您更改密码而不是生成新密钥。

在sigjuice显示时更改密码:

ssh-keygen -p -f ~/.ssh/id_rsa

所需的密码将是新密码。 (这假设您已将公钥~/.ssh/id_rsa.pub添加到a​​uthorized_keys文件中。)使用ssh进行测试:

ssh -i ~/.ssh/id_rsa localhost

您可以使用不同名称的多个键进行不同用途。

答案 2 :(得分:0)

您也可以使用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

请参阅:https://security.stackexchange.com/a/59164/194668