How to fix “User is not in the sudoers file”

Not every command on Linux is accessible to all users. Some functionality on this operating system has been restricted only to people with root access. A root user can access all the files/folders, including the system’s files. The root user can make major internal changes to the system that a regular account is not authorized to. The “sudo” keyword is utilized in Ubuntu to get the root user’s privileges. While attempting to use this keyword, an error may arise with the statement “User is not in the sudoers file”.

This article will explain why this particular error occurs and what steps anyone can take to resolve this issue.

Methods to Resolve “User is not in the sudoers file” Problem

There exist various causes that can cause this error to be invoked. The following post elaborates in great detail on all the causes and fixes. Before we get to the reasons and their solutions, we must ensure that the root is set to read and write. For this purpose, follow these steps.

Enter the GNU Grub Version of your Ubuntu Operating system by restarting the computer and clicking the shift key. GNU will easily give us access to the root shell so that we can make all the necessary changes to the system to remove this error. The following screen will pop up. Select “Advanced options” and press Enter:

The next step is select the kernel in recovery mode and press enter as shown below:

Once the recovery menu opens up, scroll to the root option as shown below, and press enter as demonstrated:

Press Enter again for maintenance and run the following command:

# mount -o rw,remount /

This command will allow you to be able to make system changes using commands.

Reason 1: The User Doesn’t Exist in the Sudo group

This error is invoked when the user tries to use the “sudo” command but that user is not in the sudo group. This leaves the user not capable of accessing a lot of the root user privileges. The snippet below visualizes the error:

Solution: Insert User into Sudo Group

The resolution to this problem is not complex at all. You simply add the user to the sudo group again. This can be achieved using the command below:

# adduser user sudo

In the code shown above, the “user” represents the username of the user that you intend to insert. Following the syntax, the below-stated command will add the user “itslinuxfoss” to the sudoers group:

# adduser itslinuxfoss sudo

By doing so, the user can be added to the sudoers file.

Reason 2: The User does not have Sudo Privilege

The possibility exists that the /etc/sudoers file was changed. This change could have been to revoke some privileges from the current user.

Solution: Give the User Sudo Privilege

The best fix to this issue is to grant that user sudo privileges once again. To achieve this, open up the file with this command:

# visudo

Add the following line to the file:

#includedir /etc/sudoers.d

The addition of this is demonstrated below:

This should grant Sudo access to the user once again.

Reason 3: Wrong Permissions for /etc/sudoers File

Files are often assigned permissions based on their functionalities. Not all files are capable of reading and writing. A few can write, whereas others are only capable of reading. The sudoers file needs to have read permission so that the system can read from the file. If this is not the case, then the system will be unable to find the user in the sudoers file.

Solution: Assign 0440 Permission to Sudoers File

The best way to resolve this error is to set the sudoers file permission to 0440. This is the permission code for giving the root access to read-only read as compared to the 0777 permission, which would grant write permissions as well, which we do not need. The “44” represents that the owner and group have read rights. To assign this permission, run the command shown below:

# chmod 0440 /etc/sudoers

Once you have executed all the required commands, you need to exit the recovery mode properly. To do this, run the command below:

# exit

Press enter on the “resume” option, and this will reboot your system. After the system is rebooted fully, use sudo with the current user.

Conclusion

Statement “User is not in the sudoers file” occurs due to 3 very common reasons. The user is not part of the sudo group, it is not allowed sudo privileges, or the sudoers file is not readable. These issues can be resolved by adding the user to the sudo group and granting it sudo privileges. Furthermore, the sudoers file can be assigned the 0440 permission, which allows it to be readable. This article has provided an in-depth guide on the reasons and solutions to fix the error “user is not in sudoers file”.