天天看点

更改EC2实例的密钥对

本文翻译自:Change key pair for ec2 instance

How do I change the key pair for my ec2 instance in AWS management console?

如何在AWS管理控制台中为我的ec2实例更改密钥对?

I can stop the instance, I can create new key pair, but I don't see any link to modify the instance's key pair.

我可以停止实例,可以创建新的密钥对,但是看不到任何修改实例密钥对的链接。

#1楼

参考:https://stackoom.com/question/X4KT/更改EC-实例的密钥对

#2楼

This answer is useful in the case you no longer have SSH access to the existing server (ie you lost your private key).

如果您不再具有对现有服务器的SSH访问权限 (即,丢失了私钥) ,则此答案很有用。

If you still have SSH access, please use one of the answers below.

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

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

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

Here is what I did, thanks to Eric Hammond's blog post:

感谢Eric Hammond的博客文章,这是我的工作:
  1. Stop the running EC2 instance 停止正在运行的EC2实例
  2. Detach its

    /dev/xvda1

    volume (let's call it volume A) - see here 分离其

    /dev/xvda1

    卷(我们将其称为卷A)- 参见此处
  3. Start new t1.micro EC2 instance, using my new key pair. 使用我的新密钥对启动新的t1.micro EC2实例。 Make sure you create it in the same subnet , otherwise you will have to terminate the instance and create it again. 确保在同一子网中创建它 ,否则,您将不得不终止实例并再次创建它。 - see here - 看这里
  4. Attach volume A to the new micro instance, as

    /dev/xvdf

    (or

    /dev/sdf

    ) 将卷A作为

    /dev/xvdf

    (或

    /dev/sdf

    )附加到新的微型实例。
  5. SSH to the new micro instance and mount volume A to

    /mnt/tmp

    SSH到新的微型实例,并将卷A挂载到

    /mnt/tmp

    $ sudo mount /dev/xvdf1 /mnt/tmp $ sudo安装/ dev / xvdf1 / mnt / tmp
  6. Copy

    ~/.ssh/authorized_keys

    to

    /mnt/tmp/home/ubuntu/.ssh/authorized_keys

    ~/.ssh/authorized_keys

    复制到

    /mnt/tmp/home/ubuntu/.ssh/authorized_keys

  7. Logout 登出
  8. Terminate micro instance 终止微实例
  9. Detach volume A from it 从中分离体积A
  10. Attach volume A back to the main instance as

    /dev/xvda

    将卷A作为

    /dev/xvda

    附加回主实例
  11. Start the main instance 启动主实例
  12. Login as before, using your new

    .pem

    file 使用新的

    .pem

    文件像以前一样登录

That's it.

而已。

#3楼

I went through this approach, and after some time, was able to make it work.

我经历了这种方法,经过一段时间后,它才得以发挥作用。

The lack of actual commands made it tough, but I figured it out.

缺乏实际的命令使操作变得很困难,但我发现了。

HOWEVER - much easier approach was found and tested shortly after:

但是-在此之后不久发现并测试了一种更为简便的方法:
  1. Save your instance as an AMI (reboot or not, I suggest reboot). 将您的实例另存为AMI(是否重启,建议重启)。 This will only work if EBS backed. 这仅在EBS支持的情况下有效。
  2. Then, simply start an instance from this AMI and assign your new Keyfile. 然后,只需从此AMI启动实例并分配新的密钥文件。
  3. Move over your elastic IP (if applicable) to your new instance, and you are done. 将您的弹性IP(如果适用)移到新实例,就完成了。

#4楼

I noticed that when managed by Elastic Beanstalk, you can change your active EC2 key pair.

我注意到,当由Elastic Beanstalk管理时,您可以更改活动的EC2密钥对。

Under Elastic Beanstalk > Configuration > Security, choose the new key from the EC2 key pair drop-down.

在Elastic Beanstalk>配置>安全下,从EC2密钥对下拉列表中选择新密钥。

You'll see this message asking if you're sure:

您会看到此消息,询问您是否确定:
EC2KeyName: Changes to option EC2KeyName settings will not take effect immediately. EC2KeyName:对选项EC2KeyName设置的更改不会立即生效。 Each of your existing EC2 instances will be replaced and your new settings will take effect then. 您现有的每个EC2实例将被替换,新设置将生效。

My instance was already terminated when I did this.

执行此操作时,我的实例已被终止。

It then started, terminated, and started again.

然后启动,终止并再次启动。

Apparently "replacing" means terminating and creating a new instance.

显然,“替换”是指终止并创建一个新实例。

If you've modified your boot volume, create an AMI first, then specify that AMI in the same Elastic Beanstalk > Configuration > Instances form as the Custom AMI ID .

如果您已经修改了启动卷,请首先创建一个AMI,然后在与Custom AMI ID相同的Elastic Beanstalk>配置>实例表单中指定该AMI 。

This also warns about replacing the EC2 instances.

这也警告要更换EC2实例。

After you've modified your EC2 key pair and Custom AMI ID, and after seeing warnings about both, click Save to continue.

修改完EC2密钥对和“自定义AMI ID”之后,并在看到有关二者的警告后,请单击“ 保存”以继续。

Remember that the IP address changes when the instance is re-created so you'll need to retrieve a new IP address from the EC2 console to use when connecting via SSH.

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

#5楼

Run this command after you download your AWS pem.

下载您的AWS pem后,运行此命令。
ssh-keygen -f YOURKEY.pem -y
           

Then dump the output into

authorized_keys

.

然后将输出转储到

authorized_keys

Or copy pem file to your AWS instance and execute following commands

或将pem文件复制到您的AWS实例并执行以下命令
chmod 600 YOURKEY.pem
           

and then

接着
ssh-keygen -f YOURKEY.pem -y >> ~/.ssh/authorized_keys
           

#6楼

This will work only if you have access to the instance you want to change/add the key in. You can create a new key pair.

仅当您有权访问要更改/添加密钥的实例时,此方法才有效。您可以创建一个新的密钥对。

Or if you already have the key pair, then you can paste the public key of the new pair in the authorized_keys file on your instance.

或者,如果您已经拥有密钥对,则可以将新密钥对的公钥粘贴到实例上的authorized_keys文件中。

vim .ssh/authorized_keys

vim .ssh / authorized_keys

Now you can use the private key for that pair and log in.

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

Hope this helps.

希望这可以帮助。

继续阅读