Jannah Theme License is not validated, Go to the theme options page to validate the license, You need a single license for each domain name.
Linux

How to Add Users in Linux?

Introduction 

One must first become a Linux user to utilize the Linux-based operating system. Each member of the Linux club is assigned a unique user identity, and these users can be grouped. By adding users to groups, they can access specific resources and permissions.

It is important to note that Linux is a multiuser environment, and managing user accounts is a crucial responsibility for system administrators. They are tasked with adding, deleting, and managing users and groups on the system using the ‘useradd’ command, which offers various options for automating identity and access management. 

This article explores the add user command and the different methods to add users in Linux.

Add user Command Syntax 

Add user Command Syntax 

It is very easy to add users in Linux using the “adduser” command syntax. We will understand individual elements of the add user syntax to get detailed knowledge about the process.

adduser [options] username

The syntax consists of various elements, and each element has its significance. Let’s understand them one by one.

  • adduser: This command is the most vital element to add a new user to the Linux system.
  • [options]: The ‘adduser’ command offers optional command-line options that can alter its behavior. These options allow you to configure additional settings for the user you are adding. Some common options include:
  • -c: This option is used to specify the user’s full name.
  • -g: Every new user needs a specific primary group which is set by this option.
  • -s: You need a shell or command-line interface to interact with the computer. This option helps in setting the shell for the new user. 
  • -d: Every user has their home directory, which stores their files, settings, etc. This option helps to set up the home directory for the user. Although, you need to specify the detailed path of the home directory.
  • -m: Similar to the “-d” option, “-m” also creates a home directory for the new user. The only difference is that it does not require extensive details about the path of the home directory.
  • -e: Inactive accounts pose security risks to organizations which is why user accounts have an expiration date. The user account’s expiration date is set using this option.
  • Username: This placeholder is used to add a user; you must provide the desired username you wish to create. Simply replace “username” with the preferred name.

Creating New Users in Linux 

Creating New Users in Linux 

Adding a User in Linux and Creating a Home Directory 

We will use the ‘-M’ option to create a home directory for the new 

user. Look at the command syntax below.

sudo useradd -M <username>
Adding a User in Linux and Creating a Home Directory 

Adding a User with a Specific Home Directory

If the home directory already exists, then you need to use the 

option ‘-d’. On the other hand, if the home directory is not present, 

then you will need to use the option ‘-m’ too. The command syntax 

will be as follows.

sudo useradd -m -d /desired/home/directory username
Adding a User with a Specific Home Directory

Adding a User without Home Directory 

When a user already has a home directory, the option ‘-M’ instructs 

to avoid the creation of a home directory. This option can be 

included in the following way.

sudo useradd -M username
Adding a User without Home Directory 

Adding a User with a Specific User ID 

Each user has a unique ID which helps in identifying the permissions 

and settings of the current user. The tag ‘-u’ is suitable for adding a 

user with a specific UID. Follow the below command syntax.

sudo useradd -u UID username
Adding a User with a Specific User ID 

Adding a User with a Specific Group ID 

To add a user to a particular group, utilize the -g tag while creating 

their profile.

sudo useradd -g <group name or GID> <username>
Adding a User with a Specific Group ID 

Adding a User to Multiple Groups 

To effortlessly add a user to multiple groups, simply utilize the -G 

option and input a list of group names or GIDs separated by 

commas, followed by the username. Allow us to provide you with 

an example:

sudo useradd -G <group1,group2,group3> <username>
Adding a User to Multiple Groups 

Adding a User with a Specific Login Shell 

Upon creating a new user, a default login shell is automatically 

assigned, commonly the Bourne shell or Bourne Again Shell. 

However, if a different shell is desired for the user, confidently use 

the -s tag and provide the path to the desired shell.

sudo useradd -s <shell path> <username>
Adding a User with a Specific Login Shell 

Adding a User with a Specific Comment 

To add a user with a specific comment, execute the following 

command.

sudo useradd -c <comment> <username>
Adding a User with a Specific Comment 

Adding a User with Account Expiry Date 

Add a user with an account expiry date to automatically delete the 

account after the provided date.

sudo useradd -e <YY-MM-D> <username>
Adding a User with Account Expiry Date 

Adding a User with a Deactivation Period 

When you create a password for a user, you can choose how long 

the password will be valid. After that time, the account will be 

deactivated. This gives the user a chance to change the password 

and reactivate the account.

sudo useradd -f <days> <username>
Adding a User with a Deactivation Period 

Adding a System User 

It is common for various programs and systems to generate separate 

user accounts for their specific functions. Such accounts are distinct

from regular user accounts and are crucial for programs like MySQL 

or Tomcat to operate efficiently on the system. Typically, daemons 

are responsible for creating these types of accounts during 

installation. 

sudo useradd -r <username>
Adding a System User 

Adding Multiple Users 

To efficiently add multiple users, the recommended approach is to use a Bash for loop either in a script or directly in the terminal. This will enable you to loop through a list of usernames seamlessly without the need to use the useradd and adduser commands simultaneously.

nano user_list.txt
Adding Multiple Users 
Adding Multiple Users 

Also Read: Ping Command Examples for Linux Users

Conclusion

In Linux, adding users is a critical task for system administrators. Each user has a unique identity, and they can be grouped for easier management of permissions and access to resources.

To create a new user in Linux, you can utilize the useradd command along with various options to customize their settings. Moreover, you can use sudo useradd -r <username> to create a system user account. This account type is specifically used for running particular programs or services on the system.

If you need to add multiple users in Linux, you can use a Bash for loop to loop through a list of usernames and add them all at once. Managing users and groups is a vital aspect of maintaining a secure and organized Linux environment. By appropriately adding users and configuring their settings, system administrators can effectively control access to resources and ensure the smooth operation of the system.

Arpit Saini

He is the Director of Cloud Operations at Serverwala and also follows a passion to break complex tech topics into practical and easy-to-understand articles. He loves to write about Web Hosting, Software, Virtualization, Cloud Computing, and much more.

Related Articles