Skip to content

Latest commit

 

History

History
151 lines (110 loc) · 7.04 KB

File metadata and controls

151 lines (110 loc) · 7.04 KB

1. useradd Command

The useradd command is a low-level utility used for adding users to the system. It is available on nearly all Unix-like systems (Linux, BSD, etc.) and is typically used in scripts, system configuration, or by administrators to create users in an automated fashion.

How useradd Works:

  • When executed, useradd makes entries in various system files such as:

    • /etc/passwd: This file contains the list of users.
    • /etc/shadow: Contains encrypted passwords for users.
    • /etc/group: Contains group information.
    • /etc/gshadow: Holds the encrypted password for groups (if applicable).
  • The command doesn't automatically create a home directory or set a password (unless you use the appropriate options). The system administrator must manually configure those settings after running useradd.

Common Options for useradd:

  • -m: Create the user's home directory. If not specified, the home directory is not created.

    sudo useradd -m john

    This will create the user john and their home directory /home/john.

  • -d <dir>: Specify a custom home directory.

    sudo useradd -m -d /home/custom_dir john

    This will create the user john and their home directory as /home/custom_dir.

  • -s <shell>: Set the login shell for the user. This is the shell that will be used when the user logs in (default is usually /bin/bash).

    sudo useradd -m -s /bin/zsh john

    This will create the user john with Zsh as their default shell.

  • -G <group1,group2,...>: Add the user to one or more groups (separate group names with commas).

    sudo useradd -m -G sudo,developers john

    This creates john and adds them to both the sudo and developers groups.

  • -e <date>: Set an expiration date for the user account in YYYY-MM-DD format.

    sudo useradd -e 2025-12-31 john

    This sets the user john to expire on December 31, 2025.

  • -c <comment>: Add a description for the user (this can be a full name or other details).

    sudo useradd -m -c "John Doe, Developer" john

Setting Password after User Creation:

After creating a user with useradd, you often need to set a password for the account manually:

sudo passwd john

This will prompt you to enter a new password for the user john.

Creating User with useradd:

A basic usage example of useradd:

sudo useradd -m -s /bin/bash john

This command will:

  • Create the user john.
  • Create the home directory /home/john.
  • Set the default shell as /bin/bash (standard shell in Linux).

Afterward, the password would need to be set with sudo passwd john.


2. adduser Command

The adduser command is a higher-level, more user-friendly tool for adding users. It is often considered a "wrapper" around the useradd command. While useradd is more direct and requires more options to be specified explicitly, adduser automates many of the tasks, such as creating a home directory and prompting for user information.

It is typically found on Debian-based systems, such as Ubuntu and Debian.

How adduser Works:

  • adduser interacts with the system in a more guided manner. It automatically sets up the user with sensible defaults (like creating a home directory, setting permissions, prompting for a password, and asking for user information).
  • It also uses useradd internally but provides a more interactive experience.

Common Steps When Running adduser:

  1. Create the User: The command creates the user with the specified username and automatically assigns it a home directory.
  2. Set the Password: You will be prompted to enter and confirm a password for the new user.
  3. User Information: The command asks for additional user information (such as full name, phone number, etc.), although this is optional.
  4. Confirm: You’ll be asked to confirm if the information is correct.
  5. Add User to Groups: You will have the option to add the user to predefined groups, like sudo, or create custom groups.
  6. Done: The new user account is created, and the system is configured with default settings.

Example of Running adduser:

sudo adduser john

The command will prompt you to fill in the following details:

  • New password for john.
  • Full name and other information (optional).
  • It will then create the user john and set up their environment.

Key Features of adduser:

  • Interactive Setup: This command takes care of more tasks automatically. For example, it creates the home directory, assigns proper permissions, and even sets up the user’s environment (like .bashrc).
  • Guided User Setup: It asks for a password, user details, and other configurations interactively, reducing the need for specifying options manually.

3. Differences Between useradd and adduser

Feature useradd adduser
Type Low-level command (more control) High-level, interactive tool (user-friendly)
Home Directory Not created by default unless -m option used Automatically created with sensible permissions
Password Not set by default Password set interactively during user creation
Additional Info No prompts for user info Prompts for full name, phone number, etc. (optional)
Groups Needs -G option to assign groups Prompts to add the user to groups
Shell Needs -s option to specify shell Uses /bin/bash by default
Default Use Case Typically used in scripts, automation, or by sysadmins Typically used for creating users interactively

4. Further Details:

For more detailed information about each command and their available options, you can run the following commands:

  • For useradd:

    useradd --help
  • For adduser:

    adduser --help

These commands will display the full set of options and explanations that can help you tailor the usage to your specific needs.


5. Conclusion

  • Use useradd when you need full control over the user creation process and want to automate tasks. It is ideal for scripts or system administrators who need to create users without being prompted for additional information.
  • Use adduser when you want a more user-friendly, guided approach that automatically takes care of home directory creation, password setup, and other tasks. It's great for administrators or users who prefer a simpler, less error-prone process.

Let me know if you'd like to explore any particular part of these commands further!