The ‘useradd’ command is used to create a new user or update default new user information under your system/server.
Syntax:
# 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.
Example:
[root@localhost ~]# useradd -b /home/hard/ me
[root@localhost ~]# cd ~me
[root@localhost me]# pwd
/home/hard/me
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.
Example:
[root@localhost me]# useradd -c "test account" me
[root@localhost 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.
Example:
[root@localhost home]# useradd -d /home/test/ me
[root@localhost home]# grep -w me /etc/passwd
me:x:502:502::/home/test/:/bin/bash
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.
Example:
[root@localhost ~]# useradd -g 0 me
[root@localhost ~]# grep -w me /etc/passwd
me:x:502:0::/home/me:/bin/bash
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.
Example:
[root@localhost ~]# useradd -G root,crybit me
[root@localhost ~]# groupmems -g crybit -l
me
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.
Example:
[root@localhost ~]# useradd -N -G crybit me
[root@localhost ~]# grep -w me /etc/passwd
me:x:502:100::/home/me:/bin/bash
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.
Example:
Note that, useradd: -o flag is only allowed with the -u flag
[root@localhost ~]# useradd -o -u 0 me
[root@localhost ~]# grep -w me /etc/passwd
me:x:0:502::/home/me:/bin/bash
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.
Example:
I.
[root@localhost ~]# useradd -s /bin me
[root@localhost ~]# grep -w me /etc/passwd
me:x:502:502::/home/me:/bin
II.
[root@localhost ~]# useradd -s /bin/bash me1
[root@localhost ~]# grep -w me1 /etc/passwd
me1:x:503:503::/home/me1:/bin/bash
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.
Example:
[root@localhost ~]# useradd -u 1000 me
[root@localhost ~]# grep -w me /etc/passwd
me:x:1000:1000::/home/me:/bin/bash
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 🙂
2 thoughts on “15+ switches of useradd command with example – Unix/Linux”