3

I accidentally made a mistake in the sudoers file, and now I can't fix it because if I try to change the file it give a permission denied message. If I use sudo to open the file, then it says syntax error and no valid sudoers sources found and won't execute the command. I'm on raspbian, a flavor of debian.

Does anyone know how to get out of this catch-22?

  • 3
    USE visudo that's what it's for. NEVER edit sudoers directly. – coteyr Sep 29 '16 at 22:17
5

You're going to have to boot into single user mode.

https://serverfault.com/questions/482079/debian-boot-to-single-user-mode

As the root user, you'll be able to edit the sudoers file to fix it. I highly recommend using the visudo command to edit your sudoers file in the future, to prevent having to go through this again, as visudo does a syntax check on the file before saving it.

  • Thanks for the visudo advice, but I don't know how to boot into single user mode. The only pre-boot option I have takes me to a graphical window where I can only edit config.txt and cmdline.txt – jath03 Sep 29 '16 at 18:35
7
  1. If you know the root password (and it is set) you can use su to became root and edit /etc/sudoers.
  2. If your root filesystem is on SD-card, you can push it out and edit /etc/sudoers on PC or any other device, provided it have card reader.
  3. You can enter U-Boot (bootloader) command line (usually by pressing a button on keyboard or via the UART) and add 1 to Linux kernel command line to enter single user mode. In this mode you will be root and could edit /etc/sudoers. See also this.
  • I haven't set a root password yet, but I will try the other options. – jath03 Sep 29 '16 at 18:36
  • It seems cmdline.txt is what you need. Add literal 1 at the end and you will go to single user mode. Do not forget to delete it after. – Anton Leontiev Sep 29 '16 at 19:14
  • Yes it worked!1 – jath03 Sep 29 '16 at 20:09
0

If you are on a cloud instance you are in a bit more trouble.

There are techniques depending on distro, but if you have Docker already installed and running, then you can generally go through a Docker container to fix the problem. That is because, usually, the Docker service is running as root and anything done to a mounted volume inside the container is affected as root on the host.

On host:

docker run --rm -ti -v /etc:/usr/local/etc busybox sh

The "busybox" is just a nice small distro that won't take long to download. If you already have a Docker image that has a shell available in it, just use that.

On Docker Container:

cd /usr/local/etc
vi sudoers

You may have added a bad file to the /etc/sudoers.d/ directory. In which case, probably just remove that file.

When you exit the container, you can sudo again.

Whew!

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.