How to Create and Manage Users Using Useradd Linux Command

In this article, we are going to learn how to create and manage users using useradd Linux command. useradd command is a Basic command in Linux is used to create and manage users. It comes with so many options and arguments to manage the users like Set Expiry date for the user, Password Inactivity, set UID (User ID) and Group ID (GID) as per your choice, Set Home Directory of the User and so on.

Create and Manage Users Using Useradd Linux Command
Create and Manage Users Using Useradd Linux Commands

Follow the below useradd Linux commands with Examples:

Create a New User

Create a new user using useradd Linux commands. Refer to the command below.

[root@localhost ~]# useradd itsmarttricks   # Create a New User

Set Password for the New User.

[root@localhost ~]# passwd itsmarttricks # Set Password for the New User
Changing password for user itsmarttricks.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.

Set User ID (UID) for New User

You can set the User ID of your own choice by using useradd Linux command with argument -u.

Note: The User ID should be Greater than OR equal to 500 and Should not already be assigned to any User.

[root@localhost ~]# useradd -u 555 user2  # Set UID for a New User
[root@localhost ~]# id user2
uid=555(user2) gid=555(user2) groups=555(user2)

Set Group ID (GID) for New User

Like UID we also can set GID of our own choice using useradd Linux command with argument -g.

[root@localhost ~]# useradd -g 556 user4   # Set GID for a New User
[root@localhost ~]# id user4
uid=556(user4) gid=556(workers) groups=556(workers)

Set UID and GID for New User in one Command

Set UID and GID for New User in one command. refer to the below command.

[root@localhost ~]# useradd -u 551 -g 556 user5  # Set UID & GID in one command
[root@localhost ~]# id user5
uid=551(user5) gid=556(workers) groups=556(workers)

Add Supplementary Groups to a New User

To add multiple supplementary/Secondary groups to a New User we can use useradd Linux command with argument -G. When you are adding a user to Multiple Supplementary groups separate groups by a comma “,“.

[root@localhost ~]# useradd -G workers,developers,admins user7  # Add Multiple Supplement Groups to a New User
[root@localhost ~]# id user7
uid=557(user7) gid=559(user7) groups=559(user7),556(workers),557(developers),558(admins)

Set Comment for New User

Set Comment for a New User. The comment may be anything like the Address of the User, Telephone Number, or maybe anything else.  We can do so by using useradd Linux command with argument -c.

[root@localhost ~]# useradd -c "first user" user1   # Set Comment for a New User
[root@localhost ~]# cat /etc/passwd | grep user1
user1:x:502:502:first user:/home/user1:/bin/bash

Set Directory for New User

The default home directory of a New user is /home. But we can set our own Home directory by using useradd command with argument -d. Refer to the command below.

[root@localhost ~]# useradd -d /myusers/user10/ user10  # Set your Home Directory
[root@localhost ~]# cd /myusers/
[root@localhost myusers]# ls
user10    # The Directory as Users name will create Automatically
[root@localhost user10]# ls -a
.  ..  .bash_logout  .bash_profile  .bashrc  .gnome2  .mozilla   # Users Default Profile Files

Set Expiry Date for a New User

We can set an expiry date for a new user by using useradd Linux command with argument -e.

[root@localhost user10]# useradd -e 2019-03-25 user11  # Set Expiry date for a New User

To confirm the same we can use chage command. refer to the output below.

[root@localhost user10]# chage -l user11
Last password change                                    : May 19, 2017
Password expires                                        : never
Password inactive                                       : never
Account expires                                         : Mar 25, 2019
Minimum number of days between password change          : 0
Maximum number of days between password change          : 99999
Number of days of warning before password expires       : 7

Don’t Create a Group as the same Name as User

Normally when we create a New User system automatically creates a group with the same name as the user. But to avoid this you can use useradd Linux command with argument -N.

[root@localhost ~]# useradd -N user12  # Create a New User without creating a Group same name as User

Don’t Create a Home Directory for a New User

If you want to create a new user without a home directory you can use useradd Linux command with argument -M.

[root@localhost ~]# useradd -M user17   # Create a new user without home directory

Assign the Same UID for Multiple New Users

We can assign the same UID to Multiple users. To do so you can use useradd Linux command with argument -o. Refer to the command below.

[root@localhost ~]# useradd -o -u 700 u5  # assign same UID to Multiple Users
[root@localhost ~]# useradd -o -u 700 u6  # assign same UID to Multiple Users
[root@localhost ~]# useradd -o -u 700 u7  # assign same UID to Multiple Users

To confirm the same reference to the below command.

[root@localhost ~]# tail -n -3 /etc/passwd
u5:x:700:700::/home/u5:/bin/bash
u6:x:700:1004::/home/u6:/bin/bash
u7:x:700:1005::/home/u7:/bin/bash

Set Password Inactivity Period for New User

