Lesson 55: useradd Command
In this lesson, you'll learn how to use the useradd command to create and manage user accounts in Linux.
useradd Command: A system administration tool for creating and managing new user accounts.The useradd command is a low-level utility that is used for adding/creating user accounts in Linux and other Unix-like operating systems. However, the adduser is very similar to the useradd command because it is just a symbolic link to it.
When we run the useradd command in the Linux terminal performs the following major things:
- It edits
/etc/passwd,/etc/shadow,/etc/group, and/etc/gshadowfiles for the newly created user accounts. - Creates and populates a home directory for the new user.
- Sets permissions and ownerships to the home directory.
useradd Command Syntax
The basic syntax of the useradd command is:
# useradd [OPTIONS] USERNAME
useradd Command Options
| Option | Description |
|---|---|
-d DIR |
Create the user's home directory at a specified path |
-u UID |
Assign a specific User ID (UID) to the new user |
-g GID |
Assign a specific primary Group ID (GID) to the new user |
-G GROUPS |
Add the user to additional supplementary groups (comma-separated) |
-M |
Do not create a home directory for the user |
-e DATE |
Set the account expiry date in YYYY-MM-DD format |
-f DAYS |
Set the number of days after password expiry before the account is disabled |
-c COMMENT |
Add a custom comment (e.g. full name) to the /etc/passwd file |
-s SHELL |
Set the user's default login shell |
-m |
Create the user's home directory if it does not exist |
-k SKEL |
Use a custom skeleton directory instead of the default /etc/skel |
-U |
Create a group with the same name as the user |
-N |
Do not create a primary group for the user |
-r |
Create a system user account |
Let's discuss the most used useradd commands with their practical examples for better understanding. Here, we will divide these examples into two parts from the basic to the advanced usage of the command.
Part I β Basic Usage of useradd
1. Add a New User in Linux
To add/create a new user, you have to follow the command useradd or adduser with a username. The username is a user login name that is used by a user to log into the system.
Only one user can be added and that username must be unique (different from other usernames that already exist on the system).
For example, to add a new user called tecmint, use the following command.
root@TecMint:~# useradd tecmint
When we add a new user in Linux with the useradd command, it gets created in a locked state. To unlock that user account, we need to set a password for that account with the passwd command.
root@TecMint:~# passwd tecmint
New password:
Retype new password:
passwd: password updated successfully
Once a new user is created, their entry is automatically added to the /etc/passwd file. The file is used to store the user's information, and the entry should be: