How to create a user in Linux

While installation, the Linux server allows us to create multiple users. Because Linux is a multi-user system, multiple users can work in the same system at the same time. We have permission to do so through the Setup agent.

We must create an account in order to work with Linux because we cannot continue to work with the root account. We have one administrative account; a system administrator account is in charge of managing the system’s user accounts and groups.

One of the most fundamental tasks of a new Linux server is adding or removing users.

A new Linux server only provides us with a root user account. Adding a user account gives a specific user a lot of power and access. It is a useful but insecure Linux server utility. It is a good idea to add an unprivileged user to perform routine tasks. However, we can gain administrative privileges by using the Sudo command-line utility.

Create a user in Linux (Ubuntu)

There are two methods for adding a user to a Linux server.

  • Graphically, via the user manager
  • By using the useradd command (Terminal)

1. Graphically via the user manager

We can create a user from the functions of the Linux GUI. It’s a straightforward procedure. Follow the steps below to add a user to your Linux server:

Step 1: Open the system search and look for the setting, then navigate to Detail-> About.

Step 2: Click on the Users option, followed by the Unlock option in the header. It will prompt you for the system security password; enter it and click OK to proceed.

Step3:To add a new user, select the Add User option.

Step4: Enter the user information, such as username and password, as well as the account type. We can create two kinds of accounts: Standard and Administrator. The standard account lacks the sudo privilege. We can, however, provide it later.

Step5: We have now successfully created a new user named JTP2.

2. By the Linux useradd command

The useradd command in Linux is a command-line utility used to add or remove a user from a Linux server or Unix-based operating system.

The useradd command may differ slightly depending on the type of Linux distribution.

The useradd command accomplishes the following tasks:

  • It modifies files such as /etc/passwd, /etc/shadow, /etc/group, and /etc/gshadow for newly created users.
  • It generates and launches a new home directory.
  • It enables us to change the ownership and permissions of the home directory.

Syntax:

useradd [options] username

To use the useradd command, we must be logged in as root or sudo.

Before we use the Linux useradd command, let’s define a few terms that are commonly used in the Linux command line.

  • Username: A username is a unique identifier that is used for logging into the Linux system. It appears when we turn on our machine. The length of the username should be between 1 and 32 characters.
  • Password: A password is a secret code that is used to prevent unauthorised access to your system. It is encrypted and stored in the etc/shadow file.

User ID (UID): Every user in Linux is assigned a unique Id, which is known as a user identification number, User ID, or UID. The root user’s UID is set to zero by default, and the remaining UIDs from 1 to 99 are reserved for other predefined accounts. Furthermore, UIDs between 100 and 999 are reserved for groups and system accounts.

Group ID (GID): The GID, or Group ID, is a Linux system-provided group identification number. It’s kept in the /etc/group file.

User Info: It enables us to specify additional information about the user, such as the user’s full name. It is entirely optional.

Home Directory: It is a user’s absolute location.

Shell: It is the absolute path to a user’s shell, /bin/bash.

To create a new user with the useradd command, use the following syntax:

sudo useradd JTP3

The above command will prompt you for the system administration password; enter it. It will make a user called JTP3. The user logs in to the system using this username. The username must be one-of-a-kind.

Execute the following command to set the password for the newly created user:

sudo passwd JTP3

The above command will prompt for a new password, which you should enter and retype. It will change the password for the user-specified.

Create a user with a home directory

A home directory may or may not be assigned to the newly created user. Execute the following command to create a user and forcefully assign it a home directory:

sudo useradd -m Demo

The preceding command will prompt for the system administration password and create a home/Demo directory for the user Demo.

Create a user with a different home directory

It will prompt for the system administration password and create a home/Demo directory for the user Demo.

Instead of the default folder, Linux allows us to create a home directory in a different location. To create a different home directory, use the -d option with the useradd command. Run the following command:

sudo useradd -m -d /Demo1 Demo1

The command above will create a Demo1 folder in the root directory for the user Demo1.

Create a user with an expiry date

To create a user with an expiry date, which means that it will be deleted after a certain date.

sudo useradd -d /home/test -e 2020-03-16 Demo2

The command above will create a user with a specified expiration date. It will create a user named Demo2, who will be deleted after a set period of time.

It will be useful if you want to create an account for a user who will leave after a short period of time.