How to Create User in Linux [Linux Create User Command]

How to Create User in Linux [Linux Create User Command], Adding users without a graphical user interface (i.e., GUI) can sometimes be cumbersome. Today we will show you how to create new users in Linux and modify the type of user created.

Updated: 05 Mar, 23 by Antoniy Yushkevych 7 Min

List of content you will read in this article:

As you know, Linux is a system where multiple people can interact with one system simultaneously. To reach this objective, you can use useradd command in Linux. This command’s primary aim is to Create User in Linux. 

If you are a system administrator, you have the authority to manage users and the groups of the system. You can create or remove the users as well. Here, we are going to talk about creating several new accounts with the use of the Linux useradd command. Along with that, you will see many more functions.

Let’s begin!

The useradd Command

Here is the general syntax to begin with useradd command, which is Linux Create User Command:

sudo useradd test_user

Linux  Virtual Server
Linux Virtual Server

Starting From $5.99/Monthly

The useradd command here creates a new account. It runs according to some of the default values mentioned in the /etc/default/useradd file.

Along with this, the command also runs through the /etc/login.defs file. This file imbibes certain information like the range of user IDs, password expiration policies, and much more.

To log in as a new user, you must set the user password first. How do you do that?

Simply write this command:

passwd username

After you execute this command, you need to enter the details. Always set a secure password with alphanumerics. 

Output:

Changing password for user username.

New password:

Retype new password:

passwd: all authentication tokens updated successfully.

Now, have a look at the list of commands to fulfill several other requirements.

The useradd command automatically creates a home directory of the user in /home. If you want to change the location, use -d option.

Now, look at the following command to create a user having /opt/username as the home directory.

sudo useradd -d /home/test_user test_user

When we talk about groups in Linux, they are primarily used in the administration and the usage of several user accounts. 

Groups generally define certain privileges. For instance, read, write, and execute permissions. These permissions are given to a particular resource to be shared among other users.

The useradd command automatically creates a group with the name as that of the username. With the exact copy of the GID as that of the UID. 

Now, -g (- -gid) helps create a user with a certain login group. You have to mention the GID number or the group name.

useradd -g 1000 test_user 

As you know, the users are known by their respective UID and username. 

When we talk about a User Identifier, UID, it is simply a positive integer that is allocated to every user in the Linux system. It helps to understand the different types of actions that a user can perform on the system. 

In the login.defs file, a range of user IDs is mentioned. The system automatically allocates the available user identifier from this range. 

To execute the command, use the following syntax:

useradd -u 1234 test_user

If you further want the verification of the user UID, type this:

id -u username

Output:

1234

The Linux operating system inhibits two kinds of groups. Primary group and secondary group. The secondary group is also called a supplementary group. 

Every user can be assigned to only one primary group. Whereas in the case of secondary groups, the number is either zero or more.

Look at the below-stated command mentioning the primary group ‘users’ and secondary group ‘wheel’ and ‘docker.’

sudo useradd -g users -G wheel,developers username

If you want to check the user groups, type the following command:

id username

Output:

uid=1002(username) gid=100(users) groups=100(users),10(wheel),993(docker)

For your information, a new user’s login shell is always set according to the one depicted in /etc/default/useradd file.

You will notice that in some cases, the default shell is /bin/sh, whereas, in others, it is specified in /bin/bash.

If you want to mention the login shell of a new user, kindly use -s (- -shell) option. 

Now here is a syntax to create a user we change the login shell:

-s /bin/sh test_user

There are certain times when you would want to know the expiry date of the user accounts. To do that, use the following option: -e (- - expiredate). You can also create temporary accounts with this option.

Here is the syntax to create a user with an expiry date:

useradd -e 2021-03-05 test_user

Using this command, you can create a user with an expiry date of March 30, 2021.

If you want to verify the user account expiry date, then use the following command:

sudo chage -l username

In case you need to add a description for a new user, use -c (- - comment) option.

For instance,

sudo useradd -c “This is a test user” test_user

To full fill this needs, type the following command:

sudo useradd -M test_user

This command helps you to create a user without a specific home directory.

To set an unencrypted password, use the following command:

sudo useradd -p test_password test_user

Particularly there is no solid technical difference between a regular user and the system user. When we talk about system users, they have created while installing the operating system. 

To create a system user account, use the -r (- - system) option. 

Syntax:

sudo useradd -r username

For your information, the system users do not have an expiry date. 

You can do that using -D, (- - defaults option). 

Manually, just edit the values in /etc/default/useradd file.

Use the following command to view the default option type:

useradd -D

Output:

GROUP=100

HOME=/home

INACTIVE=-1

EXPIRE=

SHELL=/bin/sh

SKEL=/etc/skel

CREATE_MAIL_SPOOL=no

Using the following command, you can retrieve the help section of useradd command.

sudo useradd --help

We have presented information about using useradd command in Linux to create several user accounts. The above-stated article showcases numerous ways to make your working easier in the Linux system.

We hope the information adds value to your knowledge and widens your horizons.

People are also reading:

Antoniy Yushkevych

Antoniy Yushkevych

Master of word when it comes to technology, internet and privacy. I'm also your usual guy that always aims for the best result and takes a skateboard to work. If you need me, you will find me at the office's Counter-Strike championships on Fridays or at a.yushkevych@monovm.com