When you do a fresh install of CentOS on your laptop and try to run any command using sudo, you will likely get this error message: “user is not in the sudoers file. This incident will be reported.”
This post will give you a comprehensive guide on how to add users to sudoers in CentOS.
For example, if you try running the sudo dnf update
command, you will get an error, as shown in the image below.
Table of Contents
What is SUDO?
Every Linux distribution comes with a user called root
who has permission to do anything they want on the system. It is somehow similar to the administrator user on Windows machines. The root user can perform various tasks, including:
- Creating, deleting, and writing to any files on the system.
- Update and upgrade the system and software packages.
- Assign permissions to other users.
- Add or delete users.
- Install and uninstall software
- The root user can also install viruses and malware.
In simple terms, the root user has complete control over the system. If you have created a regular user and want them to perform actions with root privileges, you need to use “sudo” in your command. SUDO, which stands for “Super User Do,” is a command-line utility that allows regular users to execute commands as the root user.
Unfortunately, not every user can call the sudo
command in a distribution like CentOS or Debian. For a user to use the SUDO command, they need to be in the wheel group or the sudoers file.
Therefore, when you try running a command and get the error “user is not in the sudoers file. This incident will be reported,” it means the user is not in the Wheel group or the sudoers file.
Solution 1: Add User to the Sudoers File
The most straightforward solution is adding the user to the sudoers file, which is found in the /etc
directory. The sudoers file is used to allocate system privileges to users and groups. Unfortunately, editing this file also requires root privileges that your user doesn’t have currently.
So how do you go about it? Follow the steps below.
- You first need to drop to the root user by executing the command below on the command line.
su -
Please take note of the hyphen in the command above. You will see a prompt to enter the root password you set when installing CentOS. After successfully logging in, you will notice the Terminal prompt change from your current user to root.
- You need to edit the sudoers file and add your user. Execute the command below.
nano /etc/sudoers
- Scroll to the bottom of the file and add the line below.
<user-name> ALL=(ALL) ALL
E.g.,
Johndoe ALL=(ALL) ALL
- When done, save the file (Ctrl + S) and exit (Ctrl + X). To return to your user, type exit on the Terminal and hit Enter.
- To verify whether the user has root privileges, try executing any commands using the SUDO command. For example, sudo yum update. You are good to go if you don’t get any errors.
Tips and Tricks of Adding User to Sudoers File.
Instead of adding a user directly to the /etc/sudoers
file, you can create a new directory called /etc/sudoers.d
and store individual files for every user inside this directory. These files will be automatically added to the sudoers file.
For example, you can create a file called user_one inside the /etc/sudoers.d
directory and add the rules as you would in the sudoers file.
cd /etc mkdir sudoers.d cd sudoers.d nano user_one
In the image above, we created a file called user_one in the /etc/sudoers.d
directory and added the rule:
johndoe ALL=(ALL) ALL
You can also set rules that limit the commands a user can execute using sudo. For example, if you wanted the user to only run the du
and ping
command using sudo, you would write your rule as follows.
<user_name> ALL=(ALL) ALL:/usr/bin/du,/usr/bin/ping
E.g.,
johndoe ALL=(ALL) ALL:/usr/bin/du,/usr/bin/ping
If you want a user to run commands with sudo
without being prompted to enter a password, you will need to use the NOPASSWD
attribute as shown below.
<user-name> NOPASSWD=(ALL) ALL
E.g.,
johndoe NOPASSWD=(ALL) ALL
Solution 2: Add the User to the Wheel Group.
Linux systems have a unique feature that lets you put users together in groups. Each group has a particular set of permissions. For example, users in a Group_A may have permissions like read and write but not execute. Check out our posts – How To Create and Manage Groups in Linux.
In Linux systems, members of the Wheel group can run the commands using the SUDO
command and authenticate themselves using their login password.
Follow the steps below to add your user to the Wheel group in CentOS. In our case, we want to add the user Johndoe
.
- To add a user to the wheel group, you need to use the usermod command. Unfortunately, this command also requires root privileges which your user doesn’t have currently. To solve this, you will need to switch to the root user by executing the command below on the Terminal.
su -
Tip: Please take note of the hyphen. This command will prompt you to enter the root password you set when installing CentOS.
After you have dropped to the root shell, execute the - After you have dropped to the root shell, execute the command below to add your user to the wheel group.
usermod -aG wheel <your-user>
E.g.,
usermod -ag wheel johndoe
- After executing the command without errors, type exit and hit enter to log out the root user, as shown in the image above.
Conclusion
This post has given you two solutions that you can use to add users to sudoers in CentOS. If you want to create a user who already has sudo
privileges, please check out our post How to Create a Sudo User in CentOS. If you have any questions or encountered any issues, please don’t hesitate to let us know in the comments below.