How useradd command works in Linux

Updated: May 6

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 defaults 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

Related Post:

How Userdel Works in Linux

Basic Linux Commands: Linux User Management


Join our newsletter! Get a free cloud server!  Never miss a post!

Thanks for submitting!