To create a new user account, we are using the useradd
command followed by the name of the user. Please note, only root or users with sudo privileges can use the useradd
command to create new user accounts.
For example, to create a new user named username you would run:
$ sudo useradd username
When executed without any option, useradd
creates a new user account using the default settings specified in the /etc/default/useradd
file.
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:
$ sudo passwd username
How to Add a New User and Create Home Directory
On most Linux distributions, when creating a new user account with useradd
, the user’s home directory is not created.
Use the -m
(--create-home
) option to create the user home directory as /home/username
:
$ sudo useradd -m username
Creating a User with Specific Group ID
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.
$ sudo useradd -g users username
Creating a User with Custom Comment
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:
$ sudo useradd -c "New testing user" username
Creating a User with an Expiry Date
To define a time at which the new user accounts will expire, use the -e
(--expiredate
) option.
$ sudo useradd -e 2022-12-31 username
Use the chage
command to verify the user account expiry date:
$ sudo chage -l username
How to Delete User in Linux
To delete a user account named username
using the userdel
command you would run:
$ sudo userdel username
In most Linux distributions, when removing a user account with userdel
, the user home and mail spool directories are not removed.
Use the -r
(--remove
) option to force userdel
to remove the user’s home directory and mail spool:
$ sudo userdel -r username