π₯ Linux/Unix: User & Group Management β useradd, usermod, groups, id Explained
π§² Introduction β Why Learn User & Group Management in Linux?
User and group management is at the core of Linux system administration. It controls who can access the system, what they can do, and how resources are shared. With tools like useradd, usermod, groups, and id, you can easily manage users, assign roles, and control file permissions.
π― In this guide, youβll learn:
- How to create, modify, and manage users and groups
- How to check group memberships and user identity
- Practical examples with command outputs
π€ 1. useradd β Add a New User
β
 What is useradd?
useradd is the standard command to create new user accounts.
π οΈ Syntax:
sudo useradd [options] username
πΉ Common Options:
| Option | Description | 
|---|---|
| -m | Create home directory | 
| -s | Specify default shell | 
| -G | Add to additional groups | 
| -u | Set user ID manually | 
| -d | Specify home directory | 
π§ͺ Example 1: Create a new user with home and bash shell
sudo useradd -m -s /bin/bash alice
π€ Output:
Creates /home/alice and assigns bash as the default shell.
π§ͺ Example 2: Add user to sudo group
sudo useradd -m -G sudo bob
π οΈ 2. usermod β Modify Existing User
β
 What is usermod?
usermod is used to change user attributes such as group membership, home directory, or login shell.
π οΈ Syntax:
sudo usermod [options] username
πΉ Common Tasks:
| Task | Command Example | 
|---|---|
| Change login shell | sudo usermod -s /bin/zsh alice | 
| Add user to a group | sudo usermod -aG developers alice | 
| Change home directory | sudo usermod -d /new/home alice | 
| Lock user account | sudo usermod -L alice | 
| Unlock user account | sudo usermod -U alice | 
π¨βπ©βπ§ 3. groups β Show Group Memberships
β
 What is groups?
Displays the groups a user belongs to.
π οΈ Syntax:
groups [username]
π§ͺ Example:
groups alice
π€ Output:
alice : alice sudo developers
π§ Helpful to confirm if a user has sudo or team-specific access.
π§Ύ 4. id β Display User and Group IDs
β
 What is id?
Shows the user ID (uid), primary group ID (gid), and all groups a user belongs to.
π οΈ Syntax:
id [username]
π§ͺ Example:
id bob
π€ Output:
uid=1001(bob) gid=1001(bob) groups=1001(bob),27(sudo)
π§ Use this to programmatically check user identity in scripts.
π§ Bonus: Related Commands
| Command | Description | 
|---|---|
| addgroup | Debian-specific alias for groupadd | 
| groupadd | Add new group | 
| groupdel | Delete a group | 
| passwd | Set or change user password | 
| deluser/userdel | Delete user account | 
π Summary β Recap & Next Steps
Managing users and groups is foundational for Linux security, multi-user access, and resource permissions. These tools help you enforce access policies, add team members, and automate user setup.
π Key Takeaways:
- Use useradd -m -s /bin/bashto create new users with default shell and home.
- Use usermod -aGto assign users to groups without overwriting existing ones.
- Use groupsoridto confirm access roles and identities.
β FAQs
β How do I add a user to multiple groups?
β
 Use:
sudo usermod -aG group1,group2 username
β What’s the difference between useradd and adduser?
β
 useradd is low-level and universal. adduser is a Debian-friendly interactive script that uses useradd underneath.
β Can I change a username?
β
 Yes:
sudo usermod -l newname oldname
β How do I remove a user and their home directory?
β
 Use:
sudo userdel -r username
β How do I temporarily disable a user account?
β
 Use:
sudo usermod -L username
Share Now :
