Open Source Projects
In this tutorial we will learn to add new users in Linux using useradd command.
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:
Notice the $ sign changes to # and we also switch from yusufshakeel user account to root account.
To add a new user we will use the useradd command.
In the following example we are creating a new user account by the login name jane.
# useradd jane
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 passwd command.
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
When a new user is added an entry is made in the /etc/passwd.
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 /etc/group file.
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.
If we want to assign specific User ID to a user then we can use the -u option.
In the following example we are creating a new user jane with userid 500.
# useradd -u 500 jane
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 /home/alice.
If we want to assign specific home directory to a user then we can use the -d option.
In the following example we are creating a new user jane with home directory /workspace/jane.
# useradd -d /workspace/jane jane
If we don't want to assign home directory to a user then we can use the -M option.
In the following example we are creating a new user jane without home directory.
# useradd -M jane
If we want to assign specific Group ID to a user then we can use the -g option.
In the following example we are creating a new user bob with groupid 500.
# useradd -g 500 bob
If we want to add a user to multiple groups then we use the -G option.
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
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-MM-DD format.
Where, 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.
Have fun learning :-)
Copyright © 2014 - 2019 DYclassroom. All rights reserved.
rendered in 0.0146 sec