Account
Categories

useradd command


Definition:

When the system admin wants to add a new user to the system, this command is used to manage their settings, such as creating a home directory, setting a password, and other related configurations.


Syntax:

useradd [options] username

Example 1 – Add a single user:

useradd shobha

Here, Shobha is a new user created by the admin.

Output:

If successful, no message is shown. If the user already exists: 
useradd: user 'shobha' already exists

Example 2 – Add a user with a home directory:

useradd -m prabha

Here, Prabha is a new user created by the admin, with a home directory set up for her.

Output: After successful execution, no message is shown.


Example 3 – Add a user with a specific shell:

useradd -s /bin/bash ravi

Here, Ravi is a new user created by the admin, with the default shell set to /bin/bash.

Output: After successful execution, no message is shown.


Example 4 – Add a user with a specific UID and group:

useradd -u 1050 -g developers raj

Here, Raj is a new user created by the admin, assigned UID 1050 and added to the developers group.

Output: After successful execution, no message is shown.


Options:

  • -m → If you use -m, a home directory is created for the user (see Example 2).
  • -s → If you use -s with a shell path, it sets that shell as the default (example: /bin/bash) (see Example 3).
  • -u → If you use -u with a number, it assigns that UID (user ID) to the user (see Example 4).
  • -g → If you use -g with a group name, the user is added to that primary group.

Example:

useradd -g teachers rahul

Here, Rahul is a new user created by the admin, and he is added to the primary group teachers.

  • -G → If you use -G with group names, the user is added to those extra groups.

Example:

useradd -G sales, marketing, priya

Here, Priya is a new user created by the admin, and she is added to the extra groups sales and marketing.

  • -c → If you use -c with text, it saves it as a comment or description for the user.

Example:

useradd -c "Project Manager" kavita

Here, Kavita is a new user created by the admin, and the comment “Project Manager” is added as her description.

  • -d → If you use -d with a path, that path becomes the user’s home directory.

Example:

useradd -d /data/users/sanjay sanjay

Here, Sanjay is a new user created by the admin, and his home directory is set to /data/users/sanjay.

  • -e → If you use -e with a date, it sets the account expiration date.

Example:

useradd -e 2025-12-31 rekha

Here, Rekha is a new user created by the admin, and her account will expire on 31st December 2025.

  • -p → If you use -p with a password, it sets that password (encrypted).

Example:

useradd -p mypassword ramesh

Here, Ramesh is a new user created by the admin, and his password is set (in encrypted form).

  • -r → If you use -r, a system account is created instead of a normal user.

Example:

useradd -r systemuser

Here, systemuser is created by the admin as a system account instead of a normal user.