CREATE & MANAGE USERS USING USERADD LINUX COMMAND
Introduction
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.
Follow the below useradd linux commands with Examples :
Create a New User
Create a new user using useradd Linux command. Refer the command below.
[root@localhost ~]# useradd elinuxbook # Create a New User
Set Password for the New User.
[root@localhost ~]# passwd elinuxbook # Set Password for the New User
Changing password for user elinuxbook.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
Set User ID (UID) for New User
You can set User ID of your own choice by using useradd Linux command with argument -u.
Note : The User ID should be Greater then OR equal to 500 and Should not already 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 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 user to Multiple Supplementary groups separate groups by 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 any thing like Address of the User, Telephone Number or may be 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
Also Read :
- Best chattr command to change File Attributes – Making Important Files Immutable
- BEST LINUX LS COMMAND WITH EXAMPLES
- BEST LINUX ALIAS COMMAND WITH EXAMPLES
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 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 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 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 same Name as User
Normally when we create a New User system automatically creates a group as same name as 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 home directory the you can use useradd linux command with argument -M.
[root@localhost ~]# useradd -M user17 # Create a new user without home directory
Assign Same UID for Multiple New Users
We can assign same UID to Multiple users. To do so you can use useradd Linux command with argument -o. Refer 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 refer 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 user will allow to login the system using his Username even after Password expiration for 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 login the system using his Username for 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 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 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 user in /etc/defaults/useradd file. Refer 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 below settings.
- 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 above task refer 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 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 the below command.
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:
Create a System User
To create a System User we can use useradd Linux command with argument -r. The difference between Normal user and 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 till 500.
You can see below, our system users 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 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 manual page of the useradd linux command using man command.
[root@localhost user10]# man useradd # To read Manual Page of useradd Linux command.
If you found this article useful then Like Us, Share Us, Subscribe our Newsletter OR if you have something to say then feel free to comment on the comment box below.