Being a Linux user, you must be well aware of the keyword “sudo,” which allows the users to execute the root user’s commands. Have you added a new user to your Linux system ever? Well, if you add a user to your Linux system, you may not be able to use the “sudo” keyword with that user, and if you use it, you will get the error “user is not in the sudoers file.”
There are other potential reasons as well that invoke the above-said error. This comprehensive post will state all the reasons and potential solutions to eliminate the error.
All the practical demonstration in this guide is tested on Ubuntu 22.04.
Why Does the “user is not in the sudoers file” Error Occurs?
Like other Linux errors, multiple reasons cause this error, including the user not being added, incorrect syntax, corrupted sudoers file, etc. Let’s look at the reasons, one by one:
User not Added to the sudoers File
By default, only users added to the sudoers file can execute commands with sudo privileges. You will encounter this error if your user account is not added to the sudoers file.
Incorrect Syntax in the sudoers File
If there is a syntax error in the sudoers file, such as a missing comma or incorrect format, it can result in the “user is not in the sudoers file” error.
Corrupted sudoers File
If it is corrupted or contains invalid entries, it can prevent your user account from being recognized as a sudoer.
Permissions Issue With the sudoers File
If the permissions of the sudoers file are not set correctly, it can prevent your user account from accessing and executing commands with sudo privileges.
The error on the terminal screen looks like as shown below:
Solution: Add the User to the sudoers File
The only solution to fix the error is to add the user to access the sudoers file and add the user to it. This will fix the above-said error:
Step 1: Switch to the Root User
As the normal user cannot access the sudoers file; therefore, you need to switch to the root user before accessing the file:
$ su -
Step 2: Add the User to the sudoers File
Now, open the sudoers file via any text editor, i.e., nano, vim:
# nano /etc/sudoers
Or you can open it using the command:
After that, add the below-stated line at the end of the file as follows:
# username ALL=(ALL:ALL) ALL
Replace the username with that you want to add to the file, i.e., linux. After that, save and close the file.
Step 3: Verification
Now, exit from the root user and use any sudo-based command to test whether you are allowed to use the sudo command or not:
$ sudo apt update
That’s how you can fix this sudoers file error.
The “user is not in the sudoers file” error usually occurs when a user account is not added to the sudoers file or if there are syntax errors, corruption, or permission issues with the sudoers file.
The solution is simple: add the user to the sudoers file. First, switch to the root user and open the sudoers file with any text/code editor. After that, add the user’s account with the appropriate syntax at the end of the file. This post has briefly demonstrated the reasons and the solutions to fix the “user is not in the sudoers file” error.
Are you a Linux enthusiast and want to get/learn more troubleshooting guides? Keep visiting Linux Genie.