Introduction
The sudo command, short for "Super User DO," temporarily grants elevated privileges to a regular user to perform administrative tasks. In CentOS and Rocky Linux, sudo enables users to execute tasks with elevated permissions.
This guide will explain how to add a user to the sudoers list on CentOS and Rocky Linux.
Prerequisites
- A system running CentOS or Rocky Linux.
- Access to a user account with root privileges.
- Access to the terminal.
How to Add Users to Sudo Group in CentOS and Rocky Linux
Managing administrative privileges is essential for maintaining system security and functionality.
By default, CentOS and Rocky Linux have a user group called the wheel group. The wheel group members are automatically granted sudo privileges. Adding a user to this group is one way to grant sudo privileges to that user.
Take the steps in the following sections to complete this process.
Step 1: Login as Administrator
If you're working on a local machine, log into the system with administrator credentials. In case you're connecting to a remote machine via SSH, enter the following into the terminal:
ssh root@server_ip_address
Replace server_ip_address
with the actual server IP address. When prompted, enter the root credentials to access the system.
Step 2: Create a New Sudo User
To create a new user, run the adduser command:
adduser [username]
Enter the actual username for your new user in place of [username]
. For example, to create user1, run:
adduser user1
The command has no output.
Next, create a password for the new user using the passwd command. For example, to add a password for user1, run:
passwd user1
The system displays a prompt to set and confirm a password for your new user account.
Note: Try our free password generator or check out our article on strong password ideas.
Step 3: Verify the Wheel Group is Enabled
Your CentOS or Rocky installation may not have the wheel group enabled. To check, open the configuration file with:
visudo
The command opens the file in Vim. Scroll through the configuration file until you see the following entry:
If the second line begins with the #
sign, the wheel group has been disabled.
Delete the #
sign at the beginning of the second line to uncomment the line:
Save and exit the editor.
Step 4: Add User to Group
Use the usermod command to add a user to the wheel group.
usermod -aG wheel [username]
For example, to add user1, run:
usermod -aG wheel user1
The command has no output
Step 5: Switch to the Sudo User
To test whether user1 has sudo privileges, switch to the user1 account with the su (substitute user) command:
su -user1
Enter the password if prompted. The terminal prompt changes to include user1.
Run sudo
whoami:
sudo whoami
The terminal requests the password for user1 and prints root
as the confirmation the user has sudo privileges.
Add User to sudoers Configuration File in CentOS and Rocky Linux
If there's a problem with the wheel group, or administrative policy prevents you from creating or modifying groups, add a user directly to the sudoers configuration file to grant sudo privileges.
Take the following steps:
Step 1: Open the Sudoers File in an Editor
Switch to root and run the following command:
visudo
This opens the /etc/sudoers file in the Vim text editor.
Step 2: Add the New User to File
Scroll down to find the following section:
Right after this entry, add the following text:
[username] ALL=(ALL) ALL
For instance, to add user1, replace [username]
with the user1
. This section should look like the following:
Save the file and exit.
Step 3: Test Sudo Privileges for the User Account
Switch user accounts with the su
command. To switch to user1, run:
su -user1
Enter the password for the account if prompted. Run the following to test sudo privileges:
sudo whoami
If the terminal returns root
, the account has sudo privileges.
Conclusion
This guide explained how to add a user to sudoers in CentOS and Rocky Linux and test the privileges of an existing sudo user.
Next, learn how to add users to sudoers or sudo group on Ubuntu.