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/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 mkdir /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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…