Create User in Linux

Create User in Linux

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

useradd [OPTIONS] username

By default, useradd creates a user but does not set a password until manually assigned.

Create a New User

To add a new user:

sudo useradd username

Example:

sudo useradd alice

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:

sudo useradd -m username

Example:

sudo useradd -m alice

The home directory /home/alice is created.

Set a Password for the User

After creating a user, set their password:

sudo passwd username

Example:

sudo passwd alice

You'll be prompted to enter and confirm a password.

Create a User with a Specific Home Directory

To specify a custom home directory:

sudo useradd -m -d /custom/path username

Example:

sudo useradd -m -d /opt/alice alice

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:

sudo useradd -u 2001 alice

Alice is assigned UID 2001.

Create a User with a Specific Group

To assign a primary group:

sudo useradd -g groupname username

Example:

sudo useradd -g developers alice

Alice's primary group is developers.

To add the user to multiple groups:

sudo useradd -G sudo,docker alice

Now, Alice is in the sudo and docker groups.

Create a User with an Expiry Date

To set an expiration date:

sudo useradd -e YYYY-MM-DD username

Example:

sudo useradd -e 2025-12-31 alice

Alice’s account will expire on December 31, 2025.

To check the expiration date:

sudo chage -l alice

Create a System User

System users are for system services (without login access):

sudo useradd -r systemuser

Verify User Creation

To check user details:

getent passwd username

Example:

getent passwd alice

Output:

alice:x:1001:1001::/home/alice:/bin/bash

To list all users:

cut -d: -f1 /etc/passwd

Difference Between useradd, adduser, and usermod

CommandPurpose
useraddCreates a new user (default Linux command).
adduserInteractive script for creating users (Debian-based systems).
usermodModifies 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? 🚀

Souy Soeng

Souy Soeng

Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

Github

Post a Comment

CAN FEEDBACK
close