How to Create a User in Linux – useradd
Command
In Linux, new user accounts can be created using the useradd
command. This is essential for managing system access, assigning user privileges, and setting up personal workspaces.
Syntax of useradd
By default, useradd
creates a user but does not set a password until manually assigned.
Create a New User
To add a new user:
Example:
This creates a new user alice
but does not create a home directory by default.
Create a User with a Home Directory
To automatically create a home directory in /home/username
:
Example:
The home directory /home/alice
is created.
Set a Password for the User
After creating a user, set their password:
Example:
You'll be prompted to enter and confirm a password.
Create a User with a Specific Home Directory
To specify a custom home directory:
Example:
This sets /opt/alice
as Alice’s home directory.
Create a User with a Specific UID
Each user has a unique User ID (UID). To specify a custom UID:
Alice is assigned UID 2001
.
Create a User with a Specific Group
To assign a primary group:
Example:
Alice's primary group is developers
.
To add the user to multiple groups:
Now, Alice is in the sudo
and docker
groups.
Create a User with an Expiry Date
To set an expiration date:
Example:
Alice’s account will expire on December 31, 2025.
To check the expiration date:
Create a System User
System users are for system services (without login access):
Verify User Creation
To check user details:
Example:
Output:
To list all users:
Difference Between useradd
, adduser
, and usermod
Command | Purpose |
---|---|
useradd | Creates a new user (default Linux command). |
adduser | Interactive script for creating users (Debian-based systems). |
usermod | Modifies an existing user. |
Conclusion
The useradd
command is essential for user management in Linux, allowing administrators to create and configure new user accounts efficiently.
Would you like additional details or SEO optimization? 🚀