Cyber Month Deal - up to 36% OFF

How to Create a User in Linux [With Examples]

Published on May 13, 2024 Updated on Aug 15, 2024

User provisioning is one of the routine tasks that Linux system administrators carry out from time to time. In this tutorial, I will first explain the differences between the adduser and useradd command line tools and then show you how to create a user in Linux using both of these with clear examples.

Linux user groups

There are two broad categories of users in Linux: regular and system users. Regular users are created by the root or administrative user (sudo user). These users have login capabilities and home directories that contain personal files and folders.

On the other hand, Linux system users are created by the system during the installation of the operating system or software applications. They are used by the system to run services.

Note: For further guidance on managing Linux users, check our guide on how to list users in Linux.

How to create a user in Linux?

The adduser and useradd are command-line tools used to create new users in Linux/UNIX systems. The two carry out the same task but employ a different execution method. So, let's dive right in; below, I'll explain each method with a specific example of how to create a user in Linux.

Option 1: The adduser command

The adduser command-line tool is a utility native to Debian-based distributions such as Ubuntu, Debian, and Mint to create a user in Linux. It's a powerful and more user-friendly command-line utility for adding users than the useradd command. It provides an interactive approach for adding users where user creation is displayed step-by-step on the screen. The adduser command syntax is pretty straightforward. You simply type adduser followed by the name of the user that you want to create.

sudo adduser username

For example, to create a new user called alex, run the command:

sudo adduser alex

Once invoked, the command creates a new user and group that bears the username of the user being created. It then adds the user to the group and creates a home directory for the user. In addition, it copies files from the /etc/skel directory to the new user’s home directory.

The /etc/skel directory contains files and directories required in the new user’s directory. These include the .bashrc, .bash_logout, and .profile configuration files.

Next, you will be prompted to provide the user’s password. Once you have provided and confirmed the password, you will be required to provide additional information about the user, such as full name, room number, work phone, and home phone. Fill in the information where applicable, or hit ENTER to skip.

To save the information provided, type ‘Y’ and hit ENTER.

create-a-regular-user-with-adduser-command

To confirm the creation of the user, run the id command followed by the username of the user.

id  alex

This prints out the UID (User ID), GID ( Group ID), and the groups that the user belongs to. Alternatively, you can view the /etc/passwd file, which is a colon-separated file that stores user information.

cat  /etc/passwd | grep -i alex

confirm-user-creation-with-id-command

As you can see, the adduser command is straightforward and easy to use, especially for beginners. Also, note that the command requires root or superuser privileges. That implies you will always have to run it as root or invoke sudo before the command if you are running it as a sudo user.

Option 2: The useradd command

The useradd is another command-line utility for creating users in a Linux system. It comes included in all Linux distributions by default. Like its counterpart adduser command, only root or sudo users can create new accounts with useradd.

The useradd utility refers to the /etc/login.defs file, from where it applies default user parameters such as the range of User IDs and Group IDs, password aging controls, login timeout, etc.

The syntax for the useradd command is as follows.

useradd [OPTIONS] username

Various options can be used alongside the useradd command to specify user attributes. Let’s look at some of the commonly used useradd command-line options.

1. Create a new user without any options

The useradd command can be used to create a user without invoking any command-line options. For example, to create a user called alice, run the command:

sudo useradd alice

The command appends an entry to the /etc/passwd, /etc/shadow, /etc/gshadow and /etc/group files.

Next, set the password using the passwd command to enable the user to login to the system. So, invoke the passwd command followed by the username.

passwd alice

Be sure to provide a strong password and confirm it.

create-a-regular-user-with-useradd-command

Once done, confirm that the user has been created by running the id command.

id alice

Additionally, you can search for the user’s entry in the /etc/passwd file as shown.

cat  /etc/passwd | grep -i alice

confirm-user-creation-with-id-command

2. Create a User with a Specific User ID

By default, the system automatically assigns the next available UID from the range of user IDs contained in the /etc/login.defs file.

The -u (--uid) option allows you to define a specific User ID for the user.

For example, to create a user called bob with a UID of 1005, run the command:

sudo useradd -u 1005 bob

Once again, you can verify the UID using the id command:

id bob

From the output below, you can see that the user bob has acquired a UID of 1005, which also happens to be its GID.

create-a-regular-user-with-specific-user-id-uid

3. Create a User with a Specific Group ID

When a new user is created, it belongs to a primary group whose Group ID ( GID ) is the same as the User ID ( UID ). Suppose you want to assign a different GID other than the default. You can achieve this by passing the -g (--gid) option and specifying the group name or GID number.

