5 UNIX / Linux useradd Command Examples

by Himanshu

What is useradd?
5 useradd examples
Syntax and Options
Related Commands

What is useradd?

The Linux useradd command is used to create new users. This command is used to update default new user information.

5 useradd Examples

1. Add a user through useradd command

The most basic usage of this command is to add a user account. This can be done by just supplying the user name along with this command.

Consider the following example :

$ sudo useradd guest
$

The above command adds a user named guest. Lets confirm it :

 $ sudo cat /etc/passwd | grep "/home"
syslog:x:101:103::/home/syslog:/bin/false
usbmux:x:107:46:usbmux daemon,,,:/home/usbmux:/bin/false
saned:x:112:118::/home/saned:/bin/false
himanshu:x:1000:1001:Himanshu,,,:/home/himanshu:/bin/bash
guest:x:1001:1003::/home/guest:/bin/sh

So the above output (highlighted in bold) confirms that a user account with name ‘guest’ was created.

2. See all the default values associated with a user account using -D option

When a user account is created, some extra information is associated with account by default. To view these default values, use the -D option along with this command.

Consider the example below :

$ useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/sh
SKEL=/etc/skel
CREATE_MAIL_SPOOL=no

So we see that all the default values were produced in the output.

3. Change default values produced by -D option

All the default values (as shown in the example above) can be changed by using various options. Here, lets try to change the SHELL. This value can be changed using -s option.

Consider the following example :

$ sudo useradd -D -s/bin/ksh
$

Now lets check whether the change was actually done or not.

$ sudo cat /etc/default/useradd
# Default values for useradd(8)
#
# The SHELL variable specifies the default login shell on your
# system.
# Similar to DHSELL in adduser. However, we use "sh" here because
# useradd is a low level utility and should be as general
# as possible
SHELL=/bin/ksh
...
...
...

The line in bold above confirms that the change actually took place. Similarly other default values can be changed.

4. Directly supply the encrypted password using -p option

As we know that whatever password we supply for a user account, it is encrypted and then stored. So through this option we can directly supply an encrypted password. The encrypted password is obtained from the crypt command. But, usage of this option is not recommended. From the man page :

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 systems password policy.

5. Create a group with same name as the user

This can be achieved by using -U option. The group will be created and the existing user will be added to it.

Consider the following example :

$ sudo useradd -U guest
$

The above command will create a user guest and will add it to a group with same name.

Syntax and Options

useradd [options] LOGIN

useradd -D

useradd -D [options]
Short Option Long Option Option Description
-b –base-dir BASE_DIR The default base directory for the system if -d HOME_DIR is not specified. BASE_DIR is concatenated with the account name to define the home directory. If the -m option is not used, BASE_DIR must exist. 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.
-c –comment COMMENT Any text string. It is generally a short description of the login, and is currently used as the field for the users full name.
-c –changes like verbose but report only when a change is made
-d –home HOME_DIR The new user will be created using HOME_DIR as the value for the users login directory. The default is to append the LOGIN name to BASE_DIR and use that as the login directory name. The directory HOME_DIR does not have to exist but will not be created if it is missing.
-D –defaults See below, the subsection “Changing the default values”.
-e –expiredate EXPIRE_DATE 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.
-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 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.
-g –gid GROUP The group name or number of the users initial login group. The group name must exist. A group number must refer to an already existing group. If not specified, the bahavior 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.

Related Commands

chfn
chsh
passwd
crypt
groupadd
groupdel
groupmod
userdel
usermod

Comments on this entry are closed.

Previous post:

Next post: