In this tutorial we will learn to add new users in Linux using
Before we run the commands given in this tutorial we will switch to root user by running the
sudo su command.
In the following example I am switching from my account to root account. You will get a similar result on your terminal.
yusufshakeel@yusufshakeel-ubuntu:~$ sudo su
[sudo] password for yusufshakeel:
$ sign changes to
# and we also switch from
yusufshakeel user account to
useradd - Add new user
To add a new user we will use the
In the following example we are creating a new user account by the login name jane.
# useradd jane
passwd - Set password
When a new user is successfully created it is in a locked state.
To unlock a new user account we have to set the password for the user account using the
In the following example we are going to set the password for the new user jane.
# passwd jane
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
/etc/passwd - User info file.
When a new user is added an entry is made in the
In the following example we are listing all the entries in the passwd file.
# cat /etc/passwd
We can see that we have the new entry for user name jane.
The line is colon
: separated and consists of 7 parts which are the following.
This is used by the user to login to the system. It must consists of 1 to 32 characters.
This represents the password which is stored in the
/etc/shadow file in encrypted form.
This is a unique ID assigned to every user.
ID 0 is assigned to the root user.
This is the ID of the group to which the user belongs.
The group id is saved in
This provides extra information about the user like the user's full name.
This represents the absolute path of the user's home directory.
This represents the absolute path of the user's shell.
useradd -u - Assigning User ID
If we want to assign specific User ID to a user then we can use the
In the following example we are creating a new user jane with userid 500.
# useradd -u 500 jane
useradd -d - Assigning home directory
By default, when we create a new user a home directory is also created inside the
/home directory by the username.
So, if we create a new user lets say, alice then we will get a new home directory
If we want to assign specific home directory to a user then we can use the
In the following example we are creating a new user jane with home directory
# useradd -d /workspace/jane jane
useradd -M - Don't create home directory
If we don't want to assign home directory to a user then we can use the
In the following example we are creating a new user jane without home directory.
# useradd -M jane
useradd -g - Assigning Group ID
If we want to assign specific Group ID to a user then we can use the
In the following example we are creating a new user bob with groupid 500.
# useradd -g 500 bob
useradd -G - Assigning multiple groups
If we want to add a user to multiple groups then we use the
In the following example we are creating a new user eve and assigning the account to developer and tester groups.
# useradd -G developer,tester eve
useradd -e - Creating temporary account
If we want to create an account that expires after a given date then we use the
-e option followed by the date in
YYYY represents year.
MM represents month and
DD represents day.
In the following example we are creating a temporary user tempuser that will expire after 2018-12-31 i.e. 31st December 2018.
# useradd -e 2018-12-31 tempuser
Hope this was useful to you. Please share this tutorial if you find my work interesting and helpful. Thanks for reading. See you in the next tutorial.