How to fix “User is not in the sudoers file” error on CentOS 8

Linux is a multi-user operating system. Here, the root user has complete control over the entire system. For security reasons, it must be protected with a password. In some cases, the root user is even disabled. CentOS has root user enabled by default.

In addition to security concerns, root access is required when required for system maintenance. Command sudo does exactly that. It grants root privilege to the command / script while it is being executed. Users with access to the command sudoare controlled by the sudoers file.

This article will show you how to fix the CentOS 8 “User is not in the sudoers file” error.

Error

The sudoers file controls the user rights for the command sudo… This error appears whenever a user without permission sudo trying to run the command sudo…

Here user “andreyex” does not have permission to command sudo…

Bug fix

The solution is to add the user to the sudoers file. However, the sysadmin must decide whether to give the user access to the command sudo…

There are several ways to approach this solution. However, all of them require prior privilege sudo… This action is reserved for the system administrator only.

If you are using a local system, you will need root access to do the following:

Log in as root.

$ su - root

The root account is password protected. What happens if you forget your root password? Sounds like the end of the world, but you can even recover the root password. Learn how to recover root password on CentOS.

Adding a user to the Wheel group

This is the most efficient way to grant a user access to privileges. sudo…

Linux ships with a user group wheel by default. The wheel group has permission to perform any action on the system with privileges. sudo… Instead of adding individual users, adding them to the wheel group makes it easier to grant access to privileges. sudo…

You can check the status of the wheel group in the sudoers file.

$ sudo vim /etc/sudoers

Here we will add the custom “grayex” to the wheel group.

$ sudo usermod -a -G wheel andreyex

How can I check the result? Run the following command. It will print the users registered in the “wheel” user group.

$ getent group wheel

Alternatively, try running the command sudo on behalf of the user.

$ sudo dnf check-update

Manually adding a user to Sudoers

Instead of using the “wheel” group, we can directly declare the user in the sudoers file with the privileges sudo…

However, this is not the best way to achieve this. If more than a couple of users need to be added, then managing all users can be tricky. Revisiting the sudoers file can be tedious.

Run the following command to open the sudoers file. Here, the “EDITOR” environment variable defines the text editor that the visudo command will use. This is the recommended and safe way to work with the sudoers file.

$ sudo EDITOR=vim visudo

Add the following line. Here we will give the user “blade” access to all parts of the system with privileges sudo…

$ andreyex ALL=(ALL:ALL) ALL

Save the file and close the editor. The user “andreyex” now has the same privileges as the root user.

Sudoers File Resolution

In Linux, file permissions are one of the main properties of a file. It describes which user has read, write, and execute permissions on a file or directory. A corrupted file permission can lead to unexpected behavior leading to this error. Learn more about file permissions.

The following command will reset the permissions on the sudoers file.

$ chmod 0440 /etc/sudoers

Reboot the system for the changes to take effect.

Conclusion

This is a simple mistake with a simple solution. These methods should work fine as long as you have access to the administrator account. If you are working in a corporate environment, ask your system administrator to grant the rights sudo for the desired user account.

Related Posts