Ubuntu Shell
  • Home
  • Blog
  • Topic
    • DevOps
      • Ansible
      • Docker
      • Kubernetes
    • Distros
      • Ubuntu
    • Hardware
      • Raspberry Pi
    • How-to
    • Linux Commands
    • Opinion
    • Programming
    • Tools
    • Troubleshoot
    • Tutorials
  • Privacy Policy
  • Contact
  • About
  • Facebook
  • Twitter
  • Instagram
  • Pinterest
  • YouTube
  • Tumblr

How to Create Users in Linux using “useradd” Command [10 Practical Examples]

February 22, 2022 No Comments 10 Mins Read
Share
Share on Facebook Share on Twitter Pinterest LinkedIn Email

Adding multiple users with access control is one of the main advantages of using Linux. Whenever creating new users in Linux, we need to take care of what type of permission or authorization is being provided to that user.

In Linux, you can easily create a new user using the useradd or adduser command. Further, you can assign a group to that user (ex: sudo) or set an expiry date on that account, which we will cover today.

Table of Contents

  • How Does the Useradd Command Work?
  • Difference Between Useradd and Adduser Commands?
  • 1. How to Create a New User in Linux
  • 2. Creating New User with Different Home Directory
  • 3. Create a New User with a Specific User ID
  • 4. Create a New User with a Specific Group ID
  • 5. Create a New User without a Home Directory
  • 6. Create a New User with an Account Expiry Date
  • 7. Create a New User with Password Expiry Date
  • 8. Create a New User with Custom Comments
  • 9. Create New User with Different Login Shell
  • 10. Useradd Assemble
  • Finally: Deleting user-created account
  • Final Word

How Does the Useradd Command Work?

The general syntax for the useradd command in Linux is as follows:

Note: Creating a new user account requires root privileges or an account with sudo privileges.

$ useradd [options] username

When you execute the useradd command in your system, it performs three different things as listed below.

  1. It populate /etc/passwd, /etc/shadow, /etc/group, and /etc/gshadow files for the newly created user account.
  2. Setup permission and privileges for that new user account.
  3. Create new home directory for that new user account at /home/<newuser> (Only adduser command does that, we will talk about it more later).

Whenever a new user is created, an automatically new entry for that user is added at the /etc/passwd file. This file stores all the current user information in your system.

To check the entry for the newly created user in /etc/passwd, use the below command.

$ cat /etc/passwd | grep username

Below is the behavior of the above command.

Viewing "/etc/passwd" for user username
Viewing “/etc/passwd” for user username

The above output contains seven entries from the user, separated with “:”. Let’s discuss each one of them.

  • Username: This entry contains the information of the user login name in the system. It should be in the range of 1 to 32 characters.
  • Password: The “x” is referencing to user password stored at /etc/shadow file in an encrypted format.
  • User ID (UID): Every newly created user is attached to a unique user id (User Identification Number). The 0 UID is reserved for the root user, while UID from 1-99 is reserved for predefined accounts. Further UIDs ranging from 100-999 are reserved for system accounts and groups.
  • Group ID (GID): Every user also has their own group id in the /etc/group file.
  • Description: This field is not required, but you can store the information related to the user like full name, phone number, etc.
  • Home Directory: Every newly created account generates a new home directory at the /home/username path. This field defines the path of the user’s home directory.
  • Shell: The actual location of the user’s shell at the /bin/bash path.

Now that you understand the basic workings of the useradd command, the adduser command will be a lot easier for you to understand. But still, we talk about some differences between them.

Difference Between Useradd and Adduser Commands?

That is an important question to know. The main difference between useradd and adduser is that the useradd command just creates the user in your Linux system, while the adduser command also creates a user but also sets up user home directories and other functionalities and also asks for information related to the user such as a password, full name, location, etc. at the time of creation.

Note: Some distributions don’t provide the adduser command; in that case, we have to use the core useradd command to create a new user. On which we are mostly going to focus.

Another important component to know is that the useradd command is binary compiled with the system. But adduser is the Perl script that utilizes the useradd command in the backend.

Difference Between Useradd and Adduser Commands
Difference Between Useradd and Adduser Commands

The adduser command is more user friendly and interactive than the useradd command, but in terms of its core value, there is no difference in what one can or cannot do.

Today, you will learn basic to advanced ways to create a new user account in Linux with practical examples using the useradd command.

1. How to Create a New User in Linux

To add or create a new user account in Linux, you have to follow the useradd or adduser command with your new username.

Ensure the new user name is unique and that any existing users do not have the same user account name to avoid any conflict at the time of creation.

For example, to create a new user account with the name “jake,” follow the below command.

$ sudo useradd jake

The above command creates the new user in a locked state, meaning an account without any password. To set a custom password, use the passwd command after useradd, as shown below.

$ sudo passwd jake

Below is the behavior of the useradd and passwd command.

Creating and assigning a new password to the user
Creating and assigning a new password to the user

Once the account is created, you can check the new entry of that user at the /etc/passwd file.

$ cat /etc/passwd | grep jake

Below is the behavior of the above command.

Viewing "/etc/passwd" for user jake
Viewing “/etc/passwd” for user jake

2. Creating New User with Different Home Directory

By default, the useradd command creates a new user’s home directory at the /home/ path. If you look above, you can see that the new user “jake” has a new home directory at the /home/jake path.

Instead of new, if you want to attach an existing home directory to the newly created user, then use the -d flag along with the useradd command as below.

$ sudo useradd -d /home/linux jake

View the /etc/passwd to verify the path is attached or not to the new user, as shown below.

$ cat /etc/passwd | grep jake

Below is the behavior of both commands.

Creating New User with Different Home Directory
Creating a New User with a Different Home Directory

3. Create a New User with a Specific User ID

