kazmax - Home Server on Linux

Linux Create a new user - useradd

To create a new user on Linux, use the command "useradd". When executing the "useradd" command, you can also specify the new user's home directory, group, and so on.

Although you can easily add users with the "useradd user name" command, we recommend that you check the default information of new users beforehand.

Also, create a new user as "root" user.

Last Update : December 11, 2018

Linux Create a new user - useradd Contents

  1. How to check the default setting of "useradd"
  2. How to change the default setting of "useradd"
  3. Add a new user with "useradd" command
  4. About other options of "useradd" command

1. How to check the default setting of "useradd"

By specifying the "-D" option to the useradd command, you can check the default setting of the user to be created.

[root@localhost ~]# useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel

The meaning of each item is as follows.

GROUP Set the group ID to 100. For Red Hat based distributions, since GID_MIN is set to 500 in /etc/login.defs, group IDs are given in order from 500.
HOME The default home directory location. In this case, the home directory is created with "/home/user_name".
INACTIVE The number of days after a password has expired before the account will be disabled.
"-1" means no time limit.
EXPIRE The date on which the user account is disabled. If there is no value it means that there is no time limit.
SHELL Default login shell name.
SKEL Template for the new user's home directory.
In this case, a copy of /etc/skel is created in the new user's home directory.

Notes

You can also check it in /etc/default/useradd file.

[root@localhost ~]# cat /etc/default/useradd
# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel

2. How to change the default setting of "useradd"

It is possible to change the default value according to the value given to the option with "-D + option" to the useradd command.

-b default_home Path to new user's home directory. Default_home followed by a user name is used as the new directory name.
-e default_expire_date The date on which the user account is disabled. The date is specified in YYYY-MM-DD format.
-f default_inactive The number of days after a password has expired before the account will be disabled.
"-1" means no time limit.
-g default_group Group name or group ID of the primary group to which the new user belongs. The group name must already exist. The group ID must correspond to a group that already exists.
-s default_shell The login shell of the new user.

Example: Set the home directory to "/home/users" and disable the account in 60 days after the user's password is invalid.

[root@localhost ~]# useradd -D -b /home/users -f 60
[root@localhost ~]# useradd -D ← Check
GROUP=100
HOME=/home/users ← has been changed
INACTIVE=60 ← has been changed
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel

Notes

Changing the default setting value is possible even if you change the /etc/default/useradd file directly.

3. Add a new user with "useradd" command

You can create a new user by specifying the user name you want to add to the useradd command. If you do not specify an option, the default setting user and home directory will be created.

Create a new user named "foo".

[root@localhost ~]# useradd -D  ← Confirm initial setting
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
[root@localhost ~]# useradd foo ← Create a user
[root@localhost ~]# id -a foo ← Confirm user
uid=514(foo) gid=514(foo) groups=514(hogehoge)
[root@localhost ~]# ls -ld /home/foo/  ← Confirm home directory
drwx------  4 foo foo 4096 Dec  2 20:20 /home/foo/

4. About other options of "useradd" command

If you want to create a user that is not the default setting, specify the option and create the user.

Example: Create user "foo" belonging to user ID 600, primary group "users", subgroup sub1, and sub2.

[root@localhost ~]# useradd -u 600 -g users -G sub1,sub2 foo  ← Create a user
[root@localhost ~]# id -a foo 
uid=600(hoge) gid=501(users) groups=501(users),503(sub1),514(sub2)

The option details below.

-M Disable home directory.
-c comment Any text string.
-d home_dir Specify home directory.
-e expire_date The date on which the user account is disabled. The date is specified in YYYY-MM-DD format.
-f inactive_days After the expiration date of the password has expired, the account will be permanently unusable after the number of days given by this option expires. If you specify 0 as the value, the account will be unusable immediately after the password expires. If -1 is specified, this function is invalid. The default value is -1.
-g initial_group Group name or group ID of the primary group to which the user belongs.
-G group,[...] Group name or group ID of the subgroup to which the user belongs. Groups must be separated by commas and should not contain spaces.
-s shell The login shell name of the user.
-u uid User ID
-o An overlap of UID is permitted.

Notes

By using the -o option, it is possible to add accounts with duplicate UIDs.

This allows you to grant the same access rights to accounts with the same UID even if the user name is different.