To set Password Inactivity for a user we can user useradd Linux command with argument -f. Password Inactivity is nothing but a setting by which the user will allow to log in to the system using his Username even after the Password expiration for the Number of days you allowed.

For Example, Here I am assigning 25 days as a Password Inactivity for user20. That means user20 will able to log in to the system using his Username for the next 25 days even after password expiration.

[root@localhost ~]# useradd -f 25 user20  # Set Password Inactivity
[root@localhost ~]# cat /etc/shadow | grep user20
user20:$1$3wMOdwJC$LYGq4eEhcHnxpw5mxAJhl.:17306:0:99999:7:25::

Check Default Settings of a User

To check the Default settings of a User use useradd Linux command with argument -D. Default settings are like Home Directory of the User, Shell of the User..etc. Refer to the sample output below.

[root@localhost home]# useradd -D   # To check default settings of Users
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes

You can also the Default settings of the user in the /etc/defaults/useradd file. Refer to the Output below.

[root@localhost home]# cat /etc/default/useradd   # To check default settings of Users
# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes

Create a Customized New User

Now we have a task that we have to create a New Customized User. The Task is We have to create a user named “sudha” with the below settings.

  • The home directory of User should be /myusers/sudha
  • Set comment for a User is “finance user”
  • UID: 600
  • GID: 557
  • Add two Supplementary Groups.
  • Set Expiry Date of the User
  • and Set Password Inactivity for the User for 10 Days.

For all the above tasks refer to the command below.

[root@localhost ~]# useradd -d /myusers/sudha -c "finance user" -u 600 -g 557 -G workers,admins -e 2019-03-25 -f 10 sudha
[root@localhost ~]# passwd sudha   # Set Password for the User
Changing password for user sudha.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.

To confirm the Expiry Date refer to the below command.

[root@localhost ~]# chage -l sudha
Last password change                                    : May 20, 2017
Password expires                                        : never
Password inactive                                       : never
Account expires                                         : Mar 25, 2019
Minimum number of days between password change          : 0
Maximum number of days between password change          : 99999
Number of days of warning before password expires       : 7

To confirm the Password Inactivity Days refer to the below command.
The rest of the Settings you can confirm on /etc/passwd and /etc/shadow file.

[root@localhost ~]# cat /etc/shadow | grep sudha
sudha:$1$9ZyKQYwq$XFi4OciZPgnJmVpDxlqrR0:17306:0:99999:7:10:17980:

Also Read – Best Linux Usermod Command With Examples

Create a System User

To create a System User we can use useradd Linux command with argument -r. The difference between a Normal user and a System User is the User ID. As per default user settings the Normal User ID range starts from 500 to further and the System Users range starts from 1 to 500.

You can see below, our system user’s UID is 496 which is below 500.

[root@localhost ~]# useradd -r shrikant   # Create a System User
[root@localhost ~]# id shrikant
uid=496(shrikant) gid=493(shrikant) groups=493(shrikant)

For More Help related useradd Linux command refer to the command below.

[root@localhost user10]# useradd --help   # For more Help on useradd Linux command
Usage: useradd [options] LOGIN

Options:
  -b, --base-dir BASE_DIR       base directory for the home directory of the
                                new account
  -c, --comment COMMENT         GECOS field of the new account
  -d, --home-dir HOME_DIR       home directory of the new account
  -D, --defaults                print or change default useradd configuration
  -e, --expiredate EXPIRE_DATE  expiration date of the new account
  -f, --inactive INACTIVE       password inactivity period of the new account
  -g, --gid GROUP               name or ID of the primary group of the new
                                account
  -G, --groups GROUPS           list of supplementary groups of the new
                                account
  -h, --help                    display this help message and exit
  -k, --skel SKEL_DIR           use this alternative skeleton directory
  -K, --key KEY=VALUE           override /etc/login.defs defaults
  -l, --no-log-init             do not add the user to the lastlog and
                                faillog databases
  -m, --create-home             create the user's home directory
  -M, --no-create-home          do not create the user's home directory
  -N, --no-user-group           do not create a group with the same name as
                                the user
  -o, --non-unique              allow to create users with duplicate
                                (non-unique) UID
  -p, --password PASSWORD       encrypted password of the new account
  -r, --system                  create a system account
  -s, --shell SHELL             login shell of the new account
  -u, --uid UID                 user ID of the new account
  -U, --user-group              create a group with the same name as the user
  -Z, --selinux-user SEUSER     use a specific SEUSER for the SELinux user mapping

OR you can read the manual page of the useradd Linux command using the man command.

[root@localhost user10]# man useradd   # To read Manual Page of useradd Linux command.

Also Read – Managing Users And Groups In Linux – A Complete Guide For Beginners

That’s all, In this article, we have explained How to Create and Manage Users Using Useradd Linux Commands. I hope you enjoy this article. If you like this article, then just share it. If you have any questions about this article, please comment.

Share this:
WhatsApp Channel Join Now
Telegram Channel Join Now
Instagram Channel Join Now

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.