- Newest
- Most votes
- Most comments
To recover access to your EC2 instance after accidentally deleting the /home/ec2-user folder, you can follow these steps:
-
Stop the EC2 instance if it's still running.
-
Create a snapshot of the instance's root EBS volume for backup purposes.
-
Detach the root EBS volume from the affected instance.
-
Launch a new temporary EC2 instance in the same Availability Zone.
-
Attach the detached root volume to the temporary instance as a secondary volume.
-
Mount the attached volume on the temporary instance:
sudo mkdir /mnt/temp sudo mount /dev/xvdf1 /mnt/temp
-
Recreate the ec2-user home directory and necessary files:
sudo mkdir -p /mnt/temp/home/ec2-user sudo chown ec2-user:ec2-user /mnt/temp/home/ec2-user
-
Copy the default ec2-user files from the temporary instance:
sudo cp -r /home/ec2-user/. /mnt/temp/home/ec2-user/
-
Ensure correct permissions are set:
sudo chmod -R 755 /mnt/temp/home/ec2-user
-
Unmount the volume:
sudo umount /mnt/temp
-
Detach the volume from the temporary instance.
-
Reattach the volume to your original instance as the root volume.
-
Start your original instance.
-
Try to SSH into your instance using your original key pair.
If you're still unable to access the instance after these steps, you may need to use AWS Systems Manager to regain access:
- Ensure the SSM agent is installed on your instance.
- Configure the necessary IAM role for Systems Manager access.
- Use the AWSSupport-ResetAccess automation document to reset access to your instance.
Remember to regularly back up critical system files and use IAM roles and policies to restrict access to sensitive directories to prevent similar issues in the future.
Sources
EC2 instance /etc folder deleted , couldnt connect now AMI- ami-04b4f1a9cf54c11d0 | AWS re:Post
Community | Recovering Access: A Guide for Lost EC2 Key Pair in Linux
Relevant content
- asked 2 years ago
- AWS OFFICIALUpdated 4 months ago
- AWS OFFICIALUpdated 4 years ago