Reference Linux
In this tutorial we will learn to add new users in Linux using useradd command.
useradd
root
Before we run the commands given in this tutorial we will switch to root user by running the sudo su command.
sudo su
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: root@yusufshakeel-ubuntu:/home/yusufshakeel#
Notice the $ sign changes to # and we also switch from yusufshakeel user account to root account.
$
#
yusufshakeel
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
passwd
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
/etc/passwd
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 root:x:0:0:root:/root:/bin/bash ... ... ... jane:x:1001:1001::/home/jane:/bin/sh
We can see that we have the new entry for user name jane.
jane:x:1001:1001::/home/jane:/bin/sh
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.
/etc/shadow
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.
/etc/group
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
If we want to assign specific User ID to a user then we can use the -u option.
-u
In the following example we are creating a new user jane with userid 500.
# useradd -u 500 jane
useradd -d
By default, when we create a new user a home directory is also created inside the /home directory by the username.
/home
So, if we create a new user lets say, alice then we will get a new home directory /home/alice.
/home/alice
If we want to assign specific home directory to a user then we can use the -d option.
-d
In the following example we are creating a new user jane with home directory /workspace/jane.
/workspace/jane
# useradd -d /workspace/jane jane
useradd -M
If we don't want to assign home directory to a user then we can use the -M option.
-M
In the following example we are creating a new user jane without home directory.
# useradd -M jane
useradd -g
If we want to assign specific Group ID to a user then we can use the -g option.
-g
In the following example we are creating a new user bob with groupid 500.
# useradd -g 500 bob
useradd -G
If we want to add a user to multiple groups then we use the -G option.
-G
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
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.
-e
YYYY-MM-DD
Where, YYYY represents year. MM represents month and DD represents day.
YYYY
MM
DD
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.