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

[[email protected] ~]# useradd -b /home/hard/ me
[[email protected] ~]# cd ~me
[[email protected] 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:

[[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.
Example:

[[email protected] home]# useradd -d /home/test/  me
[[email protected] 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:

[[email protected] ~]# useradd -g 0 me
[[email protected] ~]# 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:

[[email protected] ~]# useradd -G root,crybit  me
[[email protected] ~]# 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:

[[email protected] ~]# useradd -N -G crybit me
[[email protected] ~]# 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

[[email protected] ~]# useradd -o -u 0  me
[[email protected] ~]# 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.

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

II.

[[email protected] ~]# useradd -s /bin/bash  me1
[[email protected] ~]# 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:

[[email protected] ~]# useradd -u 1000 me
[[email protected] ~]# 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 🙂

Related Links:
groupdel, groupmems, groupmod, usermod

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

Arunlal Ashok

Linux Server Administrator. I'm dealing with Linux servers since 2012. I started this blog to share and discuss my ideas with the world. Check My Profile!! in uPwork (oDesk) and let me know if you need any assistance. Thanks!!

You may also like...

1 Response

  1. February 7, 2014

    […] useful commands: groupdel, groupmems, groupmod, useradd , usermod , chgrp, chown, ls, head, tail, top, ps, find, crontab, ftp commands, […]

Leave a Reply

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