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 :
