Troubleshooting sudoers files

Linux

The page provides tips on using thesudo command-line utility, managing thesudoers plugin, and preventing or fixing issues that arise.

Causes of problems

During each execution of thesudo command, the following process takes placeto validate thesudoers files:

  • The syntax is checked for correctness.
  • The content is analyzed to exclude some of the logical errors.
  • Ownership and permissions are checked.

The validation of thesudoers files might fail due to any of the followingerrors:

Syntax errors

You must follow specific syntax rules when you make changes to thesudoersfiles. Any deviation from this syntax, including but not limited to amissing or extra character or an inappropriate comma, can make the fileinvalid. Invalidation of the file makes it impossible to use thesudoutility.

Solution

The solution is to usevisudo utility to edit thesudoers files. Itvalidates the file content before saving and notifies in case of issues. Thevisudo utility was created for editing the file in a safe fashion.


The following examples shows both correct and incorrect syntax samples:

Correct syntax

user   ALL=(ALL) ALL

Incorrect syntax

user   ALL=(ALL), ALL

Syntax error example

$ sudo useradd username/etc/sudoers:20:17: syntax erroruser   ALL=(ALL), ALL                ^

Logical errors

Errors of this type can be caused by one of the following:

  • A misunderstanding of the principles of thesudoers plugin.
  • Deviations from the correct syntax.

However, logical errors are not recognized during validation, because theydo not violate syntax rules and therefore are tricky to detect.

Solution

You must carefully read the official documentation and adhere to itsprinciples when you edit the file.

Google also recommends that you use thevisudo utility to edit thesudoers files, as it can detect some types of logical errors, such as:

  • Undefined or unused aliases
  • Cyclic references
  • Duplicate entries

If any issues are detected, you see a warning message.


The following examples shows both logically correct and incorrect samples:

Logically correct

barbara   ALL=(ALL:ALL) /usr/bin/ls

Logically incorrect

barbara   ALL=(4LL:ALL) /usr/bin/ls               ^barbara   ALL=(ALL;ALL) /usr/bin/ls                  ^bar6ara   ALL=(ALL:ALL) /usr/bin/1s   ^                             ^

Incorrect permissions

In addition to errors caused by the content of thesudoers files, theirexcessive file permissions or incorrect ownership can also cause thesudoutility to fail.

Solution

You see a description of these errors in the output of the failedsudocommand. Read through the error message description and make the necessarycorrections.


The following is an example of the correctfile permissions and ownership

$ ls -l /etc/sudoers-r--r----- 1 root root 700 Jan 1 12:00 /etc/sudoers$ sudo useradd username

The following example shows the errorthat is displayed when there are redundant permissions for theall userspermission group:

$ ls -l /etc/sudoers-r--r---w- 1 root root 700 Jan 1 12:00 /etc/sudoers$ sudo useradd usernamesudo: /etc/sudoers is world writablesudo: no valid sudoers sources found, quittingsudo: error initializing audit plugin sudoers_audit

The following example shows the error thatis displayed for incorrect ownership. In this example, a user with an IDthat is not0 (or a user that is notroot) is the owner of the file:

$ ls -l /etc/sudoers-r--r----- 1 user user 700 Jan 1 12:00 /etc/sudoers$ sudo useradd usernamesudo: /etc/sudoers is owned by uid 1000, should be 0sudo: no valid sudoers sources found, quittingsudo: error initializing audit plugin sudoers_audit

For more information about the configuration of thesudoers files, readSudoers Manual.

To learn how to manage and use thevisudo editor, readVisudo Manual.

Consequences of problems

Issues in thesudoers files cause negative effects and can affect thefunctionality of the entire system.

Recovery in case of problems

If you lose elevated user privileges or can't use thesudo command due toproblems with thesudoers files, then use the superuser account for recovery.

In Unix-like operating systems, the superuser is a special user account with IDequal to 0, that is usually calledroot. The superuser has full access to thesystem resources, and can perform any administrative task without restrictions.While interacting with the OS on behalf of the superuser is generally consideredinsecure, it may be the only option for certain tasks such as recovery of thesudoers files.

Logging in directly as the superuser exposes the OS to risk. To avoid this risk,Google recommends that you use a startup script functionality, as this script isexecuted on behalf of the superuser.

Read more about Compute Enginestartup scripts.

To recover thesudoers files with a startup script, do the following:

  1. Create a backup copy of the current startup script if it's already in use.The backup approach depends on how the startup script is configured.

  2. Update the startup scriptwith the following command sequence:

    mv /etc/sudoers /etc/sudoers.backup.$(date +"%s") && echo "%google-sudoers ALL=(ALL:ALL) NOPASSWD:ALL" > /etc/sudoers && chown 0:0 /etc/sudoers && chmod 0440 /etc/sudoers

    Read more about what the commands does

    mv /etc/sudoers /etc/sudoers.backup.$(date +"%s")

    This command creates a copy of the/etc/sudoers file with a different name and deletes the original file. The name of the new file contains a timestamp at the end for uniqueness (e.g.sudoers.backup.1672527600).

    echo "%google-sudoers ALL=(ALL:ALL) NOPASSWD:ALL" > /etc/sudoers

    This command creates previously deleted file/etc/sudoers with a single rule that allows authorized Google Cloud users with access to the VM to execute any commands on behalf of any system user. This rule always exists by default in an additional file/etc/sudoers.d/google_sudoers.

    chown 0:0 /etc/sudoers

    This command sets the owner of the/etc/sudoers file to a user whose ID is0 and a group of owners to a group whose ID is0.

    chmod 0440 /etc/sudoers

    This command sets permissions for the/etc/sudoers file to read-only and allows only its owner and owner group to read the file.

    Note: After these manipulations, the/etc/sudoers file can still be modified by authorized users of the system using thevisudo utility.

  3. Stop the VM, if it's running. Restart the VM totrigger the execution of the startup script.

  4. Сonnect to the VM and edit thebrokensudoers file to recover it.

    sudovisudo/etc/sudoers.backup.TIMESTAMP
    Note: Instead of fixing thesudoers file manually, you cancreate a new VM instance with thesame boot disk image and copy the correct file from there.
  5. Save the changes and replace the current/etc/sudoers file with the fileyou just edited.

    sudomv/etc/sudoers.backup.TIMESTAMP/etc/sudoers
  6. Make sure that the original issue with using thesudo command and elevatedprivileges has been fixed.

  7. Remove the temporary startup script and restore the original one if it wasused.

    Note: If the temporary startup script remains, the content of the/etc/sudoers file is overwritten each time the VM is restarted.

What's Next?

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.