In Linux administration, best practice recommends running commands as a regular user with sudo privileges. This user is simply known as a sudo user, and the user bears root privileges to perform elevated tasks in the system such as installing, updating, upgrading, and removing packages to mention a few. To execute privileged commands as a sudo user, the word ‘sudo’ precedes the actual command. Sudo is short for Super User do and when invoked, it allows underprivileged users to perform elevated tasks using root privileges.
By default, the regular user created upon installation is simply an underprivileged user. Thankfully, you can add the user to the sudoers group to impart root privileges. This will allow the user to perform elevated tasks in the system just as a root user would. In this tutorial, we demonstrate how to create a user and add them to the sudoers group on Rocky Linux.
Step 1: Create a new user account
First off, we will commence with the creation of a new user on the system. In most cases, you will already have one that you created during the installation as mentioned earlier. However, we felt this step would be beneficial if you intend to add another regular user to your system.
So, first switch to root user as shown.
$ su
Then run the cd command to switch to the root user’s home directory.
$ cd
Just to confirm that you are a root user, run the whoami command.
$ whoami
To add or create a new user, use the syntax:
$ adduser username
For instance, to create a user called karim, execute the command:
$ adduser karim
Once the user is created, assign the password.
$ passwd karim
Be sure to provide the password and then confirm it.
Verify that the user is a valid regular user by checking the /etc/passwd file as follows.
$ cat /etc/passwd | grep -i karim
Step 2: Add the user to the wheel group
Now onto the crux of the matter - adding the regular user to the sudoers group. There are two ways to achieve this. The first approach is to add the regular user to the ‘wheel’ group, which is a special group in Linux systems. Users in this group are automatically granted sudo privileges and can perform elevated tasks. On Rocky Linux, the ‘wheel’ group is enabled by default. You can confirm this by viewing the sudoers file as follows.
# vim /etc/sudoers
As indicated, the line beginning with %wheel is uncommented implying that the group is active. If the line starts with a # sign , make sure to delete it to uncomment it so as to enable the group.
As observed from the output, the line should appear as :
%wheel ALL=(ALL) ALL
Thereafter, invoke the usermod command to add the user to the wheel group.
# usermod -aG wheel karim
Then use the groups command to confirm that the regular user now belongs to the ‘wheel’ group.
# groups karim
Step 3: Testing the user
To confirm that the user has acquired elevated privileges, switch to the user.
# su - karim
Then run a privileged task such as updating system packages.
$ sudo dnf update
For the first time, you will get some initial instructions about working as a sudo user. Thereafter, provide the user’s password to authenticate to see the command run with a breeze.
An alternative method of adding a user to sudoers group
The second method of adding a regular user to the sudoers group is to invoke the visudo command. This opens the /etc/sudoers file.
$ visudo
Locate the lines:
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
Just below the second line, append the line below
karim ALL=(ALL) ALL
Save the changes and exit the file. Thereafter, you can invoke the sudo command to run elevated tasks.