Question

I've launched a CentOS 6.4 AMI and configured it to act as a base instance for our application. I'm now trying to create an AMI from this which can be brought up as required.

Unfortunately when a new instance is created from the AMI the root user's key pair remains that which was assigned to the original template instance when it was created from a CentOS AMI. I was hoping that AWS would be able to assign a new key pair to each instance of the AMI as it's created. Although we won't be using the root key pair for day-to-day work on the instances, I'd prefer they didn't all share the same key.

Despite creating a new key pair at launch time I'm only able to log in using the original key. I've tried several solutions including removing the authorized_keys for the root user, but to no avail.

I'd appreciate any advice you can provide.

Thanks.

Was it helpful?

Solution 2

  1. Stop the running EC2 instance

  2. Detach its /dev/sda1 volume (let's call it volume A)

  3. Start new t1.micro EC2 instance, using my new key pair

  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

  6. Copy ~/.ssh/authorized_keys to /mnt/tmp/home/ubuntu/.ssh/authorized_keys

  7. Logout

  8. Terminate micro instance

  9. Detach volume A from it

  10. Attach volume A back to the main instance as /dev/sda1

  11. Start the main instance

  12. Login as before, using your new .pem file

OTHER TIPS

I ran into this issue as well, with my Centos 6.5 image.

Here's how I fixed it:

Install the EPEL repo

wget http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
rpm -ivh epel-release-6-8.noarch.rpm

Install the cloud-init package

yum install cloud-init

Add a user to the box called "cloud-user"

Add cloud-user to the sudoers file with the no password option

cloud-user  ALL=(ALL)       NOPASSWD: ALL

Stop the instance, and create the AMI.

When you launch an instance from that AMI you can choose any key you want, and when you login you will just use the following command

ssh -i yourkeyfile.pem cloud-user@your.ip.addy

To become root, just type the following

sudo -i

Cheers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top