Linux Commands - How to create a new user in Linux using useradd command

Reference Linux

Share

In this tutorial we will learn to add new users in Linux using useradd command.

Switch to root user

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: 
root@yusufshakeel-ubuntu:/home/yusufshakeel# 

Notice the $ sign changes to # and we also switch from yusufshakeel user account to root account.

useradd - Add new user

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 - 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 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 - User info file.

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.

  • Username
  • Password
  • User ID or UID
  • Group ID or GID
  • User Info
  • Home Directory
  • Shell

Username

This is used by the user to login to the system. It must consists of 1 to 32 characters.

Password

This represents the password which is stored in the /etc/shadow file in encrypted form.

User ID

This is a unique ID assigned to every user.

ID 0 is assigned to the root user.

Group ID

This is the ID of the group to which the user belongs.

The group id is saved in /etc/group file.

User info

This provides extra information about the user like the user's full name.

Home Directory

This represents the absolute path of the user's home directory.

Shell

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 -u option.

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 /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

useradd -M - Don't create home directory

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

useradd -g - Assigning Group ID

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

useradd -G - Assigning multiple groups

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

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-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.

Share