AWS - Create new instance from AMI, but lost keys?

落爺英雄遲暮 提交于 2020-01-05 08:56:08

问题


I have an AMI that I created a few weeks ago. I want to launch a new AWS EC2 instance and provide that AMI as the image. Problem is, I can't ssh into that box now.

I have tried to attach a new key pair when launching the new instance, but it's as if the configuration of the SSH keys on the box is ignoring my newly attached key pair.

Is there a way to overcome this issue?

I know my new key pair works, I've tested it on a totally blank new instance and I can SSH in just fine.

Any help is greatly appreciated!


回答1:


The keypair is only copied to the Amazon EC2 instance the first time that the disk is booted. Since you have already booted from that disk (or a disk from which the image was made), it will not copy the keypair again.

You will need to copy a new keypair to the /home/ec2-user/.ssh directory. This can be done by attaching the EBS volume to a different instance:

  • Stop the instance (Instance 1)
  • Detach the EBS volume (make a note of the device identifier, eg /dev/xvda)
  • Attach the EBS volume to another Linux instance (Instance 2)
  • Edit the /home/edc2-user/.ssh/authorized_keys file and insert your desired public key
  • Detach the EBS volume from Instance 1, re-attach it to Instance 2
  • Start Instance 1 and ssh to it using your new keypair

See documentation: Connecting to Your Linux Instance if You Lose Your Private Key



来源:https://stackoverflow.com/questions/42308951/aws-create-new-instance-from-ami-but-lost-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!