Linux - User Management in Linux: useradd and userdel?
Linux systems allow multiple users, and managing these users is an important administrative task. Two essential commands for this are useradd (to create users) and userdel (to delete users).
1. useradd Command
The useradd command is used to create a new user account in Linux.
Basic Syntax
useradd [options] username
Common Uses
1. Create a new user
useradd john
2. Create a user with a home directory
Some systems create it automatically; if not, use:
useradd -m john
3. Set a custom home directory
useradd -d /custom/home/path john
4. Assign a user to a specific group
useradd -g developers john
5. Specify login shell
useradd -s /bin/bash john
6. Set account expiration date
useradd -e 2025-12-31 john
2. userdel Command
The userdel command is used to delete a user account from the system.
Basic Syntax
userdel [options] username
Common Uses
1. Delete a user account
userdel john
2. Delete a user along with their home directory and mail
userdel -r john
This removes:
-
Home folder
-
Mail spool
-
User account details
3. Force delete a user even if logged in
userdel -f john
Files Affected During User Creation and Deletion
When using useradd, Linux updates:
-
/etc/passwd -
/etc/shadow -
/etc/group -
/etc/gshadow
When using userdel, Linux removes or updates:
-
User entry in system files
-
Home directory (if
-rused)
Summary
| Command | Purpose |
|---|---|
useradd |
Creates a new user account |
userdel |
Deletes an existing user account |
useradd -m |
Creates home directory |
userdel -r |
Deletes user and home directory |