For example, to create a user called john with a GID of 100, run the command:

sudo useradd -g 100 john

Alternatively, you can specify the group name corresponding to the GID number. In this case, the users group corresponds to GID 100

sudo useradd -g users john

You can verify the group the user belongs to using the id command:

id -gn john

OR

id john

create-a-regular-user-with-specific-group-id-gid

NOTE

The group that the user is being added to must already exist for this operation to be successful.

4. Create a Linux user and assign multiple groups

A Linux user can belong to two broad categories of groups: exactly one primary group, which is the default group, and one or multiple secondary groups.

You can pass the -G (--groups) option to specify one or more secondary groups that the user can be a part of. For instance, in the command below, we are creating a user called mark with a primary group called users and belonging to secondary groups called wheel, kvm, and ftp.

sudo useradd -g users -G wheel,kvm,ftp mark

To verify this, once again, run the id command.

id mark

assign-multiple-groups-to-a-user

5. Create a new user with a home directory

A home directory is a login directory that contains the user's files and folders as well as environment setting files. This is the directory a user lands into upon login.

In older Linux distributions, the home directory is not created by default using the useradd command.

Pass the -m (--create-home) option to create a user home directory in the /home/<username> path.

For instance, to create a user called patrick with a home directory in the /home/patrick path, run the command:

sudo useradd -m patrick

You can confirm the creation of the user’s home directory using the ls -la command as shown.

ls -la /home/patrick

The output lists the user’s initialization files which are typically present in any user’s home directory.

create-a-regular-user-with-a-home-directory

6. Create a user with a unique home directory

As you can see, the default user's home directory is in the /home directory. To specify a different location, pass the `-d' option.

In the following example, we create a new user called bill with the home directory specified as opt/bill.

sudo useradd -m -d /opt/bill  bill

create-a-regular-user-with-a-specific-home-directory

7. Create a user without a home directory

If you wish to exclude the creation of the user’s home directory, pass the -M option followed by the username. In the following example, we create a new user called sandra without the home directory.

sudo useradd -M sandra

To verify that the system skipped creating the home directory, run the command:

sudo ls  -la /home/sandra

From the output, the contents of the home directory were not listed for the simple reason that the directory was not created.

create-a-regular-user-without-a-home-directory

8. Create a user with a specific comment

A comment is simply a short description of the user. I can include anything from a phone number, job title, residence, etc. To add a description, use the -c option.

In this example, we are creating a new user called george with the job title "Human Resource Manager" as the comment.

sudo useradd -c "Human Resource Manager" george

create-a-regular-user-with-a-specific-comment

9. Create a user with an account expiry date

Lastly, you can create a new user and enforce an account expiry date. Once the account expires, the user will no longer be able to log in. This is mostly used when creating temporary users.

The expiry date is expressed in YYYY-MM-DD format.

For example, to create a new user called kristin whose account expires on 2024-06-10, run the command:

sudo useradd -e 2024-06-10 kristin

Invoke the chage command to confirm the user account expiry date.

sudo chage -l kristin

create-a-regular-user-with-an-account-expiry-date

Conclusion

In this tutorial, I've demonstrated how to create a user in Linux and add users using either the adduser or useradd command-line utilities. The adduser command is an interactive utility available for Debian and Ubuntu-based Linux distributions, whereas the useradd command works across all Linux distributions.

Cloud VPS - Cheaper Each Month

Start with $9.99 and pay $0.5 less until your price reaches $6 / month.

Share this article

Related Articles

Published on Jun 7, 2021 Updated on Jun 29, 2022

AlmaLinux Review: a CentOS Clone Supported by CloudLinux

AlmaLinux is an open-source Linux distribution focused on long-term stability, that is a 1:1 binary compatible fork of Red Hat Enterprise Linux (RHEL)

Read More
Published on Sep 14, 2021 Updated on Jun 29, 2022

Debian 11 "bullseye" Review: What‘s New?

Debian 11 “bullseye” was released on 14th of August 2021. This release contains over 11294 new packages out of 59551 packages overall in its repositories.

Read More
Published on May 31, 2022 Updated on May 5, 2023

A Complete Guide to Linux Bash History

Learn how to work with Bash history to become more efficient with any modern *nix operating system.

Read More
We use cookies to ensure seamless user experience for our website. Required cookies - technical, functional and analytical - are set automatically. Please accept the use of targeted cookies to ensure the best marketing experience for your user journey. You may revoke your consent at any time through our Cookie Policy.
build: d7d8ce6b7.830