Overview of useradd
`useradd` command performs actions such as adding user account information to system files like `/etc/passwd`, `/etc/shadow`, `/etc/group`, `/etc/gshadow`, creating the user's home directory, and copying default configuration files (skeleton files). All of these actions require administrator privileges (`sudo`).
Main Roles of useradd
`useradd` command is primarily used for the following purposes:
Key Use Cases
- Registering New Users: Adds new regular users to the system, allowing them to log in and access files.
- Creating Service Accounts: Creates non-login accounts for specific applications or services to run.
- Security Management: Grants each user a unique account to control system access and enhance accountability.
Key Components When Creating User Accounts
When a user account is created, the following key components are set:
Key Account Components
- Username: A unique identifier for the account, used during login.
- User ID (UID): A numeric ID that the system uses to identify the user, recorded in `/etc/passwd`.
- Primary Group: The main group that is automatically assigned when the user is created.
- Supplementary Groups: Additional groups that the user may belong to.
- Home Directory: The space where the user's personal files are stored, typically in the format `/home/username`.
- Login Shell: The default shell program that will be executed when the user logs in. (e.g., `/bin/bash`, `/bin/sh`, `/bin/false`)
- Password: Used for user authentication, set separately using the `passwd` command.
📂 Default Configuration Files (Skeleton Directory)
/etc/skel
When a new user account is created, the user's home directory is populated with predefined default configuration files (e.g., `.bashrc`, `.profile`). These files are usually located in the `/etc/skel` directory, referred to as the skeleton directory.
Key useradd Command Options
`useradd` command provides options to set various account attributes, such as the user's home directory, shell, groups, and expiration date.
1. Basic Account Settings
2. Group and UID/GID Settings
3. Account Expiration and Others
Generated command:
Try combining the commands.
Description:
`useradd` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Learn how to create new user accounts and set attributes through various examples of the `useradd` command.
Create New User Account (Default Settings)
sudo useradd testuser
Creates a new user account named `testuser`. The home directory and password for this account have not been set yet.
Create New User Account with Home Directory
sudo useradd -m devuser
sudo passwd devuser
Creates a new user account named `devuser`, creates the home directory `/home/devuser`, and copies the default configuration files from `/etc/skel`. After creating the account, you must set the password using the `passwd` command to log in.
Create User with Specific Home Directory and Shell
sudo useradd -m -d /var/www/webuser -s /bin/false webuser
Creates an account named `webuser`, sets the home directory to `/var/www/webuser`, and the default shell to `/bin/false` (no login). Suitable for web service accounts.
Add User to Multiple Groups
sudo useradd -m -G sudo,www-data -c "Administrator for Web" adminuser
sudo passwd adminuser
Creates an `adminuser` account, adds it to the `sudo` group and `www-data` group, and also sets a user description.
Create User Account with Expiration Date
sudo useradd -m -e 2025-12-31 tempuser
sudo passwd tempuser
Creates a `tempuser` account, creates the home directory, and sets it to expire on December 31, 2025. Useful for temporary worker accounts.
Check Current useradd Defaults
useradd -D
Checks the defaults (primary group, home directory prefix, shell, etc.) that will apply when creating a new user.