更改 ec2 实例的密钥对 [英] Change key pair for ec2 instance

查看:35
本文介绍了更改 ec2 实例的密钥对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I change the key pair for my ec2 instance in AWS management console? 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.

推荐答案

这个答案很有用如果您不再拥有对现有服务器的 SSH 访问权限(即您丢失了私钥).

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

如果您仍然可以访问 SSH,请使用以下答案之一.

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

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

这就是我所做的,感谢 Eric Hammond 的博文:

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

  1. 停止正在运行的 EC2 实例
  2. 分离其 /dev/xvda1 卷(我们称之为卷 A) - 见这里
  3. 使用我的新密钥对启动新的 t1.micro EC2 实例.确保在同一子网中创建它,否则您将不得不终止实例并重新创建它.- 见此处
  4. 将卷 A 附加到新的微实例,作为 /dev/xvdf(或 /dev/sdf)
  5. SSH 到新的微实例并将卷 A 挂载到 /mnt/tmp
  1. Stop the running EC2 instance
  2. Detach its /dev/xvda1 volume (let's call it volume A) - see here
  3. Start new t1.micro EC2 instance, using my new key pair. 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)
  5. SSH to the new micro instance and mount volume A to /mnt/tmp

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

  1. ~/.ssh/authorized_keys复制到/mnt/tmp/home/ubuntu/.ssh/authorized_keys
  2. 退出
  3. 终止微实例
  4. 从它分离卷 A
  5. 将卷 A 作为 /dev/xvda
  6. 附加回主实例
  7. 启动主实例
  8. 使用新的 .pem 文件像以前一样登录
  1. Copy ~/.ssh/authorized_keys to /mnt/tmp/home/ubuntu/.ssh/authorized_keys
  2. Logout
  3. Terminate micro instance
  4. Detach volume A from it
  5. Attach volume A back to the main instance as /dev/xvda
  6. Start the main instance
  7. Login as before, using your new .pem file

就是这样.

这篇关于更改 ec2 实例的密钥对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