The useradd command is a low-level utility used for adding users to the system. It is available on nearly all Unix-like systems (Linux, BSD, etc.) and is typically used in scripts, system configuration, or by administrators to create users in an automated fashion.
-
When executed,
useraddmakes entries in various system files such as:/etc/passwd: This file contains the list of users./etc/shadow: Contains encrypted passwords for users./etc/group: Contains group information./etc/gshadow: Holds the encrypted password for groups (if applicable).
-
The command doesn't automatically create a home directory or set a password (unless you use the appropriate options). The system administrator must manually configure those settings after running
useradd.
-
-m: Create the user's home directory. If not specified, the home directory is not created.sudo useradd -m john
This will create the user
johnand their home directory/home/john. -
-d <dir>: Specify a custom home directory.sudo useradd -m -d /home/custom_dir john
This will create the user
johnand their home directory as/home/custom_dir. -
-s <shell>: Set the login shell for the user. This is the shell that will be used when the user logs in (default is usually/bin/bash).sudo useradd -m -s /bin/zsh john
This will create the user
johnwith Zsh as their default shell. -
-G <group1,group2,...>: Add the user to one or more groups (separate group names with commas).sudo useradd -m -G sudo,developers john
This creates
johnand adds them to both thesudoanddevelopersgroups. -
-e <date>: Set an expiration date for the user account inYYYY-MM-DDformat.sudo useradd -e 2025-12-31 john
This sets the user
johnto expire on December 31, 2025. -
-c <comment>: Add a description for the user (this can be a full name or other details).sudo useradd -m -c "John Doe, Developer" john
After creating a user with useradd, you often need to set a password for the account manually:
sudo passwd johnThis will prompt you to enter a new password for the user john.
A basic usage example of useradd:
sudo useradd -m -s /bin/bash johnThis command will:
- Create the user
john. - Create the home directory
/home/john. - Set the default shell as
/bin/bash(standard shell in Linux).
Afterward, the password would need to be set with sudo passwd john.
The adduser command is a higher-level, more user-friendly tool for adding users. It is often considered a "wrapper" around the useradd command. While useradd is more direct and requires more options to be specified explicitly, adduser automates many of the tasks, such as creating a home directory and prompting for user information.
It is typically found on Debian-based systems, such as Ubuntu and Debian.
adduserinteracts with the system in a more guided manner. It automatically sets up the user with sensible defaults (like creating a home directory, setting permissions, prompting for a password, and asking for user information).- It also uses
useraddinternally but provides a more interactive experience.
- Create the User: The command creates the user with the specified username and automatically assigns it a home directory.
- Set the Password: You will be prompted to enter and confirm a password for the new user.
- User Information: The command asks for additional user information (such as full name, phone number, etc.), although this is optional.
- Confirm: You’ll be asked to confirm if the information is correct.
- Add User to Groups: You will have the option to add the user to predefined groups, like
sudo, or create custom groups. - Done: The new user account is created, and the system is configured with default settings.
sudo adduser johnThe command will prompt you to fill in the following details:
- New password for
john. - Full name and other information (optional).
- It will then create the user
johnand set up their environment.
- Interactive Setup: This command takes care of more tasks automatically. For example, it creates the home directory, assigns proper permissions, and even sets up the user’s environment (like
.bashrc). - Guided User Setup: It asks for a password, user details, and other configurations interactively, reducing the need for specifying options manually.
| Feature | useradd |
adduser |
|---|---|---|
| Type | Low-level command (more control) | High-level, interactive tool (user-friendly) |
| Home Directory | Not created by default unless -m option used |
Automatically created with sensible permissions |
| Password | Not set by default | Password set interactively during user creation |
| Additional Info | No prompts for user info | Prompts for full name, phone number, etc. (optional) |
| Groups | Needs -G option to assign groups |
Prompts to add the user to groups |
| Shell | Needs -s option to specify shell |
Uses /bin/bash by default |
| Default Use Case | Typically used in scripts, automation, or by sysadmins | Typically used for creating users interactively |
For more detailed information about each command and their available options, you can run the following commands:
-
For
useradd:useradd --help
-
For
adduser:adduser --help
These commands will display the full set of options and explanations that can help you tailor the usage to your specific needs.
- Use
useraddwhen you need full control over the user creation process and want to automate tasks. It is ideal for scripts or system administrators who need to create users without being prompted for additional information. - Use
adduserwhen you want a more user-friendly, guided approach that automatically takes care of home directory creation, password setup, and other tasks. It's great for administrators or users who prefer a simpler, less error-prone process.
Let me know if you'd like to explore any particular part of these commands further!