Account
Categories

useradd command


Definition:

The word useradd means adding a user, i.e., creating a new user account in the system. Whenever there is a need to give someone access to a file or system resource, the admin uses this command to create that user. The admin can also form a group and add users to it to control access. This command is handy for organizing users and groups and assigning proper permissions.

Syntax:

useradd [options] username

Example:

Example 1 – Create a simple user

useradd shobha

Output:

(no output if successful)

Example 2 – Create a user with a home directory

useradd -m prabha

Output:

(no output if successful)

But you will be checking it.

ls /home

Output:

prabha  shobha

Here, Shobha created a new home directory for Prabha.

Example 3 – Create a user with a specific shell

useradd -s /bin/bash ravi

Output:

(no output if successful)

To check it:

cat /etc/passwd | grep ravi

Output:

ravi:x:1003:1003::/home/ravi:/bin/bash

Example 4 – In error case

useradd shobha

Output:

useradd: user 'shobha' already exists

Options:

  • -m → If you use this option with the command, it creates a home directory for the user.
  • -d /path/to/home → If you use this option with the command, it sets a custom home directory for the user.
  • -s /bin/bash → If you use this option with the command, it sets the default shell for the user.
  • -u UID → If you use this option with the command, it assigns a specific user ID (UID) to the new user.
  • -g GROUP → If you use this option with the command, it assigns a primary group to the new user.
  • -G GROUP1, GROUP2 → If you use this option with the command, it assigns the user to additional (secondary) groups.
  • -c "comment" → If you use this option with the command, it adds a description or comment for the new user.
  • -e YYYY-MM-DD → If you use this option with the command, it sets an expiration date for the user account.
  • -f DAYS → If you use this option with the command, it specifies the number of days after a password expires until the user account is disabled.
  • -p PASSWORD → If you use this option with the command, it sets the encrypted password for the new user.