How useradd command works in Linux

Updated: Sep 11

Useradd is a basic Linux command to add new users. We will review how to add new users in Linux with useradd command. A Linux user must belong to a group. If we don't specify the group info for the new user, this info will be chosen by OS.




Understanding New User defaults

The following two files define the default info when users are created by the useradd command.

  • /etc/login.defs - This file defines entries for mail, password complexity and limitations, UID and GID minimum and maximum values, and whether the user’s home directory is created by default.

  • /etc/default/useradd - This file defines the defaults of shell,home directory,skel file,group id etc.



How Useradd works?

The useradd command performs the below tasks:

  • It edits the files for newly created user like /etc/passwd, /etc/shadow, /etc/group and /etc/gshadow.

  • It creates and opens a new home directory. By default, the new home directory is a copy of the /etc/skel directory and its contents.

  • It allows us to set ownerships and permissions to the home directory.



Choose Group Info for New User

If we specify the group info for this user with -g option, the new user will belong to that group.


We will check the rules to see how useradd choose which group for new users if we don't specify the group info in the command line.

  • check the /etc/logins.defs file , for the variable named: USERGROUPS_ENAB

  • If USERGROUPS_ENAB is set to yes , useradd will create a group for that user which has a name same as the user username.

  • If USERGROUPS_ENAB is set to no , then useradd will check the value of the variable GROUP defined inside : /etc/default/useradd

  • If GROUP is defined and not commented out then useradd will add the user to the group specified by GROUP .

  • If GROUP is not defined or is commented out, then useradd will assign the user to the group with the group id of 100 .


The following flow chart tells us how useradd works in Linux.

How useradd works in Linux
how useradd works in Linux



Create new users with useradd command

The following options are commonly used options for useradd command.


useradd -d /home/howtouselinux -s /bin/bash -g my_group  username
  • -d: specify the user home directory

  • -s: specify the user shell

  • -g: specify the user group


Add a new user in Linux with useradd command

We can use the useradd command without any options like this:


useradd new_username

It will create the user account but:

  • the user’s home directory won’t be created

  • the password has to be set separately

  • the default shell for the user will be sh

we can set a password for this new user account using the passwd command:

passwd new_username

we can also create home directory while creating the user.




Add a user with home directory with useradd command

The -m option of useradd command allows to copy all files from wer system skeleton directory (/etc/skel) to the newly created home directory.


In other words, we can create a user with home directory using the -m option as well.


useradd -m new_username

We can also specify an existing directory as the home directory of the newly created user with option -d.


useradd -d Path_to_Existing_Directory new_username



Add a new user with different shell

The default shell for a user created with useradd command is sh. These days sh shell is hardly used when there is bash and zsh.


A user can change his/her default shell but we can also create the user with a different default shell with the option -s.


For example, if we want the new user to use bash as the default shell, here’s what we can do:


useradd -s /bin/bash new_username




Add a new user with different group

Usually, when we create a new user, a group with the same name as the user is created. The new user is added as a member of this group.


With the option -g, we can add a new user to an already existing group as its default group.

useradd -g Existing_Group_Name_or_ID new_username

Suppose we are creating an account for a developer. Adding all the developers to a ‘dev group’ could be a strategy.


we can also add the user to additional (existing) groups with option -G.


useradd -G group_1 group_2 new_username

So if we are creating a sudo user, we can simply add the user to the sudo group while creating it.



Add a new user with specific user ID (UID)

we may also create a new user with a specific user ID with the option -u of useradd command:


useradd -u uid new_username

Bonus Tip: Combine multiple options to create a new user with different parameters

we can combine multiple options together to create a new user in Linux with a predefined configuration.


useradd -d /home/howtouselinux -s /bin/bash -g my_group 

Related Post:

How Userdel Works in Linux

Basic Linux Commands: Linux User Management




415 views