How to Create and Add User in Sudo Group - Centos, Ubuntu & Linux

In this chapter, you will learn

  1. What is sudo command?
  2. How to create a user and add them in a sudoers group on Ubuntu/CentOS?

What is sudo command?


sudo stands for 'Super User Do,' and it functions similarly to 'Run as administrator' on Windows. In Linux, to execute commands at the administrative level, a user needs to have administrator privileges, which can be granted using the sudo command.

The sudo command precedes the main command and requires the user's password for executing commands as administrators.

Note

It's important to note that this password is not the root password, but the user's own password used for authentication.

For a user to utilize sudo, they must be a member of the sudoers group and have an entry in the /etc/sudoers file.

Some Example
sudo apt-get update
$sudo yum update
$sudo systemctl restart apache2
$sudo adduser newuser

Creating a User and adding them to Sudo group

In this tutorial, I will guide you through the process of creating a new user and adding them to the sudo group. This allows the user to harness the power of the sudo command, enabling them to execute commands with elevated privileges.


Ubuntu ↓

Step 1: Create a New User

To create a new user, you need to run the sudo adduser <username> command as follows:

sudo adduser smith
jack@ubuntu:~$ sudo adduser smith Adding user 'smith' ... Adding new group 'smith' (1002) ... Adding new user 'smith' (1002) with group 'smith' ... New password: Retype new password: passwd: password updated successfully Changing the user information for smith Enter the new value, or press ENTER for the default Full Name []: Smith Room Number []: Work Phone []: Home Phone []: Other []: Is the information correct? [Y/n] Y
Step 2: Adding user to sudo group.

To add a new user to sudo group in ubuntu, execute the sudo usermod -aG sudo <username> command in terminal.

sudo usermod -aG sudo smith
Step 3: Test the user.

Now, switch the account to smith and run the some command with sudo privilege.

Switch to smith account
jack@ubuntu:~$su smith
Enter password: smith@ubuntu:~$ smith@ubuntu:~$sudo apt-get update

Centos ↓

Step 1: Create a New User

To create a new user, you need to run the sudo adduser <username> command as follows:

sudo adduser smith
Step 2: Create password.
sudo passwd smith

Follow the prompt and create password for user smith.

Step 3: Adding user to sudo group.

To add a new user to sudo group in ubuntu, execute the sudo usermod -aG sudo <username> command in terminal.

usermod –aG wheel smith
Step 4: Open visudo editor to enable wheel group.
  1. Type sudo visudo in terminal. It will open the sudo file.
  2. Scroll down to find following line.
    # %wheel ALL=(ALL) ALL
  3. Remove # from the beginning of the line.
  4. To save and exit → Press Esc button → type :wq → Press Enter.
Step 5: Test the user. Type su smith to switch user and then test this user by running some command using sudo privilege.

Option 2: Adding user to Sudo group.

If above trick won't work for you, then you can directly add the user into visudo file to give sudo access.

Step 1: Type visudo in terminal.
Step 2: Find the following line.
root    ALL=(ALL:ALL) ALL
Step 3: Add your user below this line.
smith   ALL=(ALL:ALL) ALL
Step 4: Save and exit the file.

Summary

In this tutorial, we taught you how to create a user in Ubuntu/CentOS and add them in sudo group.