Lost sudo/su on Amazon EC2 instance

Solution 1:

In that kind of situation I think you should be able to use a second instance to fix the problem:

  • Detach the EBS disk containing the broken system
  • Create another EC2 instance
  • Attach & mount the disk to the new instance
  • Fix the permissions
  • Umount, detach & reattach to the original instance

Solution 2:

  1. Stop your current instance
  2. Detach Existing Volume
  3. Create a new Volume
  4. Attach new volume to your instance as "/dev/xvda"
  5. Start your instance
  6. Attach the old volume (one which has sudo privilege issue) back to the instance while it is running as "/dev/sdf"
  7. Login to your instance using putty
  8. Use the lsblk command to view your available disk devices and their mount points (if applicable) to help you determine the correct device name to use.

      ec2-user ~$ lsblk
      NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
      xvdf 202:80 0 100G 0 disk
      xvda1 202:1 0 8G 0 disk /
    

The output of lsblk removes the /dev/ prefix from full device paths. In this example, /dev/xvda1 is mounted as the root device (note the MOUNTPOINT is listed as /, the root of the Linux file system hierarchy), and /dev/xvdf is attached, but it has not been mounted yet.

  1. Use the following command to create a mount point directory for the volume. The mount point is where the volume is located in the file system tree and where you read and write files to after you mount the volume. Substitute a location for mount_point, such as /data.

    sudo su
    cd /mnt
    mkdir other
    mount /dev/xvdf other
    cd /
    chown -R root:root /mnt/other/etc/
    exit
    
  2. Go back to AWS and stop the instance

  3. Detach both the volumes and re-attach old (now fixed) volume as /dev/xvda
  4. Start your instance and now your permissions should be back to what it was