Linux Tutorial

Linux File/Directory Management

Linux Packaging And Compression

Vim Text Editor

Linux Text Processing

Linux Software Installation

Linux User/User Group Management

Linux Permission Management

Linux Filesystem Management

Linux Advanced Filesystem Management

Linux System Management

Linux Backup and Recovery

Linux System Service Management

Linux System Log Management

Linux Boot Management

LAMP/LNMP Environment

SELinux Management

Linux useradd Command: Adding A New System User

The useradd command in Linux is used to create new user accounts on a system. It sets up various aspects of a user's account, such as their home directory, user ID, default shell, and more. In this tutorial, we will cover how to use the useradd command and its various options.

Adding a new user

To create a new user, simply run the useradd command followed by the desired username:

sudo useradd newuser

Replace newuser with the desired username. This command creates a new user with default settings, which usually includes the following:

  • A new group with the same name as the user
  • A new home directory located at /home/newuser
  • A default shell (usually /bin/bash or /bin/sh)

Creating a new user with a specific home directory

To create a user with a custom home directory, use the -d flag followed by the desired directory path:

sudo useradd -d /custom/home newuser

Creating a new user with a specific user ID (UID)

To create a user with a specific user ID (UID), use the -u flag followed by the desired UID:

sudo useradd -u 1005 newuser

Creating a new user with a specific group ID (GID)

To create a user with a specific group ID (GID), use the -g flag followed by the desired GID:

sudo useradd -g 1005 newuser

Creating a new user with a specific default shell

To create a user with a specific default shell, use the -s flag followed by the desired shell:

sudo useradd -s /bin/tcsh newuser

Creating a new user with a specific comment

To add a comment (e.g., the full name of the user) to the user's account, use the -c flag followed by the comment text:

sudo useradd -c "New User" newuser

Creating a new user without a default group

By default, a new group with the same name as the user is created. To disable this behavior and add the user to a pre-existing group, use the -N flag:

sudo useradd -N newuser

Creating a user account with multiple options

You can combine multiple options to create a user account with specific settings:

sudo useradd -d /custom/home -u 1005 -g 1005 -s /bin/tcsh -c "New User" newuser

Setting a password for the new user

After creating a new user, you should set a password for them using the passwd command:

sudo passwd newuser

You will be prompted to enter the new password twice.

In conclusion, the useradd command is an essential tool for creating and managing user accounts in Linux. By combining various options, you can create user accounts tailored to specific requirements and settings.

  1. How to use the Linux useradd command:

    • Description: The useradd command is used to add new user accounts in Linux.
    • Code:
      # Example: Adding a new user
      sudo useradd newuser
      
  2. Adding a new system user in Linux:

    • Description: System users are typically used for running services or applications. The -r option can be used to create a system user.
    • Code:
      # Example: Adding a system user
      sudo useradd -r sysuser
      
  3. Setting user properties during user creation:

    • Description: Various properties such as user ID (UID), group ID (GID), and others can be set during user creation.
    • Code:
      # Example: Setting user properties during creation
      sudo useradd -u 1001 -g 1001 newuser
      
  4. Assigning a home directory and shell to a new user:

    • Description: The -m option ensures the creation of the user's home directory, and the -s option sets the login shell.
    • Code:
      # Example: Assigning home directory and shell
      sudo useradd -m -s /bin/bash newuser
      
  5. Adding users to specific groups with useradd:

    • Description: The -G option allows adding users to specific groups during user creation.
    • Code:
      # Example: Adding a user to a specific group
      sudo useradd -G group1,group2 newuser
      
  6. Creating a user with an expiration date in Linux:

    • Description: The -e option sets an expiration date for the user account.
    • Code:
      # Example: Creating a user with expiration date
      sudo useradd -e 2023-12-31 newuser
      
  7. Password management while using useradd:

    • Description: User passwords can be set using the passwd command after creating the user. Alternatively, the -p option can be used to set an encrypted password.
    • Code:
      # Example: Setting a password for a new user
      sudo passwd newuser
      
  8. useradd examples for various scenarios in Linux:

    • Description: useradd can be customized for different scenarios, combining options to suit specific requirements.
    • Code:
      # Example: Creating a user with specific options
      sudo useradd -m -G group1 -s /bin/bash newuser
      
  9. Troubleshooting common issues with useradd:

    • Description: Troubleshooting may involve checking system logs, verifying syntax, or investigating permission issues.
    • Code:
      # Example: Troubleshooting useradd issues
      sudo useradd newuser