The useradd
command in Linux is used for creating new user accounts. When using the --extrausers
option, it enables management of user accounts with the Extra Users Database Command.
Here are several examples demonstrating the use of useradd --extrausers
:
Example 1: Create a new user named ‘testuser’ with extra user database management:
sudo useradd --extrausers testuser
Output (if successful): No output signifies successful user creation.
Verification: To verify if ‘testuser’ was added, use:
grep testuser /etc/passwd
Output: Should display details of ‘testuser’ if successfully added.
Example 2: Add a new user ‘guest’ with specific user ID (UID) and group ID (GID):
sudo useradd --extrausers --uid 2000 --gid 2000 guest
Output (if successful): No output signifies successful user creation.
Verification: Check the entry in the password file:
grep guest /etc/passwd
Output: Should show ‘guest’ with UID 2000 and GID 2000.
Example 3: Create a user ‘developer’ with a specific home directory:
sudo useradd --extrausers --home /home/developer developer
Output (if successful): No output signifies successful user creation.
Verification: Confirm the home directory creation:
ls -ld /home/developer
Output: Should display details of the ‘developer’ home directory.
Example 4: Add a user ‘admin’ with additional comments:
sudo useradd --extrausers --comment "System Administrator" admin
Output (if successful): No output signifies successful user creation.
Verification: Check comments in the password file:
grep admin /etc/passwd
Output: Should show ‘System Administrator’ as the comment for ‘admin’.
Example 5: Create a system user ‘service’ without user interaction:
sudo useradd --extrausers --system service
Output (if successful): No output signifies successful system user creation.
Verification: Verify ‘service’ user with:
grep service /etc/passwd
Output: Should indicate ‘service’ as a system user.
Example 6: Assign a custom shell ‘bash’ to user ‘shelluser’:
sudo useradd --extrausers --shell /bin/bash shelluser
Output (if successful): No output signifies successful user creation.
Verification: Check the shell assignment:
grep shelluser /etc/passwd
Output: Should show ‘/bin/bash’ as the shell for ‘shelluser’.
Example 7: Add a user ‘student’ with an expiration date:
sudo useradd --extrausers --expiredate 2025-12-31 student
Output (if successful): No output signifies successful user creation.
Verification: Check the expiration date setting:
sudo chage -l student
Output: Should display the expiration date for ‘student’.
Example 8: Create a user ‘readonly’ with read-only access:
sudo useradd --extrausers --groups readonly readonly
Output (if successful): No output signifies successful user creation.
Verification: Confirm group membership:
id readonly
Output: Should list ‘readonly’ as a supplementary group for the user.
Example 9: Add a user ‘backup’ with a specific login shell:
sudo useradd --extrausers --shell /bin/false backup
Output (if successful): No output signifies successful user creation.
Verification: Check the shell setting:
grep backup /etc/passwd
Output: Should show ‘/bin/false’ as the shell for ‘backup’.
Example 10: Create a user ‘webadmin’ with a specific UID range:
sudo useradd --extrausers --uid-range 3000-4000 webadmin
Output (if successful): No output signifies successful user creation.
Verification: Verify UID range assignment:
grep webadmin /etc/passwd
Output: Should show ‘webadmin’ with UID within the specified range.
Discussion about this post