0
0
Linux-cliHow-ToBeginner · 3 min read

How to Add User to Sudo Group in Linux Quickly

To add a user to the sudo group in Linux, use the command sudo usermod -aG sudo username. This grants the user administrative rights to run commands with sudo.
📐

Syntax

The command to add a user to the sudo group is:

sudo usermod -aG sudo username
  • sudo: runs the command with administrative privileges.
  • usermod: modifies user account settings.
  • -aG: appends the user to the specified group(s) without removing them from other groups.
  • sudo: the group name that grants sudo privileges.
  • username: the name of the user you want to add.
bash
sudo usermod -aG sudo username
💻

Example

This example adds the user alice to the sudo group, allowing her to run commands with administrative rights.

bash
sudo usermod -aG sudo alice

# Verify by checking groups for alice
groups alice
Output
alice : alice sudo
⚠️

Common Pitfalls

Common mistakes include:

  • Using -G without -a, which replaces all groups and removes existing ones.
  • Not running the command with sudo, causing permission denied errors.
  • Typing the wrong username or group name.

Always use -aG together to append groups safely.

bash
sudo usermod -G sudo alice  # Wrong: replaces groups, may remove existing ones
sudo usermod -aG sudo alice  # Correct: appends sudo group
📊

Quick Reference

CommandDescription
sudo usermod -aG sudo usernameAdd user to sudo group safely
groups usernameShow groups of the user
sudo -l -U usernameList sudo privileges of the user

Key Takeaways

Use 'sudo usermod -aG sudo username' to add a user to the sudo group safely.
Always include the '-a' flag to append groups without removing existing ones.
Run the command with 'sudo' to have the necessary permissions.
Verify the user’s groups with 'groups username' after adding.
Be careful with usernames and group names to avoid errors.