Causes of the “Username Is Not in the Sudoers File” Error
This error message can be due to several reasons that we listed:
- 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.
- 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:
Add the username to the sudo group using the following command:
For example, we have an Ubuntu user with username “samreenaaslam” that we added to a sudo group:
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:
or
Now, the file content will display inside your terminal. Locate these lines and add the username to grant the privileges:
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:
or
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.