How to Add a User to Sudoers

Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Published: August 3, 2023

On Unix and Linux, the superuser account, also known as root, admin, or supervisor, is a special user account capable of making unrestricted, system-wide changes. It is mostly used for system administration tasks such as changing the ownership of files or binding network ports. However, it is sometimes necessary to allow standard user accounts to perform some of these sensitive actions, by granting them elevated privileges and access through the use of the sudo command.

In this article, we'll cover two methods for adding a user, or a group of users, to the sudoers list.

Add user to sudoers with the usermod command

In Linux, a group is a collection of accounts that can be given special or elevated permissions on the system. For example, a group can be given read permission on a file and another group read and write permissions on the same file.

To add a user account to a group, we can use the usermod command that essentially allows us to modify an existing account.

Bash
$ usermod -a -G

Where:

  • The -a flag (short for append) is used to specify that we want to add a group to the specified user.
  • The -G flag (short for groups) is used to specify which group we want to add.

Since most Linux distributions have a special group for sudoers, the easiest way to grant superuser privileges to a user account is to add it to this group.

Add users to sudoers in Ubuntu or Debian

On Ubuntu and Debian, this group is named sudo.

Bash
$ sudo usermod -a -G sudo

Add users to sudoers in CentOS or Fedora

On CentOS and Fedora, this group is named wheel.

Bash
$ sudo usermod -a -G wheel

Check whether adding users to sudoers was successful

To verify that a user was successfully added to the sudoers group, we can display the content of the /etc/group file using the cat command, which contains the list of groups (and their users) registered on the system.

Add users to sudoers using the sudoers file

In \nix systems, user accounts and groups with sudo privileges are stored into the /etc/sudoers file (sometimes called the “sudo file”), which contains a list of instructions called privilege lines, that can be edited to grant customized access to commands a user or a group can execute, or configure custom security policies.

The general syntax for a privilege line is the following:

Bash
user on_host=(as_user:as_group) allowed_commands

Which can be roughly translated to "who where=(whom) what".

For example, the following line can be read as "the root user can run any command as any user from any group on any host."

Bash
root ALL=(ALL:ALL) ALL

And this line can be read as "the admin user can run the mkdir command as the root user on any host".

Bash
admin ALL=(root) /usr/bin/mkdir

Adding a group to sudoers

The sudoers file also allows us to grant superuser privileges to an entire group of users by specifying the group name prefixed with a percentage character (%).

Bash
%group on_host=(as_user:as_group) allowed_commands

Use visudo to safely modify the sudoers file

Because of the sensitive nature of its content, it is highly recommended to only open it using the visudo utility — which uses the vim text editor under the hood — as it will automatically check for syntax errors before the file is saved, preventing us from ending up with a broken system where it is impossible to obtain elevated privileges.

Bash
$ visudo

Note that if you are not particularly experienced with vim, you can always change the default editor using the following syntax.

Bash
$ EDITOR= visudo
Written by
Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Filed under

Related articles


Bash Comments

Comments will help make your scripts more readable

Reading User Input

Via command line arguments and prompting users for input

Curl Post Request

Use cURL to send data to a server

Upload Files With curl

Learn how to upload a file to FTP, SFTP servers, Artifactory, and AWS S3 using the curl command.

How To Copy A Directory In Linux

Learn how to copy directories and their content in Linux using the cp command with options like -r for recursive copying, -i for interactive mode, and -a for preserving attributes.

Create Groups In Linux

Learn how to manually and automatically create and list groups in Linux.

How to Check the Size of Folders in Linux

Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.

Count Files in Linux

Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.

List Open Ports in Linux

Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.

Format Command Output In Linux

Learn how to filter and format the content of files and the output of commands in Linux using the awk command.

Create Directories Recursively With mkdir

Learn how to recursively create nested directories using the mkdir command, Bash scripts, and Python scripts.

Remover Users in Linux

Learn how to remove local and remote user accounts and associated groups and files in Linux using the userdel and deluser commands.