1 Answer
- Newest
- Most votes
- Most comments
0
Could you please follow the below troubleshooting steps
Steps to Regain Access to EC2 Instances
-
Stop the Instance:
- Go to the EC2 console: EC2 Console.
- Select the instance you need to access and stop it.
aws ec2 stop-instances --instance-ids i-1234567890ab -
Detach the Root Volume:
- Detach the root EBS volume from the stopped instance.
aws ec2 detach-volume --volume-id vol-1234567890ab -
Attach the Volume to Another Instance:
- Attach the detached volume to another running instance where you have access. Attach it as a secondary volume (e.g.,
/dev/sdf).
aws ec2 attach-volume --volume-id vol-1234567890ab --instance-id i-0987654321fe --device /dev/sdf - Attach the detached volume to another running instance where you have access. Attach it as a secondary volume (e.g.,
-
Access the Attached Volume:
- SSH into the instance where you attached the volume.
ssh -i /path/to/your/key.pem ec2-user@<instance-public-dns>- Mount the attached volume.
sudo mkdir /mnt/recovery sudo mount /dev/xvdf1 /mnt/recovery -
Modify the Necessary Files:
-
Option 1: Reset the Root Password:
- Edit the
/etc/shadowfile on the mounted volume to reset the root password.
sudo chroot /mnt/recovery sudo passwd root exit - Edit the
-
Option 2: Add a New SSH Key:
- Add your SSH public key to the
~/.ssh/authorized_keysfile for the root user.
sudo chroot /mnt/recovery sudo mkdir -p /root/.ssh sudo nano /root/.ssh/authorized_keys # Paste your SSH public key into the file exit - Add your SSH public key to the
-
-
Unmount the Volume:
- Unmount the volume from the recovery instance.
sudo umount /mnt/recovery -
Detach the Volume from the Recovery Instance:
- Detach the volume from the recovery instance.
aws ec2 detach-volume --volume-id vol-1234567890ab -
Reattach the Volume to the Original Instance:
- Reattach the volume to the original instance as the root volume (e.g.,
/dev/sda1).
aws ec2 attach-volume --volume-id vol-1234567890abcdef0 --instance-id i-1234567890ab --device /dev/sda1 - Reattach the volume to the original instance as the root volume (e.g.,
-
Start the Original Instance:
- Start the original instance.
aws ec2 start-instances --instance-ids i-1234567890B -
Access the Instance:
- SSH into the instance using the new root password or the new SSH key you added.
ssh -i /path/to/your/key.pem root@<instance-public-dns>
Relevant content
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 10 months ago

Are the EC2s configured with SSM, if so, at a minimum, you can login with SSM and do your task.