How to Add User to Sudoers or Sudo Group on Ubuntu

April 3, 2024

Introduction

Sudo permissions enable users to execute commands with elevated privileges. These permissions are defined in the sudoers file to prevent accidental misuse of commands or enable users to execute administrative commands.

This tutorial will show you how to add a new user on Ubuntu and grant them sudo access.

How to Add User to Sudoers or Sudo Group on Ubuntu

Prerequisites

  • A system running Ubuntu (this tutorial uses Ubuntu 22.04.).
  • Access to an account with root or sudo privileges.
  • Access to the terminal.

Steps to Add Sudo User on Ubuntu

Standard user accounts have limitations that prevent them from performing sensitive tasks.

For example, to view the /root directory contents with the ls command, users need elevated permissions. Running the command without those permissions results in an error:

ls /root
ls terminal output

In cases like this, the sudo (Super User Do) command is the recommended method for managing the elevated permissions. Follow the steps below to allow an account to use the sudo command to perform administrative tasks.

Step 1: Create a New User

To create a new user:

1. Use the adduser command:

sudo adduser [name-of-user]

For instance, create a user called user1 with:

sudo adduser user1
sudo adduser user1 terminal output

The adduser command creates a new user with a group and Home directory for that user.

2. The system prompts for a password. Enter a secure password, then retype it to confirm.

add password

3. The system asks for additional information about the user, including name, phone numbers, etc. These fields are optional and can be skipped by pressing Enter.

enter additional info

4. Type y to confirm the information and complete the settings for the new user.

Step 2: Add User to Sudoers File or Sudo Group

The sudoers file and the sudo group are related to granting users administrative privileges via the sudo command.

/etc/sudoers is a configuration file that specifies which users or groups can use sudo for administrative tasks. It defines permissions, such as command execution and password requirements.

On the other hand, the sudo group is created specifically to manage sudo privileges. Users added to this group gain sudo access to run commands with elevated permissions using sudo.

To grant users sudo permission without editing the sudoers file, add them to the sudo group with the usermod command:

sudo usermod -aG sudo [name-of-user]

For example, grant user1 sudo privileges with:

sudo usermod -aG sudo user1

The command has no output, but the -aG option tells the system to append the user to the specified group.

Step 3: Verify User Belongs to Sudo Group

To confirm user1 belongs to the sudoers group, view the groups the user belongs to:

groups user1
groups user1 terminal output

The system responds by listing the username and all groups it belongs to.

Step 4: Verify Sudo Access

To verify sudo access, take the following steps:

1. Switch users with the su command. For example:

su - user1
su user1 terminal output

Enter the password when prompted.

2. Run a command that requires elevated privileges. For instance, list the /root directory contents using the ls command with sudo:

sudo ls /root
sudo ls /root terminal output

When the command provides the output, the user has sudo access.

Conclusion

After reading this article, you know how to add and create a user with sudo privileges on Ubuntu.

Next, learn about the difference between sudo vs. su.

Was this article helpful?
YesNo
Goran Jevtic
Goran combines his leadership skills and passion for research, writing, and technology as a Technical Writing Team Lead at phoenixNAP. Working with multiple departments and on various projects, he has developed an extraordinary understanding of cloud and virtualization technology trends and best practices.
Next you should read
How to Use the sudo Command in Linux
August 18, 2020

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 & Add User to Sudo Group on CentOS 7
December 5, 2018

This guide will walk you through the steps to create or add a sudo user on CentOS 7. The "sudo" command...
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