Providing Sudo Privileges to a Group
Introduction
In Linux environments, administrators may need to grant sudo privileges to multiple users. Instead of configuring sudo access individually for each user, it is more efficient to create a group and assign sudo privileges to that group. Any user added to the group will automatically inherit the configured sudo permissions.
This article explains how to create a group, add users to the group, and grant sudo access to all members of the group.
Prerequisites
Before proceeding, ensure that you have:
- Root or sudo access to the server.
- Basic knowledge of Linux user and group management.
- Access to edit the sudoers configuration using
visudo.
Implementation
Step 1: Create a Group
Create a new group that will be used for sudo access:
| $ groupadd testgroup |
Verify the group:
| $ getent group testgroup |
Step 2: Add Users to the Group
Create users and add them to the group:
| $ useradd -G testgroup username1 $ useradd -G testgroup username2 |
Alternatively, add existing users to the group:
| $ usermod -aG testgroup username1 $ usermod -aG testgroup username2 |
Verify group membership:
| $ id username1 $ id username2 |
Step 3: Edit the Sudoers File
Open the sudoers file using visudo:
| visudo |
Add the following line at the end of the file:
%testgroup ALL=(ALL) NOPASSWD: ALL
Replace testgroup with the actual group name.
Explanation
%testgroup– Applies the rule to all users in the group.ALL=(ALL)– Allows execution of commands as any user.NOPASSWD: ALL– Allows sudo commands without prompting for a password.
If you want users to enter their password when using sudo, use:
%testgroup ALL=(ALL) ALL
Step 4: Test Sudo Access
Switch to a user in the group:
| $ su – username1 |
Verify sudo access:
| $ sudo whoami |
Expected output:
root
Conclusion
Using a dedicated group for sudo access simplifies administration when managing multiple users. By granting sudo permissions to a group, you can easily add or remove users without modifying the sudoers file each time. Always follow the principle of least privilege and grant sudo access only to trusted users. For improved security, consider requiring passwords for sudo operations instead of using the NOPASSWD option.
