Enhancing Skills

useradd: Create a new user account

Command: useradd

The useradd command is used to create a new user account on the system. It allows specifying various options to configure the new user’s home directory, default shell, user ID, and more. This command requires superuser (root) privileges.


Sample Commands and Outputs:

  • useradd -m -s /bin/bash username: Creates a new user with a home directory and default shell. Sample Command and Output:
  $ sudo useradd -m -s /bin/bash user1

Description:

  • -m: Creates the user’s home directory if it does not exist.
  • -s /bin/bash: Specifies /bin/bash as the user’s default shell.
  • user1: The username for the new account.
  • useradd -d /custom/home/dir -s /bin/zsh username: Creates a new user with a custom home directory and shell. Sample Command and Output:
  $ sudo useradd -d /home/customuser -s /bin/zsh customuser

Description:

  • -d /custom/home/dir: Specifies a custom home directory for the user.
  • -s /bin/zsh: Specifies /bin/zsh as the user’s default shell.
  • customuser: The username for the new account.
  • useradd -u 1050 -g groupname -c "Full Name" username: Creates a new user with a specific UID, primary group, and comment. Sample Command and Output:
  $ sudo useradd -u 1050 -g developers -c "John Doe" johndoe

Description:

  • -u 1050: Specifies the user ID (UID) for the new user.
  • -g groupname: Specifies the primary group for the user.
  • -c "Full Name": Adds a comment, typically the user’s full name.
  • johndoe: The username for the new account.
  • useradd -e 2024-12-31 username: Creates a new user with an account expiration date. Sample Command and Output:
  $ sudo useradd -e 2024-12-31 tempuser

Description:

  • -e 2024-12-31: Sets the expiration date for the user account in YYYY-MM-DD format.
  • tempuser: The username for the new account.
  • useradd -g groupname -G additionalgroups -s /bin/bash username: Creates a new user with additional groups. Sample Command and Output:
  $ sudo useradd -g users -G admins,developers -s /bin/bash newuser

Description:

  • -g groupname: Specifies the primary group for the user.
  • -G additionalgroups: Specifies additional groups the user will belong to.
  • -s /bin/bash: Specifies /bin/bash as the user’s default shell.
  • newuser: The username for the new account.

Note: The useradd command is used to create new user accounts and set initial configurations. After creating a user, it is often necessary to set a password using the passwd command and configure other settings as needed.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.