Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Mastering User Management on Linux
User management is a fundamental skill for Linux administrators. This article covers essential commands for creating, modifying, and deleting user accounts, managing groups, and monitoring user activities in Linux systems.
In the examples below,
saiis used as the username for demonstration purposes.
usermod Command
The usermod command modifies existing user account attributes. It allows administrators to change user properties without recreating the account.
To get help information about usermod −
$ usermod --help
Key options include −
-c, --comment COMMENT new value of the GECOS field -d, --home HOME_DIR new home directory for the user account -e, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE -f, --inactive INACTIVE set password inactive after expiration -g, --gid GROUP force use GROUP as new primary group -G, --groups GROUPS new list of supplementary GROUPS -a, --append append user to supplemental GROUPS -l, --login NEW_LOGIN new value of the login name -L, --lock lock the user account -U, --unlock unlock the user account
Basic Usage
$ sudo usermod [options] [username]
Common usermod Operations
Change home directory −
$ sudo usermod --home /home/newpath sai
Set account expiration date −
$ sudo usermod --expiredate 2024-12-31 sai
Lock user account −
$ sudo usermod --lock sai
Unlock user account −
$ sudo usermod --unlock sai
Add user to additional groups −
$ sudo usermod --append --groups wheel,sudo sai
useradd Command
The useradd command creates new user accounts. It is a low-level utility that directly modifies system files like /etc/passwd and /etc/shadow.
To view useradd options −
$ sudo useradd --help
Common options include −
-b, --base-dir BASE_DIR base directory for the home directory -c, --comment COMMENT GECOS field of the new account -d, --home-dir HOME_DIR home directory of the new account -e, --expiredate EXPIRE_DATE expiration date of the new account -g, --gid GROUP name or ID of the primary group -G, --groups GROUPS list of supplementary groups -m, --create-home create the user's home directory -s, --shell SHELL login shell of the new account
Basic Usage
$ sudo useradd [options] [username]
Common useradd Operations
Create user with home directory −
$ sudo useradd --create-home --shell /bin/bash sai
Create user with specific home directory −
$ sudo useradd --home-dir /home/custom sai
Create user with expiration date −
$ sudo useradd --expiredate 2024-12-31 sai
Add user to specific groups during creation −
$ sudo useradd --groups wheel,users sai
userdel Command
The userdel command removes user accounts from the system. It can optionally remove the user's home directory and mail spool.
$ userdel --help
Available options −
-f, --force force removal of files, even if not owned by user -r, --remove remove home directory and mail spool -Z, --selinux-user remove any SELinux user mapping for the user
Basic Usage
$ sudo userdel [options] [username]
Common userdel Operations
Delete user account only −
$ sudo userdel sai
Delete user with home directory −
$ sudo userdel --remove sai
Force delete user and files −
$ sudo userdel --force sai
Password and Account Management
Change password aging information −
$ sudo chage sai
Interactive output example −
Changing the aging information for sai
Enter the new value, or press ENTER for the default
Minimum Password Age [0]: 7
Maximum Password Age [99999]: 90
Last Password Change (YYYY-MM-DD) [2024-01-15]:
Password Expiration Warning [7]: 7
Password Inactive [-1]: -1
Account Expiration Date (YYYY-MM-DD) [-1]: 2024-12-31
Change user information −
$ sudo chfn sai
Change file ownership −
$ sudo chown sai:sai /path/to/file
Group Management
| Command | Purpose | Example |
|---|---|---|
groupadd |
Create new group | sudo groupadd developers |
groupdel |
Delete group | sudo groupdel developers |
groups |
Show user's groups | groups sai |
grpck |
Verify group files integrity | sudo grpck |
User Monitoring and Information
Display current users and activities −
$ w
Show logged-in users −
$ who
Display current username −
$ whoami
Show user and group IDs −
$ id sai
Example output −
uid=1001(sai) gid=1001(sai) groups=1001(sai),27(sudo),1000(users)
View login history −
$ last
View failed login attempts −
$ sudo lastb
Check login failures by user −
$ sudo faillog -u sai
Conclusion
Effective user management in Linux involves understanding the useradd, usermod, and userdel commands for account lifecycle management, along with group management and monitoring tools. These commands provide comprehensive control over user accounts, permissions, and system security in Linux environments.
