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.
Question
Answer
This answer is useful in the case you no longer have SSH access to the existing server (i.e. you lost your private key).
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
Here is what I did, thanks to Eric Hammond's blog post:
- Stop the running EC2 instance
- Detach its
/dev/xvda1volume (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 mkdir /mnt/tmp; sudo mount /dev/xvdf1 /mnt/tmp
- Copy
~/.ssh/authorized_keysto/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
.pemfile
That's it.