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


当前回答

这个答案是有用的情况下,您不再有SSH访问现有的服务器(即您丢失了您的私钥)。

如果您仍然拥有SSH访问权限,请使用下面的答案之一。

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html#replacing-lost-key-pair

以下是我所做的,感谢Eric Hammond的博客文章:

停止正在运行的EC2实例 分离它的/dev/xvda1卷(我们称它为卷A)—请参见这里 开始新的t1。microec2实例,使用我的新密钥对。确保在相同的子网中创建它,否则将不得不终止实例并重新创建它。-请看这里 将卷A挂载到新的微实例,如/dev/xvdf(或/dev/sdf) SSH到新的微实例,将卷A挂载到/mnt/tmp

$ sudo mkdir /mnt/tmp; sudo mount /dev/xvdf1 /mnt/tmp

~ /副本。Ssh /authorized_keys到/mnt/tmp/home/ubuntu/.ssh/authorized_keys 注销 终止微实例 从它分离卷A 将卷A挂回主实例为/dev/xvda 启动主实例 像以前一样登录,使用新的.pem文件

就是这样。

其他回答

我注意到,当由Elastic Beanstalk管理时,您可以更改活动的EC2密钥对。2 .在“Elastic Beanstalk > Configuration > Security”下的“EC2密钥对”下拉框中选择新密钥。你会看到这条消息询问你是否确定:

EC2KeyName:对选项EC2KeyName设置的更改将不会生效 立即。您现有的每个EC2实例都将被替换 届时,您的新设置将生效。

当我这样做时,我的实例已经终止了。然后开始,结束,再开始。显然,“替换”意味着终止并创建一个新实例。如果您已经修改了引导卷,那么首先创建一个AMI,然后在相同的Elastic Beanstalk > Configuration > Instances表单中指定这个AMI作为自定义AMI ID。这也会对替换EC2实例发出警告。

在修改了EC2密钥对和Custom AMI ID并看到有关这两者的警告之后,单击Save继续。

请记住,当重新创建实例时,IP地址会发生变化,因此您需要从EC2控制台检索一个新的IP地址,以便在通过SSH连接时使用。

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

vim . ssh / authorized_keys

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

希望这能有所帮助。

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

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

就是这样。享受:)

最简单的解决方法是复制的内容

~/.ssh/id_rsa.pub

到您的AWS实例的authorized_keys at

~/.ssh/authorized_keys

这将允许您ssh进入EC2实例,而无需为ssh命令指定pem文件。测试连接后,可以删除所有其他键。

如果你需要创建一个新的密钥来与其他人共享,你可以通过:

ssh-keygen -t rsa

这将创建私钥。Pem文件,你可以通过以下方式获取该文件的公钥:

ssh-keygen -f private_key.pem -y > public_key.pub

任何拥有private_key的人。Pem能够连接到

ssh user@host.com -i private_key.pem

来自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)并将其重新连接到工作实例,然后更改卷上的密钥并将其重新连接到前一个实例。