Sudoers file seems that has error:

sudo: >>> /etc/sudoers: syntax error near line 56 <<<
sudo: parse error in /etc/sudoers near line 56
sudo: no valid sudoers sources found, quitting
sudo: unable to initialize policy plugin

I can connect to EC2 via SSH as ec2-user but can not edit sudoers file in order to fix the error.

Tried 'visudo':

visudo: /etc/sudoers: Permission denied
visudo: /etc/sudoers: Permission denied

Tried 'pkexec visudo':

pkexec visudo
Error executing command as another user: No authentication agent was found.

What can I do at this point in order to fix /etc/sudoers file?



As this is an EC2 image and you do not have access to the console, you would have to open a ticket with Amazon Support to see if they can fix your sudoers file. I would prepare for the fact that 1) it will take Amazon a while to get to your request and 2) they may not be able to fix the problem. If either are true, you may have to resign yourself to the fact that you will have to terminate and rebuild your instance.

  • That's what I was afraid of... – Joe Jan 19 '18 at 14:54

As this is an EC2 instance, You'll need to contact Amazon and open a ticket to have someone go into single user mode and correct the improper syntax in the /etc/sudoers file. There isn't anything that you can do on your end as the system will need to be rebooted.

  • This is EC2 instance (virtual owned by AWS).. How can I interrupt reboot in order to get to the screen to press ESC and go into menu selection? When I type reboot - I get reboot: Need to be root (and I can not do sudo because of corrupted file). – Joe Jan 19 '18 at 4:26
  • My mistake. I'll edit to address this. – Nasir Riley Jan 19 '18 at 4:31

If you have this issue in an AWS EC2 instance You can edit the /etc/sudoers file by following the steps below which I have used to recover from this issue on RHEL EC2 instances on many occasions:

  1. Open EC2 Dashboard
  2. Stop the instance.
  3. Detach the root volume (example /dev/sda1) instance A.
  4. Attach the volume to another EC2 instance B (sudo mkdir /recoversudo)
  5. Create a mount point and mount the volume (eg. sudo mount /dev/xvdg2 /recoversudo)
  6. Temporarily add write permissions to the sudoers file /recoversudo/etc/sudoers (sudo chmod 660 /recoversudo/etc/sudoers)
  7. Edit, make your changes and save (sudo vi(m) /recoversudo/etc/sudoers)
  8. Revert permission to read-only (sudo chmod 440 /recoversudo/etc/sudoers)
  9. Unmount the volume (sudo umount /dev/xvdg2) from instance B
  10. Attach back the volume to instance A as root volume (eg. /dev/sda1)
  11. Start instance A and you should be able to login

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.