Linux Useradd
Linux Useradd
Linux Useradd
useradd Command
The general syntax for the useradd command is as follows:
When invoked, useradd creates a new user account according to the options specified on
the command line and the default values set in the /etc/default/useradd file.
The variables defined in this file differ from distribution to distribution, which causes the
useradd command to produce different results on different systems.
useradd also reads the content of the /etc/login.defs file. This file contains configuration for
the shadow password suite such as password expiration policy, ranges of user IDs used
when creating system and regular users, and more.
For example to create a new user named username you would run:
To be able to log in as the newly created user, you need to set the user password. To do that
run the passwd command followed by the username:
Use the -m (--create-home) option to create the user home directory as /home/username:
The command above creates the new user’s home directory and copies files from /etc/skel
directory to the user’s home directory. If you list the files in the /home/username directory,
you will see the initialization files:
ls -la /home/username/
Copy
drwxr-xr-x 2 username username 4096 Dec 11 11:23 .
drwxr-xr-x 4 root root 4096 Dec 11 11:23 ..
-rw-r--r-- 1 username username 220 Apr 4 2018 .bash_logout
-rw-r--r-- 1 username username 3771 Apr 4 2018 .bashrc
-rw-r--r-- 1 username username 807 Apr 4 2018 .profile
Within the home directory, the user can write, edit and delete files and directories.
Here is an example showing how to create a new user named username with a home
directory of /opt/username:
By default, when a new user is created, the system assigns the next available UID from the
range of user IDs specified in the login.defs file.
Invoke useradd with the -u (--uid) option to create a user with a specific UID. For example to
create a new user named username with UID of 1500 you would type:
id -u username
Copy
1500
When creating a new user, the default behavior of the useradd command is to create a
group with the same name as the username, and same GID as UID.
The -g (--gid) option allows you to create a user with a specific initial login group. You can
specify either the group name or the GID number. The group name or GID must already
exist.
The following example shows how to create a new user named username and set the login
group to users type:
id -gn username
users
You to specify a list of supplementary groups which the user will be a member of with the -G
(--groups) option.
The following command creates a new user named username with primary group users and
secondary groups wheel and docker.
id username
The -c (--comment) option allows you to add a short description for the new user. Typically
the user’s full name or the contact information are added as a comment.
In the following example, we are creating a new user named username with text string Test
User Account as a comment:
To define a time at which the new user accounts will expire, use the -e (--expiredate) option.
This is useful for creating temporary accounts.
For example to create a new user account named username with an expiry time set to
January 22 2019 you would run:
sudo useradd -e 2019-01-22 username
Use the chage command to verify the user account expiry date: