如何在AWS管理控制台中更改ec2实例的密钥对?我可以停止实例,我可以创建新的密钥对,但我没有看到任何链接来修改实例的密钥对。


当前回答

来自AWS EC2支持的指令:

Change pem login go to your EC2 Console Under NETWORK & SECURITY, click on Key Pair Click on Create Key Pair Give your new key pair a name, save the .pem file. The name of the key pair will be used to connect to your instance Create SSH connection to your instance and keep it open in PuttyGen, click "Load" to load your .pem file Keep the SSH-2 RSA radio button checked. Click on "Save private key" You'll get pop-up window warning, click "Yes” click on "Save public key" as well, so to generate the public key. This is the public key that we're going to copy across to your current instance Save the public key with the new key pair name and with the extension .pub Open the public key content in a notepad copy the content below "Comment: "imported-openssh-key" and before "---- END SSH2 PUBLIC KEY ---- Note - you need to copy the content as one line - delete all new lines on your connected instance, open your authorized_keys file using the tool vi. Run the following command: vi .ssh/authorized_keys you should see the original public key in the file also move your cursor on the file to the end of your first public key content :type "i" for insert on the new line, type "ssh-rsa" and add a space before you paste the content of the public key , space, and the name of the .pem file (without the .pem) Note - you should get a line with the same format as the previous line press the Esc key, and then type :wq!

这将保存更新后的authorized_keys文件

现在尝试使用新的密钥pai打开一个新的SSH会话到您的实例

当您确认能够使用新的密钥对SSH进入实例时,您可以vi . SSH /authorized_key并删除旧的密钥。

对Shaggie评论的回答:

如果您无法连接到实例(例如密钥损坏),请使用AWS控制台分离卷(http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-detaching-volume.html)并将其重新连接到工作实例,然后更改卷上的密钥并将其重新连接到前一个实例。

其他回答

Yegor256的回答对我来说很有用,但我想我只是添加一些评论来帮助那些不太擅长挂载驱动器的人(比如我!):

Amazon允许您在附加卷时选择要如何命名它。您必须使用从/dev/sda到/dev/sdp范围内的名称 新版本的Ubuntu会将你放入的内容重命名为/dev/xvd(x)或类似的名称。

所以对我来说,我选择了/dev/sdp作为AWS中的挂载名称,然后我登录到服务器,发现Ubuntu已经将我的卷重命名为/dev/xvdp1)。然后我必须安装驱动器-对我来说,我必须这样做:

mount -t ext4 xvdp1 /mnt/tmp

在跳过所有这些步骤之后,我可以在/mnt/tmp目录下访问我的文件

如果遵循以下步骤,将节省大量时间,也不需要停止正在运行的实例。

开始新的t1。microec2实例,使用新的密钥对。确保在相同的子网中创建它,否则将不得不终止实例并重新创建它。 SSH到新的微实例,复制~/的内容。Ssh /authorized_keys在计算机的某处。 用旧的ssh密钥登录主实例。 从点2复制并替换文件内容到~/.ssh/authorized_keys 现在您只需使用新密钥即可再次登录。旧钥匙不能用了。

就是这样。享受:)

替代解决方案。如果你有唯一的访问服务器。在这种情况下,不要从AWS控制台删除pem文件。只需删除pem访问密钥从sudo纳米~/。Ssh / authized_keys,并添加系统公共Ssh密钥。现在您可以访问ssh user@i.p

只有当您可以访问想要更改/添加密钥的实例时,这才有效。 您可以创建新的密钥对。或者,如果您已经拥有密钥对,那么您可以将新密钥对的公钥粘贴到实例上的authorized_keys文件中。

vim . ssh / authorized_keys

现在您可以使用该对的私钥并登录。

希望这能有所帮助。

谢谢你们的建议。当我需要休息钥匙对时,我一定会把它们记在心里。 然而,出于效率和懒惰的考虑,我想出了另一个办法:

创建新的密钥对并下载凭证 右键单击实例>创建AMI一旦完成 终止实例(或者只是停止它,直到您确定可以从新的闪亮AMI创建另一个实例) 从刚才创建的AMI启动一个新的EC2实例,并指定在上面步骤(1)中创建的新密钥对。

希望这能对你有用,为你节省一些时间,并尽量减少你从这样的东西得到的白发数量:)