Search code examples
amazon-web-servicessshcentospublic-key-encryptionamazon-ami

AWS CentOS Root Keypairs


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.


Solution

    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