AWS Ubuntu EC2实例误设/home目录777权限后无法连接的恢复方案咨询
Fixing AWS Ubuntu EC2 After Accidental
sudo chmod -R 777 /home Alright, let's get your EC2 instance back online—this is a classic "oops" moment, but AWS gives us a straightforward way to recover without losing your data. Here's what to do:
Step 1: Prepare the Faulty Instance's Root Volume
- Stop your problematic EC2 instance (don't terminate it—we need the data!).
- Go to the EC2 Console > Volumes, find the root volume attached to your instance (it'll show as "/dev/sda1" or "/dev/xvda" in the "Attachment Information" column).
- Detach this volume from the instance (right-click > Detach Volume).
Step 2: Launch a Temporary Rescue Instance
- Spin up a new Ubuntu EC2 instance in the same Availability Zone as your original instance (this is critical for attaching the volume later).
- Use the same Ubuntu version as your original instance if possible—avoids permission/tool mismatches.
Step 3: Attach the Faulty Volume to the Rescue Instance
- Go back to the Volumes page, right-click your detached root volume > Attach Volume.
- Select your rescue instance from the dropdown, and set the device name to something like
/dev/xvdf(avoid using/dev/sda1since that's the rescue instance's own root volume). - Connect to your rescue instance via SSH.
Step 4: Mount the Volume and Fix Permissions
Once connected to the rescue instance:
- Create a mount point for the faulty volume:
sudo mkdir /mnt/recover - Mount the volume (replace
/dev/xvdfwith the device name you chose earlier):sudo mount /dev/xvdf /mnt/recover - Fix the base
/homedirectory permissions (default should be755—only root can write, others can read/execute):sudo chmod 755 /mnt/recover/home - Fix the user's home directory (e.g.,
/home/ubuntu—default is700so only the user can access it):sudo chmod 700 /mnt/recover/home/ubuntu sudo chown -R ubuntu:ubuntu /mnt/recover/home/ubuntu - Fix SSH-related permissions (this is why you couldn't connect—SSH rejects overly permissive
.sshdirectories):
If you have other users on the instance, repeat steps 4 and 5 for their home directories too.sudo chmod 700 /mnt/recover/home/ubuntu/.ssh sudo chmod 600 /mnt/recover/home/ubuntu/.ssh/authorized_keys
Step 5: Reattach the Volume to the Original Instance
- Unmount the volume from the rescue instance:
sudo umount /mnt/recover - Go back to the Volumes page, detach the volume from the rescue instance, then reattach it to your original instance as the root volume (device name
/dev/sda1or/dev/xvda).
Step 6: Test the Original Instance
- Start your original EC2 instance and try connecting via SSH—it should work now.
- Double-check other permissions if needed (e.g., any custom files in
/homethat might need specific permissions).
Quick Notes
- You can terminate the rescue instance once you're done to avoid unnecessary charges.
- Always double-check device names when attaching/detaching volumes to avoid overwriting the wrong data.
- If you're unsure about default permissions, compare them to the rescue instance's
/homedirectory—they'll match a fresh Ubuntu setup.
内容的提问来源于stack exchange,提问作者Shibdas Kumbhakar
相关产品推荐
相关产品推荐

