Linux User Management
This page provides a detailed guide on using the useradd and usermod commands in Linux for managing users.
User related information save in /etc/passwd
Group related information save in /etc/group
User Password related information save in /etc/shadow
Group pasword related information save in /etc/gshadow
To view the contents of these files, you can use the cat command:
View user information:
cat /etc/passwd
User related information
1. User ID (UID)
2. Group ID (GID)
3. Default directory (/home/username)
4. Default shell (/bin/bash)
5. User information save (/etc/passwd)
6. Group information save (/etc/group)
7. User password information save (/etc/shadow)
8. Group password information save (/etc/gshadow)
9. User's full name
10. User's home directory
11. User's login shell
12. User's password (x means password is stored in /etc/shadow)
13. User's group (x means group is stored in /etc/gshadow)
User Default authentication structure
cat /etc/login.defs
cat /etc/default/useradd
The UID and GID ranges are defined in the /etc/login.defs file. The default range for UIDs is typically from 1000 to 60000, while GIDs can range from 1000 to 60000 as well.
The /etc/default/useradd file may also contain settings for the minimum and maximum UID and GID values.
UID and GID range
1. UID range: 1000-60000
2. GID range: 1000-60000
3. UID and GID for system users: 0-999
4. UID and GID for regular users: 1000-60000
5. UID and GID for special users: 60001-99999
Passwd file entry structure
username:password:UID:GID:comment:home_directory:login_shell
1. username: The name of the user.
2. password: The password for the user (x means password is stored in /etc/shadow).
3. UID: The user ID (a unique number assigned to the user).
4. GID: The group ID (the primary group of the user).
5. comment: Additional information about the user (e.g., full name).
6. home_directory: The user's home directory.
7. login_shell: The shell that the user will use when logging in.
Group file entry structure
groupname:password:GID:member1,member2,...
1. groupname: The name of the group.
2. password: The password for the group (x means password is stored in /etc/gshadow).
3. GID: The group ID (a unique number assigned to the group).
4. member1,member2,...: The members of the group (comma-separated list).
1. useradd Command
The useradd command is used to create a new user in Linux.
Options
-c: Add a comment (e.g., full name).-d: Specify the user's home directory.-e: Set the account expiration date.-g: Specify the primary group.-G: Specify additional groups.-m: Create the home directory if it does not exist.-s: Specify the login shell.-u: Specify the user ID (UID).
Examples
Create a user with a home directory:
sudo useradd -m -d /home/abhishek -s /bin/bash abhishek
Create a user with a specific UID and group:
sudo useradd -u 1001 -g developers abhishek
2. usermod Command
The usermod command is used to modify an existing user account.
Options
-c: Change the comment (e.g., full name).-d: Change the home directory.-e: Change the account expiration date.-g: Change the primary group.-G: Add the user to additional groups.-l: Change the login name.-s: Change the login shell.-u: Change the user ID (UID).-L: Lock the user account.-U: Unlock the user account.
Examples
Change a user's home directory:
sudo usermod -d /new/home/directory abhishek
Add a user to a new group:
sudo usermod -aG sudo abhishek
Lock a user account:
sudo usermod -L abhishek
3. userdel Command
The userdel command is used to delete a user account.
Options
-r: Remove the user's home directory and mail spool.-f: Force the removal of the user account.-Z: Remove the SELinux user mapping.
Examples
Delete a user account and remove the home directory:
sudo userdel -r abhishek
Forcefully delete a user account:
sudo userdel -f abhishek
Remove the SELinux user mapping:
sudo userdel -Z abhishek