Linux Commands

How to Fix ‘User Is Not in the Sudoers File’ Error?

In Ubuntu and its derivatives, when you try to execute the Linux commands as a regular user with administrative “sudo” privileges, you may receive the “Username is not in the sudoers file. This incident will be reported” error. This error message blocks the ordinary users from running the administrative commands on your Linux system.

Causes of the “Username Is Not in the Sudoers File” Error

This error message can be due to several reasons that we listed:

  1. If you try to run an administrative command with “sudo” but your username on your system is not recognized as admin or doesn’t have permission to execute such commands.
  2. If the current user is not included in the sudoers file. The sudoers file is a system configuration file that includes roles for all recognized users and groups that are allowed to run the “sudo” command to perform the administrative tasks. So, if your username is not included in this file and you try to perform an administrative action with the “sudo” command, it blocks your access and shows the “username is not in the sudoer file” error message on the terminal.

How to Resolve the “User Is Not in the Sudoers File” Error

There are several solutions available to fix this “username is not in the sudoers file” error which we will discuss in this section.

Solution 1: Add a User to the Sudo Group

The first solution is to add a user to the sudo group. If the user is not yet added to the sudo group. It is important that to add a user in the “sudo” group; you should have administrative or root privileges. To view the sudoers file content, you can use the “cat” command:

$ cat /etc/sudoers

Add the username to the sudo group using the following command:

# adduser username sudo

For example, we have an Ubuntu user with username “samreenaaslam” that we added to a sudo group:

# adduser samreenaaslam sudo

Solution 2: Add a Username in the Sudoer File

If the user is not in the “/etc/sudoers” file or the file was modified to stop adding members to the admin or sudo groups from gaining superuser privileges, you can add the username to the “/etc/sudoer” file in this case. Use the following command to open this file in a text editor:

# visudo

or

$ sudo visudo

Now, the file content will display inside your terminal. Locate these lines and add the username to grant the privileges:

# User privilege specification

root ALL=(ALL:ALL) ALL

Username ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges

%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command

%sudo ALL=(ALL:ALL) ALL

# See sudoers(5) for more information on "#include" directives:

@includedir /etc/sudoers.d

Solution 3: Change the Permissions on Sudoers File

This error may be due to missing file permissions that are not set to 0440. To set the permissions on the “/etc/sudoers” file, use the following command:

# chmod 0440 /etc/sudoers

or

sudo chmod 0440 /etc/sudoers

Note: If you are using CentOS or RPM-based distribution, add the users in the wheel group to fix this error and make changes in the respective files.

Conclusion

That’s all the solutions to resolve the “username is not in sudoers file” error. If you encounter this type of error on your Linux system, especially on Debian-based distribution, use the provided methods to fix this error. We hope that this guide will help you in troubleshooting your sudoer file errors.

About the author

Samreena Aslam

Samreena Aslam holds a master’s degree in Software Engineering. Currently, she's working as a Freelancer & Technical writer. She's a Linux enthusiast and has written various articles on Computer programming, different Linux flavors including Ubuntu, Debian, CentOS, and Mint.