Creating user accounts is a fundamental responsibility of a Linux system administrator, enabling secure access control and proper permission management. Linux provides powerful tools to create and manage users efficiently, especially in multi-user and server environments.
User accounts are commonly created to:
- Provide system access to new users such as developers or team members
- Create service accounts for applications like web servers or databases
- Isolate tasks and improve system security through permission separation
- Manage system resources by assigning users specific roles
Let us consider a command to add a user named test_user to the system. This command creates a new user account without additional customization.
Command:
sudo useradd test_userOutput:

Working with the useradd Command
Syntax:
useradd [options] [user_name]- useradd – Command used to create a new user account
- options – Optional flags to customize the user (UID, home directory, shell, etc.)
- username – Name of the user account to be created
1. Create a User with a Custom Home Directory
This creates a new user and assigns a specific directory as the user’s home directory.
Command:
sudo useradd -d /home/test_user test_userOutput:

- User test_user is created
- Home directory is set to /home/test_user (directory may need to be created manually unless -m is used)
2. Create a User with a Specific User ID (UID)
This creates a user with a custom user ID instead of an automatically assigned one.
Command:
sudo useradd -u 1234 test_userOutput:

- User test_user is created with UID 1234
- UID is recorded in /etc/passwd
3. Create a User with a Specific Group ID (GID)
This assigns the user to an existing group using its group ID.
Command:
sudo useradd -g 1000 test_userOutput:

- User test_user is created
- Primary group is set to GID 1000
4. Create a User Without a Home Directory
This creates a user account without creating a home directory.
Command:
sudo useradd -M test_userOutput:

- User test_user is created
- No home directory is assigned or created
5. Create a User with an Account Expiry Date
This creates a user account that automatically expires on a specific date.
Command:
sudo useradd -e 2020-05-30 test_userOutput:

- User test_user is created
- Account expires on 30 May 2020
- After expiry, login is disabled
6. Create a User with a Comment or Description
This adds descriptive information (such as full name or role) to the user account.
Command:
sudo useradd -c "This is a test user" test_userOutput:

- User test_user is created
- Comment field is stored in /etc/passwd
7. Create a User with a Specific Login Shell
This sets a custom default shell for the user.
Command:
sudo useradd -s /bin/sh test_userOutput:

- User test_user is created
- Default login shell is set to /bin/sh
8. Display Help for the useradd Command
This displays all available options and usage information for useradd.
Command:
sudo useradd --helpOutput:

- Displays a list of all useradd options
- Shows syntax and flag descriptions
Files Modified by useradd
When you run useradd, it's the command simply edits a set of system files to register the new user.
- /etc/passwd: Stores user information like the username, UID, GID, home directory path, and default shell.
- /etc/shadow: Stores the user's encrypted password. This is why useradd alone can't set a password.
- /etc/group: Stores information about user groups.
- /etc/gshadow: Stores secure information for groups.
- /home/[username]: (If created) This is the user's personal directory. The initial files in it are copied from /etc/skel.
adduser vs useradd
- adduser is an interactive, user-friendly tool commonly used on Debian/Ubuntu systems. It creates the user, home directory, password, and prompts for additional details.
- useradd is a low-level, non-interactive command designed for scripting and automation, commonly used on RHEL, CentOS, and Fedora systems.