Linux Commands – useradd
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed groupmod command in Linux which is used to modify an existing group attribute.
https://cloudaffaire.com/linux-commands-groupmod/
In this blog post, we will discuss useradd command in Linux. useradd command is used to create a new user in Linux. When invoked without the -D option, the useradd command creates a new user account using the values specified on the command line plus the default values from the system. Depending on command line options, the useradd command will update system files and may also create the new user’s home directory and copy initial files. User name can be up to 32 characters long and you need root or sudo privileges to create a user.
User Configurations:
- CREATE_HOME (boolean): Indicate if a home directory should be created by default for new users. This setting does not apply to system users, and can be overridden on the command line.
- GID_MAX (number), GID_MIN (number): Range of group IDs used for the creation of regular groups by useradd, groupadd, or newusers. The default value for GID_MIN (resp. GID_MAX) is 1000 (resp. 60000).
- MAIL_DIR (string): The mail spool directory. This is needed to manipulate the mailbox when its corresponding user account is modified or deleted. If not specified, a compile-time default is used.
- MAIL_FILE (string): Defines the location of the users mail spool files relatively to their home directory. The MAIL_DIR and MAIL_FILE variables are used by useradd, usermod, and userdel to create, move, or delete the user’s mail spool. If MAIL_CHECK_ENAB is set to yes, they are also used to define the MAIL environment variable.
- MAX_MEMBERS_PER_GROUP (number): Maximum members per group entry. When the maximum is reached, a new group entry (line) is started in /etc/group (with the same name, same password, and same GID). The default value is 0, meaning that there are no limits in the number of members in a group.
- PASS_MAX_DAYS (number): The maximum number of days a password may be used. If the password is older than this, a password change will be forced. If not specified, -1 will be assumed (which disables the restriction).
- PASS_MIN_DAYS (number): The minimum number of days allowed between password changes. Any password changes attempted sooner than this will be rejected. If not specified, -1 will be assumed (which disables the restriction).
- PASS_WARN_AGE (number): The number of days warning given before a password expires. A zero means warning is given only upon the day of expiration, a negative value means no warning is given. If not specified, no warning will be provided.
- SYS_GID_MAX (number), SYS_GID_MIN (number): Range of group IDs used for the creation of system groups by useradd, groupadd, or newusers. The default value for SYS_GID_MIN (resp. SYS_GID_MAX) is 101 (resp. GID_MIN-1).
- SYS_UID_MAX (number), SYS_UID_MIN (number): Range of user IDs used for the creation of system users by useradd or newusers. The default value for SYS_UID_MIN (resp. SYS_UID_MAX) is 101 (resp. UID_MIN-1).
- UID_MAX (number), UID_MIN (number): Range of user IDs used for the creation of regular users by useradd or newusers. The default value for UID_MIN (resp. UID_MAX) is 1000 (resp. 60000).
- UMASK (number): The file mode creation mask is initialized to this value. If not specified, the mask will be initialized to 022.
- USERGROUPS_ENAB (boolean): Enable setting of the umask group bits to be the same as owner bits (examples: 022 -> 002, 077 -> 007) for non-root users, if the uid is the same as gid, and username is the same as the primary group name.
User Files:
- /etc/passwd: User account information.
- /etc/shadow: Secure user account information.
- /etc/group: Group account information.
- /etc/gshadow: Secure group account information.
- /etc/default/useradd: Default values for account creation.
- /etc/skel/: Directory containing default files.
- /etc/login.defs: Shadow password suite configuration.
Linux Commands – useradd:
You can use useradd command to create a new user.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
############################## ## Linux Commands | useradd ## ############################## ## Prerequisites: One Unix/Linux/POSIX-compliant operating system with bash shell ##-------- ## useradd ##-------- ## useradd [options] LOGIN ## useradd -D [options] sudo useradd myuser ## create a user ## User account information. sudo grep "myuser" /etc/passwd ## returns myuser:x:1001:1001::/home/myuser:/bin/bash ## Secure user account information. sudo grep "myuser" /etc/shadow ## returns myuser:!!:18408:0:99999:7::: ## Group account information. sudo grep "myuser" /etc/group ## returns myuser:x:1001: ## Secure group account information. sudo grep "myuser" /etc/gshadow ## returns myuser:!:: ## Default values for account creation. sudo cat /etc/default/useradd ## returns default values ## Directory containing default files. sudo ls /etc/skel/ ## returns default files if configured ## user configuration. sudo cat /etc/login.defs ## returns user and group config sudo userdel -rf myuser ## delete the user |
You can use useradd -p or –password PASSWORD option to set the user login credentials. The default is to disable the password.
Note: This option is not recommended because the password (or encrypted password) will be visible by users listing the processes. Instead set the user password using passwd command.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
## useradd -p or --password PASSWORD option options sudo useradd --password "mypwd" myuser ## create myuser with password mypwd sudo grep "myuser" /etc/shadow ## returns myuser:mypwd:18408:0:99999:7::: su - myuser ## try to login using myuser ## will not work as the password provided with ## --password needs to be encrypted, but we provided in plain ## text, you can use mkpasswd to get the encrypted version sudo passwd myuser ## enter a password as per your system password policy ## generally defined in /etc/pam.d/ su - myuser ## try to login using myuser new password pwd ## by default user home directory is created in /home/myuser exit ## exit myuser sudo userdel -rf myuser ## delete the user |
You can use useradd -d or –home-dir HOME_DIR options to create a custom home directory (HOME_DIR) for the user. By default, the user’s home directory is created as /home/user_name.
1 2 3 4 5 6 7 8 9 |
## useradd -d or --home-dir HOME_DIR options sudo useradd -d /tmp/myuser myuser ## create a user with custom home dir sudo passwd myuser ## set the password su - myuser ## login as myuser echo $HOME ## returns /tmp/myuser exit sudo userdel -rf myuser ## delete the user |
You can use useradd -b or –base-dir BASE_DIR options to create a custom base directory for the user if -d HOME_DIR is not specified. BASE_DIR is concatenated with the account name to define the home directory. The BASE_DIR must exist otherwise the home directory cannot be created. If this option is not specified, useradd will use the base directory specified by the HOME variable in /etc/default/useradd, or /home by default.
1 2 3 4 5 6 7 8 9 10 |
## useradd -b or --base-dir BASE_DIR options mkdir /tmp/mybasedir ## create a directory sudo useradd -b /tmp/mybasedir myuser ## create a user with custom base dir sudo passwd myuser ## set the password su - myuser ## login as myuser echo $HOME ## returns /tmp/mybasedir/myuser exit sudo userdel -rf myuser ## delete the user |
You can use useradd -c or –comment COMMENT options to add a short description of the user.
1 2 3 4 5 6 7 |
## useradd -c or --comment COMMENT options sudo useradd -c "cloudaffaire" myuser ## create a user with short description sudo grep "myuser" /etc/passwd ## returns myuser:x:1001:1001:cloudaffaire:/home/myuser:/bin/bash sudo userdel -rf myuser ## delete the user |
You can use useradd -e or –expiredate EXPIRE_DATE options to define the date on which the user account will be disabled. The date is specified in the format YYYY-MM-DD. If not specified, useradd will use the default expiry date specified by the EXPIRE variable in /etc/default/useradd, or an empty string (no expiry) by default.
1 2 3 4 5 6 7 |
## useradd -e or --expiredate EXPIRE_DATE options sudo useradd -e 2020-12-31 myuser ## create a user with expiry date sudo chage -l myuser ## returns Account expires : Dec 31, 2020 sudo userdel -rf myuser ## delete the user |
You can use useradd -f or –inactive INACTIVE options to define the number of days after a password expires until the account is permanently disabled. A value of 0 disables the account as soon as the password has expired, and a value of -1 disables the feature. If not specified, useradd will use the default inactivity period specified by the INACTIVE variable in /etc/default/useradd, or -1 by default.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
## useradd -f or --inactive INACTIVE options sudo useradd --inactive 30 myuser ## create a user with password inactive days sudo passwd myuser ## set user password sudo passwd -x 90 myuser ## set password expiry date sudo chage -l myuser ## returns ## Password expires : Aug 25, 2020 ## Password inactive : Sep 24, 2020 sudo userdel -rf myuser ## delete the user |
You can use useradd -s or –shell SHELL options to define the name of the user’s login shell. The default is to leave this field blank, which causes the system to select the default login shell specified by the SHELL variable in /etc/default/useradd, or an empty string by default.
1 2 3 4 5 6 7 8 9 |
## useradd -s or --shell SHELL options sudo grep "SHELL" /etc/default/useradd ## returns SHELL=/bin/bash sudo useradd -s /bin/sh myuser ## make /bin/sh as default shell sudo grep "myuser" /etc/passwd ## myuser:x:1001:1001::/home/myuser:/bin/sh sudo userdel -rf myuser ## delete the user |
You can use useradd -r or –system options to create a system account. There is no difference between a system user and a normal user, only system users will be created with no aging information in /etc/shadow, and their numeric identifiers are chosen in the SYS_UID_MIN-SYS_UID_MAX range, defined in /etc/login.defs, instead of UID_MIN-UID_MAX (and their GID counterparts for the creation of groups). Also, for system user useradd will not create a home directory, regardless of the default setting in /etc/login.defs (CREATE_HOME). You have to specify the -m options if you want a home directory for a system account to be created.
1 2 3 4 5 6 7 8 9 10 11 |
## useradd -r or --system options sudo useradd -r myuser ## create a system user sudo grep "myuser" /etc/passwd ## myuser:x:996:994::/home/myuser:/bin/bash sudo ls /home/myuser ## ls: cannot access /home/myuser: No such file or directory sudo grep "myuser" /etc/shadow ## myuser:!!:18409:::::: sudo userdel -rf myuser ## delete the user |
You can use useradd -m or –create-home options to create the user’s home directory if it does not exist. The files and directories contained in the skeleton directory (which can be defined with the -k option) will be copied to the home directory. By default, if this option is not specified and CREATE_HOME is not enabled, no home directories are created.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
## useradd -m or --create-home options sudo useradd -r myuser ## create a system user (home dir not created by deafult) sudo ls -l /home | grep myuser ## home directory is not created sudo userdel -rf myuser ## delete the user sudo useradd -m -r myuser ## create a system user (with -m, home dir is created) sudo ls -l /home | grep myuser ## drwx------ 2 myuser myuser 62 May 27 06:09 myuser sudo userdel -rf myuser ## delete the user |
You can use useradd -k or –skel SKEL_DIR options to copy the content of skeleton directory to user’s home directory. This option is only valid if the -m (or –create-home) option is specified. If this option is not set, the skeleton directory is defined by the SKEL variable in /etc/default/useradd or, by default, /etc/skel.
1 2 3 4 5 6 7 8 9 10 11 |
## useradd -k or --skel SKEL_DIR options sudo grep SKEL /etc/default/useradd ## SKEL=/etc/skel sudo mkdir -p /tmp/myskel/myfile ## create a directory sudo useradd -m -k /tmp/myskel myuser ## create the user sudo ls -l /home/myuser ## drwxr-xr-x 2 myuser myuser 6 May 27 06:42 myfile sudo userdel -rf myuser ## delete the user |
You can use useradd -M or –no-create-home options to disable home directory creation even if the system wide setting from /etc/login.defs (CREATE_HOME) is set to yes.
1 2 3 4 5 6 7 8 9 10 11 |
## useradd -M or --no-create-home options sudo grep CREATE_HOME /etc/login.defs ## CREATE_HOME yes sudo useradd myuser ## create a user sudo ls -l /home | grep myuser ## drwx------ 2 myuser myuser 62 May 27 06:09 myuser sudo userdel -rf myuser ## delete the user sudo useradd -M myuser ## create a user sudo ls -l /home | grep myuser ## home directory not created sudo userdel -rf myuser ## delete the user |
You can use useradd -u or –uid UID options to provide custom user id. This value must be unique and non-negative, unless the -o option is used. The default is to use the smallest ID value greater than or equal to UID_MIN and greater than every other user.
1 2 3 4 5 6 7 8 9 10 11 |
## useradd -u or --uid UID options sudo grep "UID*" /etc/login.defs ## get UID config sudo useradd myuser ## create a user sudo grep "myuser" /etc/passwd ## myuser:x:1001:1001::/home/myuser:/bin/bash sudo userdel -rf myuser ## delete the user sudo useradd -u 2001 myuser ## create a user sudo grep "myuser" /etc/passwd ## myuser:x:2001:2001::/home/myuser:/bin/bash sudo userdel -rf myuser ## delete the user |
You can use useradd -o or –non-unique options to allow the creation of a user account with a duplicate (non-unique) UID. This option is only valid in combination with the -u option.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
## useradd -o or --non-unique options sudo useradd -u 2001 myuser1 ## create myuser1 with UID 2001 sudo useradd -u 2001 myuser2 ## try to create myuser2 with UID 2001 ## useradd: UID 2001 is not unique sudo useradd -o -u 2001 myuser2 ## now try with -o option sudo grep "myuser*" /etc/passwd ## returns ## myuser1:x:2001:2001::/home/myuser1:/bin/bash ## myuser2:x:2001:2002::/home/myuser2:/bin/bash sudo userdel -rf myuser1 ## delete the users sudo userdel -rf myuser2 |
You can use useradd -K or –key KEY=VALUE options to overrides /etc/login.defs defaults (UID_MIN, UID_MAX, UMASK, PASS_MAX_DAYS and others).
1 2 3 4 5 6 7 8 9 10 11 12 |
## useradd -K or --key KEY=VALUE options sudo cat /etc/login.defs sudo useradd -r myuser ## create a system user sudo grep "myuser" /etc/passwd ## myuser:x:996:994::/home/myuser:/bin/bash sudo userdel -rf myuser ## delete the user ## create a system user with -K option sudo useradd -r -K SYS_UID_MIN=2000 -K SYS_UID_MAX=4000 myuser sudo grep "myuser" /etc/passwd ## myuser:x:4000:994::/home/myuser:/bin/bash sudo userdel -rf myuser ## delete the user |
You can use useradd -U or –user-group options to create a group with the same name as the user, and add the user to this group. The default behavior (if the -g, -N, and -U options are not specified) is defined by the USERGROUPS_ENAB variable in /etc/login.defs.
1 2 3 4 5 6 7 8 9 10 11 |
## useradd -U or --user-group options sudo grep USERGROUPS_ENAB /etc/login.defs ## returns USERGROUPS_ENAB yes sudo useradd -U myuser ## create a user with group name same as user name sudo grep "myuser" /etc/group ## myuser:x:1001: id -g -n myuser ## myuser is the primary group of myuser sudo userdel -rf myuser ## delete the user |
You can use useradd -g or –gid GROUP options to add the user to an existing group. -g options accept both group name or number of the user’s initial login group. If not specified, the behavior of useradd will depend on the USERGROUPS_ENAB variable in /etc/login.defs. If this variable is set to yes (or -U/–user-group is specified on the command line), a group will be created for the user, with the same name as her loginname. If the variable is set to no (or -N/–no-user-group is specified on the command line), useradd will set the primary group of the new user to the value specified by the GROUP variable in /etc/default/useradd, or 100 by default.
1 2 3 4 5 6 7 8 9 10 |
## useradd -g or --gid GROUP options sudo groupadd mygroup ## create a new group sudo grep "mygroup" /etc/group ## returns mygroup:x:1001: sudo useradd -g mygroup myuser ## create a user and add to a group id -g -n myuser ## mygroup is the primary group of myuser sudo userdel -rf myuser ## delete the user sudo groupdel mygroup ## delete the group mygroup |
You can use useradd -G or –groups GROUP1[,GROUP2,…[,GROUPN]]] options to add the user to multiple secondary group during user creation. All the groups must pre-exist and needs to be separated from the next by a comma, with no intervening whitespace.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
## useradd -G or --groups GROUP1[,GROUP2,...[,GROUPN]]] options sudo groupadd mygroup1 ## create some groups sudo groupadd mygroup2 sudo groupadd mygroup3 ## create user, adding it to multiple groups sudo useradd -g mygroup1 -G mygroup2,mygroup3 myuser id -g -n myuser ## Primary group: mygroup1 id -G -n myuser ## returns mygroup1 mygroup2 mygroup3 sudo userdel -rf myuser ## delete the user sudo groupdel mygroup1 ## delete the groups sudo groupdel mygroup2 sudo groupdel mygroup3 |
You can use useradd -N or –no-user-group options to not create a group with the same name as the user, but add the user to the group specified by the -g option or by the GROUP variable in /etc/default/useradd. The default behavior (if the -g, -N, and -U options are not specified) is defined by the USERGROUPS_ENAB variable in /etc/login.defs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
## useradd -N or --no-user-group options sudo groupadd mygroup ## create a group sudo useradd -G mygroup myuser ## create a user id -g -n myuser ## returns myuser id -G -n myuser ## returns myuser mygroup sudo userdel -rf myuser ## delete the user sudo useradd -N -G mygroup myuser ## create a user with -N option id -g -n myuser ## returns users id -G -n myuser ## returns users mygroup sudo userdel -rf myuser ## delete the user sudo groupdel mygroup ## delete the group |
You can use useradd -D or –defaults options to update the default user configurations defined in /etc/default/useradd file. You can change below default configurations using -D option
- -b, –base-dir BASE_DIR: The path prefix for a new user’s home directory.
- -e, –expiredate EXPIRE_DATE: The date on which the user account is disabled.
- -f, –inactive INACTIVE: The number of days after a password has expired before the account will be disabled.
- -g, –gid GROUP: The group name or ID for a new user’s initial group.
- -s, –shell SHELL: The name of a new user’s login shell.
1 2 3 4 5 6 7 8 9 |
## useradd -D or --defaults options sudo useradd -D ## get current default configs sudo useradd -D -s /bin/sh ## change SHELL from /bin/bash to /bin/sh sudo useradd -D ## get current default configs sudo useradd -D -s /bin/bash ## change back SHELL from /bin/sh to /bin/bash |
Hope you have enjoyed this article. In the next blog post, we will discuss passwd command in Linux.