sudo
is a command-line utility designed to allow trusted users to run commands as another user, by default the root user.
You have two options to grant sudo access to a user. The first one is to add the user to the sudoers file.
This file contains information that defines which users and groups are granted with sudo privileges, as well as the level of the privileges.
The second option is to add the user to the sudo group defined in the sudoers
file.
By default, on RedHat based distributions like CentOS and Fedora, members of the “wheel” group are granted with sudo privileges.
Adding User to the wheel Group
The easiest way to grant sudo privileges to a user on CentOS is to add the user to the “wheel” group. Members of this group are able to run all commands via sudo
and prompted to authenticate themselves with their password when using sudo
.
We’re assuming that the user already exists.
To add the user to the group, run the command below as root or another sudo user. Change “username” with the name of the user that you want to grant permissions to.
usermod -aG wheel username
Granting sudo access using this method is sufficient for most use cases.
To test the sudo access, run the whoami
command:
sudo whoami
You will be prompted to enter the password. If the user have sudo access, the command will print “root”:
root
If you get an error saying “user is not in the sudoers file”, it means that the user doesn’t have sudo privileges.