sudo command (originally standing for superuser do) is a program designed to allow a sysadmin to give certain users the ability to run some (or all) commands as
root while logging the commands and arguments.
sudo command, users can install, update, and remove packages and edit configuration files.
This is very useful when you do not want to give a user a root password or if you want to allow a user to do something (i.e. su) without logging in as root.
By default, the sudo command is disabled for standard users for security reasons to prevent any accidental overwriting of system files or any unauthorized root access.
In this article, we will explain how to create a sudo user in CentOS by adding them to the wheel group.
Table of Contents
Create a Sudo User in CentOS
The following steps guide you in creating a sudo user in CentOS. If you’re adding an existing user to the sudo group, then skip steps 1 and 2, and start directly from step 3.
Step 1: Login as a Root User
To create a sudo user in CentOS, first log in as a root user via the SSH protocol:
ssh [email protected]_ip_address
Step 2: Create a New User Account
Create a new user account by using the adduser command, which allows you to add a new user to your Linux machine. You can also modify the configuration files of the user to be created. In the following command, change the username to the name of the new user:
The adduser command will create a home directory (
/home/username) and copy multiple configuration files from /etc/skel directory to it. Within the home directory, users can write, edit, and delete files and directories.
After adding the new user, you need to create a password using the
sudo passwd username
Make sure that the password is strong and unpredictable. A strong password should be a combination of letters, numbers, and special characters.
Changing password for user username. New password: Retype new password: passwd: all authentication tokens updated successfully.
Step 3: Add The User to The Wheel Group
In CentOS, you can add users to the wheel group to create sudo users. You can do it by running any one of the following commands:
- usermod: modifies attributes of an already created user account
- gpasswd: used to administer
/etc/gshadow. When paired with -a, it can define group administrators.
sudo usermod -aG wheel username
Alternatively, you can use the gpasswd command:
sudo gpasswd -a username wheel
Users of the wheel group have sudo privileges by default.
Step 4: Test The Sudo Access
Now, that you’ve created the sudo user, the next step is to test their sudo access. To test sudo access, start by switching to the new sudo user account:
su - username
As the new user, verify your sudo access by typing sudo and append any other command:
For example, try to access the /root directory, which is only accessible to the root users:
sudo ls -la /root
The first time you log in as the superuser, you will be asked to enter the user password. If the username and password are correct, then all sudo commands will run with root privileges.
You’ll also be reminded reminded of the usual lecture regarding using sudo:
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 username:
We hope this article helped you create sudo users in CentOS to safely perform administrative tasks.
If you encountered any issues or have any questions then feel free to leave a comment or to contact us.