Wednesday, January 15, 2025
HomeTechHow to add User in Linux | user add Command

How to add User in Linux | user add Command

In Linux, you can add a new user to the system using the useradd command. This command creates a new user, allowing you to assign a username, configure a home directory, set a password, and more.

Syntax of the useradd Command

useradd [options] username
  • username: The name of the new user you want to add.
  • [options]: Various options that allow you to customize the user creation process (e.g., setting the home directory, shell, etc.).

Steps to Add a User Using the useradd Command

  1. Open Terminal: Launch your terminal (you need superuser privileges).
  2. Use the useradd Command: To add a new user, you can simply run the useradd command with the desired username.

    Example:

    sudo useradd username
    

    Replace username with the name of the user you wish to create.

  3. Set a Password for the New User: After creating the user, you’ll need to set a password. You can do this using the passwd command.

    Example:

    sudo passwd username
    

    You will be prompted to enter the new password for the user.

  4. Optional: Specify Additional Options: You can customize the user creation with additional options:
    • Set the Home Directory: By default, useradd creates the user’s home directory as /home/username. You can specify a custom home directory with the -d option.
      sudo useradd -d /custom/directory username
      
    • Assign the User to a Group: You can assign the user to a specific group using the -G option.
      sudo useradd -G groupname username
      
    • Set the Default Shell: You can specify the default shell (e.g., /bin/bash) using the -s option.
      sudo useradd -s /bin/bash username
      
    • Create a Home Directory: By default, useradd creates a home directory. If you want to explicitly create the home directory (in case it’s not created automatically), use the -m option:
      sudo useradd -m username
      
    • Expire the Account After a Specific Date: You can set an expiration date for the user account with the -e option:
      sudo useradd -e 2025-12-31 username
      
  5. Verify the New User: To check if the user has been added successfully, you can list the contents of the /home/ directory or use the id command to display user information.
    id username
    

    This will display the user ID, group ID, and groups the user belongs to.

Example: Creating a User with Custom Options

sudo useradd -m -s /bin/bash -G sudo username
sudo passwd username

This command:

  • Creates the user username.
  • Sets the default shell to /bin/bash.
  • Adds the user to the sudo group.
  • Creates the home directory for the user.

Conclusion

The useradd command in Linux is a powerful tool for adding new users to the system. By using various options, you can customize the user’s environment, set their password, and configure group memberships. Always ensure you set a secure password for the user and verify the creation process to confirm the user has been added successfully.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Linked List Data Structure

Who Is Joey Cold Cuts?

What is Graph Algorithms

What is a Loopback Address?

Recent Comments

0
Would love your thoughts, please comment.x
()
x