How to fix “user is not in the sudoers file” in CentOS?


In this article we will see why we are getting “user is not in the sudoers file” error and what we can do about it. But before we start, let’s learn some theory. I will be using CentOS 7 for demo.

What is sudo?

sudo is a command that allows you to run other commands as a superuser. In other words, if you are not logged in as root, but need to execute some command that requires superuser privileges, you have to use sudo command.

The following example shows, that you get an error if you try to install software without root privileges:

[orkhans@localhost ~]$ yum install tree
Loaded plugins: fastestmirror, langpacks
You need to be root to perform this command.

The following command sudo yum install tree should execute the command with elevated privileges and install the software.

Who are sudoers?

There’s one important thing about using sudo in CentOS: the user can use sudo command only if that user belongs to wheel group, otherwise, we get error “user is not in the sudoers file”.

[orkhans@localhost ~]$ sudo yum install tree

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for orkhans: 
orkhans is not in the sudoers file.  This incident will be reported.

How to add user to sudoers ?

Now let’s fix it and add an existing user to sudoers file. Log into the server as root. Then add your user to wheel group.

[root@localhost ~]# usermod -aG wheel orkhans

The previous command adds user orkhans to wheel group. Now user orkhans can run commands with elevated privileges and the following command runs smoothly sudo yum install tree.

By the way, you can make the new user a sudoer during installation of CentOS, if you make him an administrator, like shown on the picture below:

How to edit /etc/sudoers file?

If you decide to edit /etc/sudoers file, you should only use special utility for this visudo. It uses standard vi editor, but makes sure you don’t break the file, through some basic syntax check.  

Run visudo command and it will open /etc/sudoers file where you will find the following lines:

## Allows people in group wheel to run all commands
%wheel ALL=(ALL) ALL

The %wheel ALL=(ALL) ALL should be uncommented, otherwise adding user to the wheel group won’t have any effect.


Now you know how to add your user to sudoers and run commands with elevated privilege.

Add a Comment