• Modern UX

    Edit and navigate faster in the terminal with Warp's IDE-like input editor.

  • Warp AI

    AI suggests what commands to run and learns from your documentation.

  • Agent Mode

    Delegate tasks to AI and use natural language on the command line.

  • Warp Drive

    Save and share interactive notebooks, workflows, and environment variables.

  • All Features

How to Add a User to Sudoers

Thumbnail for Razvan LudosanuRazvan Ludosanu

Razvan Ludosanu

Founder, learnbackend.dev

Published: 8/3/2023

About Terminus

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.

 $ usermod -a -G  
Run in Warp

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.

 $ sudo usermod -a -G sudo 
Run in Warp

Add users to sudoers in CentOS or Fedora

On CentOS and Fedora, this group is named wheel.

 $ sudo usermod -a -G wheel 
Run in Warp

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.

Thumbnail for

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.

Thumbnail for

The general syntax for a privilege line is the following:

 user on_host=(as_user:as_group) allowed_commands
Run in Warp

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."

 root ALL=(ALL:ALL) ALL
Run in Warp

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

 admin ALL=(root) /usr/bin/mkdir
Run in Warp

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 (%).

 %group on_host=(as_user:as_group) allowed_commands
Run in Warp

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.

 $ visudo
Run in Warp

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

 $ EDITOR= visudo
Run in Warp

Written by

Thumbnail for Razvan LudosanuRazvan Ludosanu

Razvan Ludosanu

Founder, learnbackend.dev

Filed Under

Related Articles

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.

UnixLinux
Thumbnail for Razvan LudosanuRazvan Ludosanu

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.

LinuxUnix
Thumbnail for Razvan LudosanuRazvan Ludosanu

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.

LinuxUnix
Thumbnail for Razvan LudosanuRazvan Ludosanu

Linux Chmod Command

Understand how to use chmod to change the permissions of files and directories. See examples with various chmod options.

Linux
Thumbnail for Razvan LudosanuRazvan Ludosanu

POST JSON Data With Curl

How to send valid HTTP POST requests with JSON data payloads using the curl command and how to avoid common syntax pitfalls. Also, how to solve the HTTP 405 error code.

BashUnixLinux
Thumbnail for Neeran GulNeeran Gul

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.

Linux

Create Groups In Linux

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

Linux

Switch Users In Linux

Learn how to switch between users, log in as another user, and execute commands as another user in Linux.

Linux

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.

Linux

Delete Files In Linux

Learn how to selectively delete files in Linux based on patterns and properties using the rm command.

Linux

Find Files In Linux

Learn how to find and filter files in Linux by owner, size, date, type and content using the find command.

Linux

Copy Files In Linux

Learn how to safely and recursively copy one or more files locally and remotely in Linux using the cp and scp command.

Linux

Trusted by hundreds of thousands of professional developers

Download Warp to get started

Download for Mac
Thumbnail for null