In Linux, every new user is dynamically assigned a unique UID (Unique Identification Number) ranging from 100 to 999.

It can be manipulated using a custom UID by using the -u flag along with the useradd command, as shown below.

$ sudo useradd -u 555 jake

View the /etc/passwd to verify the UID is assigned or not to the new user, as shown below.

$ cat /etc/passwd | grep jake

Below is the behavior of both commands.

Create a New User with a Specific User ID
Create a New User with a Specific User ID

4. Create a New User with a Specific Group ID

In Linux, every new user has their own unique GID (Group Identifier). The -g flag helps us attach different GIDs to the newly created user.

Example: I want to attach the “ubuntushell” UID, which is “1002” as “jake” GID. For that, I will follow the below command.

Tip: If you want to attach multiple groups to the newly created user, then separate each group with “,” in between.

$ sudo useradd -g ubuntushell jake

View the /etc/passwd to verify whether the GID is assigned or not to the new user, as shown below.

$ cat /etc/passwd | grep jake

Below is the behavior of both commands.

Create a New User with a Specific Group ID
Create a New User with a Specific Group ID

5. Create a New User without a Home Directory

For security reasons, if you do not want to attach a user to any home directory, then you can use the -M flag along with the useradd command.

Note: On next reboot, the user’s new home directory will be root, and if the user uses the su command to login, then its login directory will be the previous user’s home directory.

Use the below command to create a new user named “jake” without any home directory.

$ sudo useradd -M jake

To verify, use the below command to check if any new home directory is created with the name “jake” or not.

$ ls /home/

Below is the behavior of both commands.

Create a New User without Home Directory
Create a New User without a Home Directory

6. Create a New User with an Account Expiry Date

In Linux, when we create a new user account, it sets 0 as an expiry date (Never expire). For security purposes, if you want to create a user account with a specific expiry date, you can use the -e flag to set the expiry date in YYYY-MM-DD format.

For example, I want to create a new user with “jake” with account expiry date “2021-05-05” in YYYY-MM-DD format. After it hits the expiry date, that account will automatically be removed from the system.

$ sudo useradd -e 2021-05-05 jake

You can verify the expiry date for the user “jake” using the chage command as shown below.

$ chage -l jake

Below is the behavior of both commands.

Create a New User with Account Expiry Date
Create a New User with Account Expiry Date

7. Create a New User with Password Expiry Date

When we create a new user, their password is set to “-1” (meaning immortal). If you want to force the user to change the password at a specific time, then you can use the -f flag along with the useradd command.

Read Also: How to Force User to Change Their Password at Next Login in Linux

For example, I want to set the password expiry date to 45 days for user “jake” after his creation; I will follow the below command.

$ sudo useradd -f 45 jake

You can verify the expiry date for the user “jake” using the chage command as shown below.

$ sudo chage -l jake

Below is the behavior of both commands.

Create a New User with Password Expiry Date
Create a New User with Password Expiry Date

8. Create a New User with Custom Comments

The -c flag helps you to add comments to the user in the /etc/passwd file. This information can be anything like user full name, phone number, location, etc.

For example, I want to create a new user “jake”, with the comment “Jake Redfield”, for that, I will follow the below command.

$ sudo useradd -c "Jake Redfield" jake

View the /etc/passwd to verify the comment is added or not to the new user, as shown below.

$ cat /etc/passwd | grep jake

Below is the behavior of both commands.

Create a New User with Custom Comments
Create a New User with Custom Comments

9. Create New User with Different Login Shell

Sometimes, we do not want to assign any shell to users, such as bash. In that case, we attach users to a different shell, such as the nologin shell.

To attach a user to a different shell, in this case the /sbin/nologin shell, use the below command.

$ sudo useradd -s /sbin/nologin jake

View the /etc/passwd to verify the shell is replaced or not for the new user, as shown below.

$ cat /etc/passwd | grep jake

Below is the behavior of both commands.

Create New User with Different Login Shell
Create New User with Different Login Shell

10. Useradd Assemble

All the stuff we have learned can be used at the same time. Let’s combine no home directory, different UID, different GID, account expiry date, password expiry date, comment, and separate login shell at the same time for user “jake”, as shown below.

$ sudo useradd -M -u 555 -g ubuntushell -e 2021-05-05 -f 45 -c "Jake Redfield" -s /sbin/nologin jake

Below is the behavior of both commands.

Useradd Assemble
Useradd Assemble

Finally: Deleting user-created account

If you want to remove a newly created user from your system, you can use the userdel command with the user’s username, as shown below.

$ sudo userdel jake

Below is the behavior of both commands.

Deleting user-created account
Deleting a user-created account

Final Word

I hope this guide will help navigate you in the right direction for useradd and adduser command usage.

How to Create Users in Linux using “useradd” Command [10 Practical Examples]

Question

Your answer:

Correct answer:

You got {{SCORE_CORRECT}} out of {{SCORE_TOTAL}}

Your Answers

1
Author Ubuntu Shell

Ubuntu Shell is an independent technology web portal with a heavy slant towards Linux, Server, Self-Hosting, DevOps, Cloud Learning, and Open Source.

  • Website
  • Facebook
  • Instagram

Related Posts

direnv command linux

Direnv: Manage Isolated Environment Variables for Specific Project in Linux

May 6, 2022
cat command with examples

Cat Command in Linux with Examples

February 25, 2022
how to list existing user in linux

Listing All Users in a Linux System

February 25, 2022

Write A Comment Cancel Reply

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Ubuntu Shell © 2022. All Rights Reserved.

  • Facebook
  • Twitter
  • Instagram
  • Pinterest
  • Vimeo
  • RSS
  • YouTube
  • Tumblr

    Type above and press Enter to search. Press Esc to cancel.