useradd Command in Linux

Topics Covered

Overview

The useradd command in Linux is a powerful tool that allows administrators to create and manage new user accounts on a Linux system. The command provides various options to customize the user account creation process, such as setting up a specific home directory, assigning a default shell, and applying a custom user ID.

Linux useradd Command Syntax

The syntax for the useradd command is as follows:

Where:

  • options: The options are flags that can be used to customize the user account creation process.
  • username: The desired username for the new user account.

useradd Command Options:

  • -c: Add a comment to the user account, usually the user's full name.
  • -d: Specify a custom home directory for the new user.
  • -g: Assign the new user to an existing group by providing the group name or group ID.
  • -G: Assign the new user to multiple supplementary groups, separated by commas.
  • -s: Specify a custom default shell for the new user.
  • -u: Assign a custom user ID (UID) for the new user.
  • -M: Do not create a home directory for the new user.
  • -N: Do not create a primary group for the new user.

Example Usages

  • Create a new user with the default options:

    Explanation: This command creates a new user named 'john' with the default options, such as a home directory in /home/john and the default shell assigned by the system.

  • Create a new user with a custom home directory:

    Explanation: This command creates a new user named 'jane' and specifies a custom home directory at /custom/home/directory.

Tips

  • Always use the '-m' option with '-d' to create the specified home directory if it does not exist.

  • To avoid conflicts, make sure to check for existing users with the same name or UID before creating a new user.

Advanced Use Cases of useradd Command in Linux

  • Create a new user with a custom comment, home directory, and shell:

    Explanation: This command creates a new user named 'jane', with a custom comment 'Jane Doe', a custom home directory at /custom/home/directory, and the Zsh shell as the default shell.

  • Create a new user and assign to multiple supplementary groups:

    Explanation: This command creates a new user named 'john' and assigns the user to multiple supplementary groups: group1, group2, and group3.

  • Create a new user with a custom UID:

    Explanation: This command creates a new user named 'jane' and assigns a custom UID of 1500.

Conclusion

  • The useradd command is a powerful tool for creating and managing user accounts in Linux.

  • Various options allow customization of the user account creation process.

  • Always check for existing users with the same name or UID before creating a new user.