How to Add a User to Sudoers
Razvan Ludosanu
Founder, learnbackend.dev
Published: 8/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.
$ 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.
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:
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
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.
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.
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.
Linux Chmod Command
Understand how to use chmod to change the permissions of files and directories. See examples with various chmod options.
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.
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 Groups In Linux
Learn how to manually and automatically create and list groups in Linux.
Switch Users In Linux
Learn how to switch between users, log in as another user, and execute commands as another user in 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.
Delete Files In Linux
Learn how to selectively delete files in Linux based on patterns and properties using the rm command.
Find Files In Linux
Learn how to find and filter files in Linux by owner, size, date, type and content using the find command.
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.