How to Create Sudo User on CentOS and Rocky Linux

December 25, 2024

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.

How to Create Sudo User 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
passwd user1 terminal output

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:

wheel group disabled in Vim terminal output

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:

wheel group enabled in Vim terminal output

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
su user1 terminal output

Enter the password if prompted. The terminal prompt changes to include user1.

Run sudo whoami:

sudo whoami
sudo whoami terminal output

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:

root ALL=(ALL) ALL line in Vim terminal output

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:

user1 ALL=(ALL) ALL line in Vim terminal output

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
terminal output for 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.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
How to Use the sudo Command in Linux
December 25, 2024

sudo stands for SuperUser DO, and it's used to temporarily elevate privileges in Linux. This guide will show...
Read more
How to Use the su Command in Linux with Examples
April 16, 2024

Learn how to use the su command with practical examples and explanations. Change users in the terminal window...
Read more
How to Add User to Sudoers or Sudo Group on Ubuntu
April 3, 2024

Sudo permissions enable users to execute commands with elevated privileges. These permissions are...
Read more
How to Update Linux Kernel In Ubuntu
December 7, 2023

The Linux kernel is like the central core of the operating system. It works as sort of a mediator, providing...
Read more