Linux useradd command

|

What is the useradd Command?

The word useradd means adding a user, that is, creating a new user account in the system. Whenever someone needs access to a file or system resource, the administrator uses this command to create a new user account. The administrator can also create a group and add users to it to control access. This command helps organize users and groups and assign appropriate permissions.

Syntax:

useradd [options] username

Example 1: Create a simple user


Command:

useradd shobha

Output:

(no output if successful)

Example 2: Create a user with a home directory


Command:

useradd -m prabha

Output:

(no output if successful)

But you will be checking it.

ls /home

Output:

prabha  shobha

Here, the command creates a new home directory for Prabha.


Example 3: Create a user with a specific shell


Command:

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: Error Case


Command:

useradd shobha

Output:

useradd: user 'shobha' already exists

Options:

  1. -m → If you use this option with the command, it creates a home directory for the user.
  2. -d /path/to/home → If you use this option with the command, it sets a custom home directory for the user.
  3. -s /bin/bash → If you use this option with the command, it sets the default shell for the user.
  4. -u UID → If you use this option with the command, it assigns a specific user ID (UID) to the new user.
  5. -g GROUP → If you use this option with the command, it assigns a primary group to the new user.
  6. -G GROUP1, GROUP2 → If you use this option with the command, it assigns the user to additional (secondary) groups.
  7. -c "comment" → If you use this option with the command, it adds a description or comment for the new user.
  8. -e YYYY-MM-DD → If you use this option with the command, it sets an expiration date for the user account.
  9. -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.
  10. -p PASSWORD → If you use this option with the command, it sets the encrypted password for the new user.

ONLINE WEB TOOLS

WhatIsMyIpAddress
Shorterner