Skip to content

What is useradd Linux command?

The useradd command in Linux is used to create new user accounts. It allows you to specify various attributes for the new user, such as the home directory, login shell, and User ID. By using useradd, system administrators can easily and efficiently add and manage user accounts on a Linux system.

useradd Syntax:

Terminal window
useradd [options] username

useradd Options:

OptionDescription
-c, —comment COMMENTSpecify a comment describing the user
-m, —create-homeCreate the user’s home directory
-g, —gid GROUPSpecify the initial login group for the user
-G, —groups GROUPSSpecify additional groups for the user
-s, —shell SHELLSpecify the user’s login shell
-u, —uid UIDSpecify the user ID for the new user
-p, —password PASSWORDSet the encrypted password for the user

Parameters:

ParameterDescription
usernameThe name of the user to be created

useradd Command Usage Examples:

Create a New User

Terminal window
useradd john

Creates a new user with the username “john”.

Set Home Directory for New User

Terminal window
useradd -d /home/jane jane

Creates a new user named “jane” with the home directory set to “/home/jane”.

Assign User ID when Creating a User

Terminal window
useradd -u 1001 jerry

Creates a new user with the username “jerry” and assigns the user ID 1001.

Specify User’s Shell

Terminal window
useradd -s /bin/bash mike

Creates a new user named “mike” and sets the default shell to “/bin/bash”.

Create a System User

Terminal window
useradd -r serveruser

Creates a system user named “serveruser” with a UID lower than 1000.

How do I use useradd in Linux?

To use the useradd command in bash, execute the following command:

Terminal window
useradd example_user

What is the syntax for useradd command?

The syntax for the useradd command in Linux is as follows:

Terminal window
useradd [options] username

How can I create a new user with a specific home directory using useradd?

To create a new user with a specific home directory, use the -d option with the useradd command. For example:

Terminal window
useradd -d /home/new_user example_user

How to set the expiration date for a user account with useradd?

You can set the expiration date for a user account using the -e option with the useradd command. Here’s an example:

Terminal window
useradd -e 2023-12-31 example_user

How to create a new user and assign a specific user ID (UID) using useradd?

To create a new user and assign a specific user ID (UID), use the -u option with the useradd command. For example:

Terminal window
useradd -u 1001 example_user

How can I set the default login shell for a new user with useradd?

To set the default login shell for a new user, use the -s option with the useradd command. Here’s an example:

Terminal window
useradd -s /bin/bash example_user

How to create a new user and add the user to a specific group using useradd in Linux?

To create a new user and add the user to a specific group, use the -g option with the useradd command. For example:

Terminal window
useradd -g group_name example_user

How do I create a new user and assign multiple supplementary groups with useradd?

To create a new user and assign multiple supplementary groups, use the -G option with the useradd command. Here’s an example:

Terminal window
useradd -G group1,group2 example_user

Applications of the useradd command

  • Creating a new user account on a Linux system
  • Specifying user details like username, home directory, and default shell
  • Setting user account expiration dates
  • Assigning a group to the new user
  • Setting a password for the new user
  • Creating user’s home directory if it does not already exist
  • Adding user to specific supplementary groups
  • Setting user account attributes like UID and GID
  • Providing user account with specific login environment configurations