14

我想使用受密码保护的 pem 文件通过 SSH 连接到我的 EC2 实例。如何使用密码保护 pem 文件?我过去做过这个,但不记得我是怎么做到的。我获取了一个由 AWS 生成的 pem 文件并在其上运行了一些命令,它生成了如下所示的内容:

-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC,<BlahBlahBlah>

<encrypted stuff is here>

-----END RSA PRIVATE KEY-----

然后,当我 SSH 进入该框时,我指定了受密码保护的 pem 文件,它要求我在解密和 SSH 之前输入密码。

我发现了这个:https ://martin.kleppmann.com/2013/05/24/improving-security-of-ssh-private-keys.html

这告诉我使用这个命令

ssh-keygen -t rsa -N 'super secret passphrase' -f test_rsa_key

但是生成的加密文件(具有我正在寻找的正确标题)似乎不起作用。我收到“权限被拒绝(公钥)”。当我尝试使用该加密的 pem 文件进行 ssh 时。我可以使用未加密的 pem 文件通过 SSH 连接到盒子。

4

2 回答 2

21

这是因为您使用的命令会生成一个新的密钥对,而不是保护您现有的私钥。

尝试使用-p选项ssh-keygen

ssh-keygen -p -f my_private_key

它将提示您输入密码并保护您的私钥。

Enter new passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved with the new passphrase.

现在如果你使用my_private_keyin ssh,它会提示输入密码并且它会成功。

 -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.
于 2017-12-02T01:24:41.007 回答
0

您可以安装和使用 puttygen:

sudo apt install putty

要生成受保护的密钥,请执行以下操作:

puttygen KEY_PAIR_PRIVATE.pem -O private-openssh -o KEY_PAIR_PRIVATE.key -P

选项 -P 是为私钥设置一个新的密码。

PS:您可能需要设置使用密钥的权限,如下所示:

sudo chmod 755 KEY_PAIR_PRIVATE.key

最后,您可以安全地访问您的 aws 实例:

ssh -i KEY_PAIR_PRIVATE.key ubuntu@IP_EC2_INSTANCE_OR_HOSTNAME
于 2018-10-09T19:39:55.697 回答