How to use useradd command in Linux: Creating and Managing User Accounts

Introduction

In Linux, the useradd command is a powerful tool used to create new user accounts. This command allows the creation of new user accounts with specific attributes such as user ID, group ID, login shell, home directory, and others. The useradd command is an essential tool for system administrators in creating new accounts, and it is also used for creating temporary accounts for guest users. In this article, we will discuss how to use the useradd command in Linux.


Step 1: Syntax of the useradd command

The syntax of the useradd command is simple and straightforward. The basic format of the command is as follows:

useradd [options] username

The useradd is the keyword to create a new user account. The options are additional parameters that can be used to specify the attributes of the user account. The username is the name of the user account that will be created.

Step 2: Creating a basic user account

To create a basic user account, we need to use the useradd command with the username of the account we want to create. The basic syntax of the command is as follows:

useradd username

This command will create a new user account with the default attributes such as user ID, group ID, login shell, and home directory. Once the account is created, we need to set the password for the new user account.

Step 3: Setting a password for the new user account

After creating a new user account, we need to set a password for the account. To set a password for the new user account, we can use the passwd command. The basic syntax of the command is as follows:

passwd username

This command will prompt us to enter a new password for the new user account. We need to enter the password twice to confirm it. Once we have set the password, the new user account is ready to use.

Step 4: Specifying user ID and group ID

By default, the useradd command creates a new user account with the next available user ID and group ID. However, we can specify a specific user ID and group ID for the new user account using the -u and -g options respectively. The basic syntax of the command is as follows:

useradd -u user_id -g group_id username

Here, user_id is the user ID we want to assign to the new user account, and group_id is the group ID we want to assign to the new user account. Once we have specified the user ID and group ID, we can set the password for the new user account using the passwd command.

Step 5: Creating a home directory for the new user account

By default, the useradd command creates a new user account with a home directory in the /home directory. However, we can specify a custom home directory for the new user account using the -d option. The basic syntax of the command is as follows:

useradd -d home_directory username

Here, home_directory is the custom home directory we want to assign to the new user account. Once we have specified the custom home directory, we can set the password for the new user account using the passwd command.

Step 6: Assigning a login shell to the new user account

By default, the useradd command assigns the /bin/sh login shell to the new user account. However, we can assign a different login shell to the new user account using the -s option. The basic syntax of the command is as follows:

useradd -s login_shell username

Here, login_shell is the custom login shell we want to assign to the new user account. Once we have specified the custom login shell, we can set the password for the new user account using the passwd command.

Step 7: Adding additional information to the new user account

The useradd command also allows us to add additional information to the new user account. This additional information can include the user's full name, phone number, and email address. We can add this information using the -c option. The basic syntax of the command is as follows:

useradd -c "Full Name" username

Here, Full Name is the full name of the user we want to add to the new user account. Once we have added the additional information, we can set the password for the new user account using the passwd command.

Step 8: Creating a system user account

In Linux, we can create two types of user accounts, regular user accounts and system user accounts. System user accounts are used to run system services and daemons. To create a system user account, we need to use the useradd command with the -r option. The basic syntax of the command is as follows:

useradd -r username

Here, username is the name of the system user account we want to create. Once the system user account is created, we can set the password for the new user account using the passwd command.

Step 9: Adding the new user account to a specific group

In Linux, we can add a new user account to a specific group using the useradd command. To add the new user account to a specific group, we need to use the -G option. The basic syntax of the command is as follows:

useradd -G groupname username

Here, groupname is the name of the group we want to add the new user account to. Once we have added the new user account to the specific group, we can set the password for the new user account using the passwd command.


Conclusion

The useradd command is a powerful tool that is used to create new user accounts in Linux. This command allows us to create new user accounts with specific attributes such as user ID, group ID, login shell, home directory, and others. The useradd command is an essential tool for system administrators in creating new accounts, and it is also used for creating temporary accounts for guest users. By following the steps outlined in this article, you should now have a good understanding of how to use the useradd command in Linux.