How to Create User on Ubuntu Linux Command Line

How to Create User on Ubuntu Linux Command Line. In this post, we introduce the concept of user account, and its components. After that, we show you how to create a user on Ubuntu via the command line.

What is User Account in Linux?

First of all, the user account is a security measure to ensure that only authorized users can access the system. Hence, a user account is also used to control the privileges of the user and track the usage of system resources. Linux administrators need to make sure that they have a secure password for their accounts and never share it with anyone.

To set up and use eDirectory to manage Linux access, you must understand how the user logins work.

So, the user must already have a user account with properties or authority to access files saved in a system, to log in to a Linux system. Local user accounts are those that are kept on the computer. Regardless of whether you have saved your account on the same computer or a different computer, accounts stored in eDirectory are referred to as eDirectory user accounts.

User Account Components

Usually, a Linux computer’s login account contains the following details:

Username and user ID (UID)

Both Username and UID are the two distinct identifiers that each user account has.

A user account’s username is mapped to a specific UID when it is created. Presently, a user can change the username as per his choice or requirement, but it must be unique. Remember the same username cannot be used by two users.

However, UID is fixed and unchangeable. Once an account is active, you cannot change the UID for that user account. Its role is to verify and keep track of user account activities and system resource usage.

The user uses their username, whilst the system uses their UID.

Password

You are already aware, that Passwords are a critical component of any security system. They provide the first line of defence against unauthorized access to your account. Each user account has a password that is encrypted and can be saved on the system itself. Then, it can be stored on a local password in the /etc/passwd or /etc/shadow file.

Remember, that Linux encrypts the password that the user enters when logging in and compares it with the saved password details. Significantly, the user is given access if the added value matches the one saved in the password field on the system.

For user account information, administrators choose /etc/passwd file, whereas the /etc/shadow file is for saving the encrypted passwords.

Primary group name and group ID (GID)

The Primary Group Name is the name of the group that a user belongs to. The Primary Group Name can be any string and does not need to be unique. A Primary Group ID must be unique for each group and it cannot contain spaces or special characters.

Secondary Group Names and ID’s

Secondary Groups are saved by administrators in /etc/group as entries and are not mandatory for user accounts. The Group ID is a unique number that identifies your group. Regardless, you use this ID to find and add members to the group.

Home Directory

Next component is a home directory. In effect, it is a directory in the file system that is assigned as the default location for a user’s data. The home directory is generally used to store personal files, like documents and pictures, but can also be used to store application data. Only the user and system administrator access this specific directory.

Preferred shell

The shell provides the user with a command line interface and a way to interact with the operating system. There are many different shells in use today, but the default shell for Linux is mostly /bin/bash.

We now came to the main part of the article How to Create User on Ubuntu Linux Command Line.

How to Create User on Ubuntu Linux Command Line

There are several ways to create a user on Ubuntu Linux. Create a user via the command line or through the GUI. In fact, creating users via the command line gives you more control and option compared to the graphical user interface. There are two command line tools you can use to create a user in Linux.

  • adduser – It is an interactive and friendly command line utility that allows you to create a user and set a password on the fly.
  • useradd – It is a low level utility. You will need to run another command to set a password after creating a user.

Create a User Using adduser Command

The basic syntax to create a user using adduser command is shown below:

				
					adduser [username]
				
			

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

				
					adduser testuser1
				
			

You will be asked to set a password as shown in the following screen:

The above command will create the user’s home directory at /home/testuser1 and copy files from the /etc/skel directory to the user’s home directory.

To verify your newly created user, run the following command:

				
					id testuser1
				
			

You should see the user id and group id of the created user in the following output:

				
					uid=1000(testuser1) gid=1000(testuser1) groups=1000(testuser1)
				
			

If you want to see the hashed password of the user, run the following command:

				
					cat /etc/passwd | grep testuser1
				
			

You should see the following output:

				
					testuser1:x:1000:1000:Hitesh Jethva,1,8292922922,:/home/testuser1:/bin/bash
				
			

Create a User Using useradd Command

In this section, we will show you how to create a user using useradd command in Linux.

1. Basic Syntax of useradd Command

The basic syntax of the useradd command is shown below:

				
					useradd [option] [username]
				
			

A brief explanation of each option is shown below:

  • -d – Define the new home directory of the user account.
  • -s – Define the login shell of the user account.
  • -c – Specify the comment of the user account.
  • -g – Set the primary group of the new user account.
  • -m – Create the user’s home directory at the default location.
  • -r – Create a system user account.
  • -M – Do not create a user’s home directory.
  • -u – Set the user ID of the new account.
  • -g – Define the group or ID of the new account.

2. Create a User with a Custom Home Directory

When you create a user using the useradd command, it will create a user’s home directory at /home/username location. You can also use define the custom home directory path using the -d option.

Let’s create a new user named testuser2 with a custom home directory path at /opt/.

				
					useradd -m -d /opt/testuser2 testuser2
				
			

After creating a user, you can verify the user’s home directory using the following command:

				
					ls -la /opt/testuser2/
				
			

You should see the following screen:

3. Create a User with a Custom User Identifier ID

By default, useradd command creates a random UID number for a new user. You can also set your own UID number using the -u option.

Let’s create a new user named testuser3 and specify a custom UID 1099:

				
					useradd -u 1099 testuser3
				
			

After creating a user account, you can verify the UID number using the following command:

				
					id testuser3
				
			

You should see the following screen:

4. Create a User with an Expiration Date

The useradd command also allows you to create a user account with an expiration date. This is very helpful when you want to give someone temporary access to the system.

To create a user account named testuser4 and set an expiration date 2022-12-30, run the following command:

				
					useradd -e 2022-12-30 testuser4
				
			

Next, you can now verify the user’s expiration date using the following command:

				
					chage -l testuser4
				
			

You should see the user’s expiration date on the following screen:

5. Set a Password of the User Account

When you create a user account using useradd command, it will not create a password. So, the user can not log in to the system without password.

In this case, you will need to set a password for each user. Let’s create a password for the user named testuser4 using the passwd command:

				
					passwd testuser4
				
			

You will be asked to set a password on the following screen:

Next, verify that the user account works by logging in to the testuser4 account using the su command:

				
					su - testuser4
				
			

You will be asked to provide a password to log in to the system as shown below:

Thank you for reading How to Create User on Ubuntu Linux Command Line. We will conclude this article now. 

How to Create User on Ubuntu Linux Command Line Conclusion

In this guide, we explained how to create users on Ubuntu Linux via the command line. We show you how to use adduser and useradd commands to create a user account on Ubuntu. Also, we tried to explain each option you can use while creating a new user account on Ubuntu Linux. I hope this guide helps you to create a user account on Ubuntu Linux. Also there is a possibility to create a user account via the GUI interface but it has limited options.

Please take a look at more Ubuntu content by navigating to our blog over here

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x