Besides security concerns, access to root privilege is necessary when it’s required to perform system maintenance. The sudo command does exactly that. It grants a command/script root privilege as long as it is executed. Users having access to the sudo command are managed by the sudoers file.
In this guide, check out how to fix CentOS 8 error “User is not in the sudoers file”.
The sudoers file manages user permission for the sudo command. Whenever a user with no sudo permission tries to run a sudo command, this error will appear.
Here, the user “blade” doesn’t have permission to the sudo command.
Fixing the Error
The solution is to add the user to the sudoers file. However, it’s up to the system administrator to decide whether the user should be granted access to the sudo command.
There are multiple ways of approaching this solution. However, all of them require having prior sudo privilege. It’s an action reserved for the system administrator only.
If you’re running a local system, you need access to the root account to perform these steps:
Login to root.
The root account is password protected. What happens when you forget the root password? It sounds like doomsday, but it’s possible to recover even the root password. Check out how to recover root password on CentOS.
Adding the User to the Wheel Group
This is the most efficient way of granting a user access to sudo privilege.
By default, Linux comes with the user group wheel. The wheel group has permission to perform any action on the system with sudo privilege. Instead of adding individual users, adding them to the wheel group allows an easier way of granting access to sudo privilege.
You can check the status of the wheel group in the sudoers file.
Here, we’ll be adding the user “blade” to the wheel group.
How to verify the result? Run the following command. It’ll print the users registered under the user group “wheel”.
Alternatively, try running a sudo command as the user.
Manually Adding a User to Sudoers
Instead of using the “wheel” group, we can directly declare the user in the sudoers file to have sudo privilege.
However, it’s not the optimal way to achieve it. If more than a couple of users are to be added, then managing all the users can be tricky. Going through the sudoers file repeatedly can be tedious.
Run the following command to open up the sudoers file. Here, the “EDITOR” environment variable determines the text editor that the visudo command will use. It’s the recommended and secure way of tinkering with the sudoers file.
Add the following line. Here, we’ll be granting the user “blade” access to all parts of the system with sudo privilege.
Save the file and close the editor. The user “blade” now has the same privilege as the root user.
Sudoers File Permission
In Linux, file permission is one of the fundamental properties of a file. It describes which user has read, write, and execute permission to the file or directory. Corrupted file permission may lead to unexpected behavior, leading to this error. Learn more about file permissions.
The following command will reset the file permission of the sudoers file.
Reboot the system to implement the change.
It’s a simple error with a simple solution. These methods should work just fine as long as there’s access to an administrative user account. If you’re working in a corporate environment, ask the system administrator to grant sudo privilege to the desired user account.
Do you need to manage multiple users?
Then check out how to create and delete users on CentOS 8.