This article will help you to understand the 'useradd' command in Linux/Unix based systems. As we all are aware that Linux is a multi-user and a multitasking operating system (OS). It has been built with a large number of commands from basic to advance. Here, we are going to see the 'useradd' command, it is also called 'adduser'. In RHEL/CENTOS both (useradd and adduser) commands do the same functions/features, and there is no key difference between them, whereas it may not be true with other distros. The path of the 'adduser' is just a symbolic link to the 'useradd' command.
Useradd command is to create and manage users in Linux/Unix based systems. It comes with a lot of options/arguments. Being a Linux system administrator, it is one of your routine jobs to create users with a normal/default setup, or sometimes you may ask to create a specific home directory of the users, or set user account expiry date, or shell, or group, etc, etc.
This step by step guide will help you on how to use the various options in the 'useradd' commands on RHEL/CentOS 7.6. We will see one by one mostly with examples. The same guide can be used for all the other versions of RHEL/CentOS/Fedora. For other distros, it may come with a small type of difference in using it. Therefore, it is highly recommended to read the guidelines before creating user accounts.
Global Syntax for useradd command with options:
useradd [options] USERNAME
The following cheat sheet provides an overview of the options used in the useradd command in Linux.
Let us begin by executing the default form of useradd command followed by the user name. Once this command is executed, it will perform the following actions:
(a) A new entry will be added to '/etc/passwd, /etc/shadow, /etc/group, /etc/gshadow' files.
(b) A new user's home directory will be created along with the bash files, which are copied from the skel directory. Also, permission and ownership of the home directory will be set by default.
Warning:
1. How to add/create a user ?
# useradd linuxteck
Note:
(a) /etc/passwd => This file is used to store all the information of the user accounts. You can verify the information of the newly created username in the '/etc/passwd' file with the following command:
# cat /etc/passwd | grep linuxteck
Output:
linuxteck:x:1003:1005::/home/linuxteck:/bin/bash
Note:
(b) /etc/shadow => This file is used to store all the password information of the user. Only root account/equivalent privileges accounts can handle this file. By default, the root account holds the permission and ownership of this file. Use the following command to view the password related information of the new user named "linuxteck".
# cat /etc/shadow | grep linuxteck
Output:
linuxteck:$6$PsZ2cWaJ$yUscpxrjnDkq4LgwvRNLMBK6GqXkWavI4Czf44v6NVl4NzH.czTTxw.IHqvohU5Y1LQrTjPYe/MRd8w8ndAbe0:18333:0:99999:7:::
Note:
(c) /etc/group => This file is used to store a list of groups and the members belong to each group. Basically, in Linux/Unix, multiple users can be placed into single or multiple groups. To see the information in the '/etc/group' file you can use the following command:
# cat /etc/group | grep linuxteck
(d) /etc/gshadow => This file is used to store an encrypted password for each group and group membership. Generally, it can be readable only by root or super users and the default permission of this file is set as "000".
# cat /etc/gshadow | grep linuxteck
Output:
linuxteck : ! : :
Note:
2. How to define a home directory during user creation in Linux?
#useradd -b /opt linuxteck
Note:
# cat /etc/passwd | grep linuxteck
Output:
linuxteck:x:1003:1005::/opt/linuxteck:/bin/bash
3. How to create a "Custom Comments" while creating the user?
# useradd -c "John Gomez" john
Note:
# cat /etc/passwd | grep john
Output:
john:x:1003:1004:John Gomez:/home/john:/bin/bash
4. How to create a specific home directory name when creating a new user?
# useradd -d /opt/oracle jose
Note:
# cat /etc/passwd | grep jose
Output:
jose:x:1004:1005::/opt/oracle:/bin/bash
5. How to check the user's default configuration?
# useradd -D OR # cat /etc/default/useradd
Output:
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
Note:
6. How to create a user account with an expiry date?
# useradd -e 2020-03-22 linuxteck
Note:
Before setting up the User Expiry option:
# chage -l linuxteck
After setting the User Expiry option:
# chage -l linuxteck
7. How to create a password expiry date for a User account?
# useradd -f 2 linuxteck
Note:
# cat /etc/shadow | grep linuxteck
Output:linuxteck:!!:18343:0:99999:7:2::
OR
# passwd -S linuxteck
Output:
linuxteck LK 2020-03-22 0 99999 7 2 (Password locked.)
Note:
# passwd -S jose
Output:
jose LK 2020-03-20 0 99999 7 -1 (Password locked.)
8. How to create a User with a particular Group ID?
# useradd -g 1001 linuxteck
Note:
# cat /etc/passwd | grep linuxteck
Output:
linuxteck:x:1005:1001::/home/linuxteck:/bin/bash
Note:
# tail -10 /etc/group
Output:
unbound:x:982:
mysql:x:1001:
openvpn:x:981:
lightdm:x:980:
nm-openvpn:x:979:
nm-openconnect:x:978:
yumex:x:977:
teck:x:1003:
john:x:1004:
jose:x:1005:
Note:
Command 3:
# id linuxteck
Output:
uid=1005(linuxteck) gid=1001(mysql) groups=1001(mysql)
9. How to add a user to multiple groups?
# useradd -G svn,git,database linuxteck
Note:
# id linuxteck
Output:
uid=1005(linuxteck) gid=1009(linuxteck)
groups=1009(linuxteck),1006(svn),1007(git),1008(database)
10. How to create a User without a Home Directory?
# useradd -M jose
Note:
Command: 1
# id jose
Output:
uid=1006(jose) gid=1010(jose) groups=1010(jose)
Command: 2
# ls -ld /home/jose
Output:
ls: cannot access /home/jose: No such file or directory
Note:
11. How to create a User without a User Private Group (UPG)?
# useradd -N tony
Note:
Command :1
# id tony
Output:
uid=1009(tony) gid=100(users) groups=100(users)
Command :2
# tail -1 /etc/passwd
Output:
tony:x:1009:100::/home/tony:/bin/bash
Note:
12. How to create a user with an encrypted password?
# useradd -p '$6$Zg8Mxq7u$WzUhqPrmus21PNlFRXm7RO4peDCLionkkI.' linuxteck
Note:
# tail -1 /etc/shadow
Output:
linuxteck:$6$Zg8Mxq7u$WzsxJUbF0wsbPNlFRXm7RO4peDCLionkkI.:18343:0:99999:7:::
13. How to add a system user account to Linux?
# useradd -r www-data
Note:
# id www-data
Output:
uid=982(www-data) gid=976(www-data) groups=976(www-data)
Note:
14. How to add/create a user with a custom shell type?
# useradd -s /bin/sh john
Note:
# cat /etc/passwd | grep john
Output:
john:x:1011:1012::/home/john:/bin/sh
15. How to create a User with a particular User ID (UID)?
# useradd -u 1050 gomez
Note:
# tail -1 /etc/passwd
Output:
gomez:x:1050:1050::/home/gomez:/bin/bash
' useradd' command with advanced option:
16. How to create a new user using the same home directory as another user?
# useradd -m -k /opt/oracle mable
Note:
17. How to create a user with a custom UID/GID, custom comments, specific home directory, and custom shell?
# useradd -u 1500 -g 1200 -c "Simon George" -d /opt/oracle -s /bin/sh simon
Note:
# tail -1 /etc/passwd
Output:
simon:x:1999:1200:Simon George:/opt/oracle:/bin/sh
18. How to create a system user without a group or home directory?
# useradd -M -N -r www-data
Note:
# cat /etc/passwd | grep www-data
# cat /etc/group
# id www-data
19. How to create a completely customized user with the following options:
(i) The UID : 1500
(ii) The GID : 1200
(iii) Comment Field : Users Full Name
(iv) The home directory : /opt/oracle
(v) Supplementary Goups: svn,git
(vi) Set User Expriy : User expiry as 2020-03-25
(vi) Set Password : User's password inactivity for 2 days
# useradd -d /opt/oracle -c "Joseph Alex" -u 1050 -g 1200 -G svn,git -e 2020-03-25 -f 10 joseph
Note:
(a) For General: # cat /etc/passwd | grep joseph
(b) For User expriy date : # chage -l joseph
(c) For password expiry : # cat /etc/shadow| grep joseph OR passwd -S joseph
20. How to create a user without a home directory, no shell, no group or custom comment?
# useradd -M -N -s /bin/false -c "Linux Teck" linuxteck
Note:
# ls -l /home/
OR
# tail -1 /etc/passwd
Output:
linuxteck:x:2000:100:Linux Teck:/home/linuxteck:/bin/false
# man useradd OR # useradd -h
Help:
I hope this article will help you to understand a few things about the 'useradd' commands in Linux. Drop me your feedback/comments. If you like this article, kindly share it and it may help others as well.
Thank you!