How to Create User in Linux by Adduser in Ubuntu 19.04 | Linux Tutorial

How to Create User in Linux by Adduser in Ubuntu 19.04 | Linux Tutorial

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

Today I am going to cover how to create user in Linux or add user in Ubuntu by command. Having multiple users with different privilege 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 19.04

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

18-addusr-command-in-ubuntu-linux

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 19.04, and have vijay user that is has sudo privileges. if you are sudo user then add prefix sudo to every command otherwise you will get error.

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

Syntax ls /home is showing contents of /home directory

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

Following syntax and result is confirmed user has been created.

vijay@Ubuntu-19:~$sudo cat /etc/passwd | grep ram
ram:x:1002:1002::/home/ram:/bin/sh
vijay@Ubuntu-19:~$

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 time same it will not display on screen when you type and retype.

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

Create a user in Linux with different Home directory

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

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

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-19:~$sudo useradd -d /data/project/ nonu
vijay@Ubuntu-19:~$sudo cat /etc/passwd | grep nonu
nonu:x:1004:1004::/data/project/:/bin/sh
vijay@Ubuntu-19:~$

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-19:~$sudo useradd -u 1200 -g 5000 surya
useradd: group '5000' does not exist
vijay@Ubuntu-19:~$

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-19:~$sudo tail -5 /etc/passwd
systemd-coredump:x:999:999:systemd Core Dumper:/:/sbin/nologin
cyrage:x:1001:1001:Cyrage,,,:/home/cyrage:/bin/bash
ram:x:1002:1002::/home/ram:/bin/sh
ansh:x:1003:1003::/home/ansh:/bin/sh
nonu:x:1004:1004::/data/project/:/bin/sh
vijay@Ubuntu-19:~$sudo useradd -u 1200 -g 1004 surya
vijay@Ubuntu-19:~$sudo cat /etc/passwd | grep surya
surya:x:1200:1004::/home/surya:/bin/sh/
vijay@Ubuntu-19:~$

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-19:~$sudo useradd -M anshika
vijay@Ubuntu-19:~$ls /home/
ansh  cyrage  vijay
vijay@Ubuntu-19:~$

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-19:~$sudo useradd -e 2020-5-29 hema
vijay@Ubuntu-19:~$sudo chage -l hema
Last password change					: May 11, 2019
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
vijay@Ubuntu-19:~$

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-19:~$sudo useradd -c "Amitabh Bachhan" amit
vijay@Ubuntu-19:~$sudo cat /etc/passwd | grep amit
amit:x:1203:1203:Amitabh Bachhan:/home/amit:/bin/sh
vijay@Ubuntu-19:~$

If Appreciate My Work, You should consider: