You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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/sda1 since 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:

  1. Create a mount point for the faulty volume:
    sudo mkdir /mnt/recover
    
  2. Mount the volume (replace /dev/xvdf with the device name you chose earlier):
    sudo mount /dev/xvdf /mnt/recover
    
  3. Fix the base /home directory permissions (default should be 755—only root can write, others can read/execute):
    sudo chmod 755 /mnt/recover/home
    
  4. Fix the user's home directory (e.g., /home/ubuntu—default is 700 so only the user can access it):
    sudo chmod 700 /mnt/recover/home/ubuntu
    sudo chown -R ubuntu:ubuntu /mnt/recover/home/ubuntu
    
  5. Fix SSH-related permissions (this is why you couldn't connect—SSH rejects overly permissive .ssh directories):
    sudo chmod 700 /mnt/recover/home/ubuntu/.ssh
    sudo chmod 600 /mnt/recover/home/ubuntu/.ssh/authorized_keys
    
    If you have other users on the instance, repeat steps 4 and 5 for their home directories too.

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/sda1 or /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 /home that 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 /home directory—they'll match a fresh Ubuntu setup.

内容的提问来源于stack exchange,提问作者Shibdas Kumbhakar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 15:07:32