Skip to content

Instantly share code, notes, and snippets.

@izshreyansh
Created September 12, 2019 14:22
Show Gist options
  • Save izshreyansh/7f7ea43145daefc105c08c12343fbf5c to your computer and use it in GitHub Desktop.
Save izshreyansh/7f7ea43145daefc105c08c12343fbf5c to your computer and use it in GitHub Desktop.
Lost aws public key?
Just do this: 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:
Stop the running EC2 instance
Detach its /dev/xvda1 volume (let's call it volume A) - see here
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
Attach volume A to the new micro instance, as /dev/xvdf (or /dev/sdf)
SSH to the new micro instance and mount volume A to /mnt/tmp
$ sudo mount /dev/xvdf1 /mnt/tmp
Copy ~/.ssh/authorized_keys to /mnt/tmp/home/ubuntu/.ssh/authorized_keys
Logout
Terminate micro instance
Detach volume A from it
Attach volume A back to the main instance as /dev/xvda
Start the main instance
Login as before, using your new .pem file
That's it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment