15+ switches of useradd command with example – Unix/Linux

The ‘useradd’ command is used to create a new user or update default new user information under your system/server.

# useradd [options] NAME

Switches of ‘useradd’ command:

1, -b, –base-dir BASE_DIR

The default base directory for the system if -d HOME_DIR is not specified. 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.

[[email protected] ~]# useradd -b /home/hard/ me
[[email protected] ~]# cd ~me
[[email protected] me]# pwd

2, -c, –comment COMMENT

Any text string. It is generally a short description of the login, and is currently used as the field for the user´s full name.

[[email protected] me]# useradd -c "test account" me
[[email protected] me]# grep -w  me /etc/passwd
me:x:502:502:test account:/home/me:/bin/bash

3, -d, –home HOME_DIR

The new user will be created using HOME_DIR as the value for the user´s login directory. The default is to append the LOGIN name to BASE_DIR and use hat as the login directory name. The directory HOME_DIR does not have to exist but will not be created if it is missing.

[[email protected] home]# useradd -d /home/test/  me
[[email protected] home]# grep -w me /etc/passwd

4, -e, –expiredate EXPIRE_DATE

The date on which the user account will be disabled. The date is specified in the format YYYY-MM-DD.

5, -f, –inactive INACTIVE

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 hasexpired, 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.

6, -g, –gid GROUP

The group name or number of the user´s initial login group. The group name must exist. A group number must refer to an already existing group.

[[email protected] ~]# useradd -g 0 me
[[email protected] ~]# grep -w me /etc/passwd

7, -G, –groups GROUP1[,GROUP2,…[,GROUPN]]]

A list of supplementary groups which the user is also a member of. Each group is separated from the next by a comma, with no intervening whitespace. The groups are subject to the same restrictions as the group given with the -g option. The default is for the user to belong only to the initial group.

[[email protected] ~]# useradd -G root,crybit  me
[[email protected] ~]# groupmems -g crybit -l

Click here for “groupmems command details”

8, -h, –help

Display help message and exit.

9, -l, –no-log-init
Do not add the user to the lastlog and faillog databases.By default, the user´s entries in the lastlog and faillog databases are resetted to avoid reusing the entry from a previously deleted user.

10, -M

Do not create the user´s home directory, even if the system wide setting from /etc/login.defs (CREATE_HOME) is set to yes.

11, -N, –no-user-group

Do 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.

[[email protected] ~]# useradd -N -G crybit me
[[email protected] ~]# grep -w me /etc/passwd

12, -o, –non-unique

Allow the creation of a user account with a duplicate (non-unique) UID.
This option is only valid in combination with the -o option.
Note that, useradd: -o flag is only allowed with the -u flag

[[email protected] ~]# useradd -o -u 0  me
[[email protected] ~]# grep -w me /etc/passwd

Now I created the user me with root privilage UID=0 🙂

12, -p, –password PASSWORD

The encrypted password, as returned by crypt(3). 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. You should make sure the password respects the system´s password policy.

13, -s, –shell SHELL

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.

[[email protected] ~]# useradd -s /bin me
[[email protected] ~]# grep -w me /etc/passwd


[[email protected] ~]# useradd -s /bin/bash  me1
[[email protected] ~]# grep -w me1 /etc/passwd

14, -u, –uid UID

The numerical value of the user´s ID. This value must be unique, unless the -o option is used. The value must be non-negative. The default is to use the smallest ID value greater than 999 and greater than every other user. Values between 0 and 999 are typically reserved for system accounts.

[[email protected] ~]# useradd -u 1000 me
[[email protected] ~]# grep -w me /etc/passwd

15, -U, –user-group

Create a group with the same name as the user, and add the user to this group.

16, -Z, –selinux-user SEUSER

The SELinux user for the user´s login. The default is to leave this field blank, which causes the system to select the default SELinux user.

Thank you 🙂

Related Links:
groupdel, groupmems, groupmod, usermod

ls, head, tail, top, ps, find, crontab

Post navigation

Arunlal Ashok

SRE at NewFold Digital. Linux lover. Traveller. Father of two princesses.

Always happy for an open discussion! Write to arun ((@)) crybit ((dot)) com.

2 thoughts on “15+ switches of useradd command with example – Unix/Linux

Leave a Reply

Your email address will not be published. Required fields are marked *