Mastering User Creation: Using the adduser Command in Linux


Are you a perfect user in Linux or need one more user for your Linux system/server.

Today I am going to cover how to create user in Linux or add a user in Ubuntu by command. Having multiple users with different privileges is good for linux system security propose.

This article is not to cover types of users in Linux.

‘useradd’ and adduser are the two most popular commands in Linux to create a new user. You are a system administrator and asked to create a new user account in Linux with some specific properties, limitations, and comments, etc.

useradd command in Linux is more powerful than the adduser. adduser is a low-level utility, that is used for adding new user accounts only, cannot add specific properties for the same user.

Most Linux System Administrator used to useradd command rather than adduser.

When you run ‘useradd‘ command in Linux, it performs the following major operations:

  • It edits /etc/passwd, /etc/shadow, /etc/group and /etc/gshadow files for the newly created User account.
  • Creates and populate a home directory for the new user.
  • Sets permissions and ownership to the home directory.

Article Contents

How to add a user in Ubuntu 20.04

Add user by using adduser command syntax is given in image below


useradd in Linux for creating a new user

useradd command in Ubuntu or other Linux is used to create users. But normal user can’t run this command, and will get error “Permission denied”

Only root user or sudo user can perform this task, When you create a user at the time of installation Ubuntu has sudo privileges and run useradd command.

I have Ubuntu 20.04 and have vijay user that has sudo privileges. if you are sudo user then add prefix sudo to every command otherwise you will get an error.

Syntax “sudo useradd ram” creates a user with name ram without a home directory.

Syntax ls /home is showing contents of /home directory

vijay@Ubuntu-20:~$useradd ram 
useradd: Permission denied.
useradd: cannot lock /etc/passwd; try again later.
vijay@Ubuntu-20:~$sudo useradd ram
vijay@Ubuntu-20:~$ls /home/
cyrage  vijay

Following syntax and result is confirmed user has been created.

vijay@Ubuntu-20:~$sudo cat /etc/passwd | grep ram

Generate password of new user

When you create a user with useradd command, the user is created without a password. So you need to create/generate a new password for newly created user ‘ram’.

When you run “sudo passwd username” command it will ask for a new password and retype the same password again.

Warning: Enter type two times the same it will not display on the screen when you type and retype.

vijay@Ubuntu-20:~$sudo passwd ram
New password: 
Retype new password: 
passwd: password updated successfully

Create a user in Linux with different Home directory

When you create user in Linux, the home directory for the user has been created in /home directory by default at the same time.

vijay@Ubuntu-20:~$sudo useradd -m ansh
vijay@Ubuntu-20:~$ls /home/
ansh  cyrage  vijay

If you want to assign a different home directory to the user then you can run the following syntax:

$sudo useradd -d /path/different_Directory Username

I am going to create new user name nonu with home directory /data/project

vijay@Ubuntu-20:~$sudo useradd -d /data/project/ nonu
vijay@Ubuntu-20:~$sudo cat /etc/passwd | grep nonu

Create a user in Linux with Specific User ID and Group ID

you can use -u [USER ID NO] for assign userID -g [GROUP ID NO.] for Group ID.

For Example, I am going to create a user with the name Surya and userid 1200 and group id 5000. I got an error when I performed the same task

When you assign Group ID, Any group must have same group ID assigned.

vijay@Ubuntu-20:~$sudo useradd -u 1200 -g 5000 surya
useradd: group '5000' does not exist

I checked by using following commands for looking group ID then I changed group ID in my next command

Hurr I got success.

vijay@Ubuntu-20:~$sudo tail -5 /etc/passwd
systemd-coredump:x:999:999:systemd Core Dumper:/:/sbin/nologin
vijay@Ubuntu-20:~$sudo useradd -u 1200 -g 1004 surya
vijay@Ubuntu-20:~$sudo cat /etc/passwd | grep surya

Create a user in Linux without Home Directory

If you want to create a new user in Linux without home directory use the -M [Capital M] option followed by useradd command

Look on the syntax given below

vijay@Ubuntu-20:~$sudo useradd -M anshika
vijay@Ubuntu-20:~$ls /home/
ansh  cyrage  vijay

Create a user in Linux with account Expiry Date

-e option is used to add expiry date with useradd command. Date format should be YYYY-MM-DD.

Use chage -l command to see details:

vijay@Ubuntu-20:~$sudo useradd -e 2020-5-29 hema
vijay@Ubuntu-20:~$sudo chage -l hema
Last password change					: May 11, 2020
Password expires					: never
Password inactive					: never
Account expires						: May 29, 2020
Minimum number of days between password change		: 0
Maximum number of days between password change		: 99999
Number of days of warning before password expires	: 7

Add a user with the custom comment in Linux

Comment is playing role to define special about user. You can use “Dont Delete this account” or “This is account of HR” or Be careful this CEO’s account” or Full name of user.

Other system administrator can identify user with this comment. -c option is used to define comment.

The comment will display in /etc/passwd file. you can see in result.

vijay@Ubuntu-20:~$sudo useradd -c "Amitabh Bachhan" amit
vijay@Ubuntu-20:~$sudo cat /etc/passwd | grep amit
amit:x:1203:1203:Amitabh Bachhan:/home/amit:/bin/sh

If you like our content, please consider buying us a coffee.
Thank you for your support!