useradd Command in Linux

Introduction

This lab provides a comprehensive guide on creating and managing user accounts on a Linux system. You'll learn how to utilize the useradd command and associated tools to create new user accounts, assign secure passwords, and effectively manage user account attributes. This tutorial covers crucial aspects of user and permission management, including user creation, password setup, and account verification. The step-by-step instructions are designed to be clear and concise, offering a hands-on learning experience for systemadmin professionals and Linux enthusiasts alike.

Create a New User Account

In this section, we will explore the process of creating a new user account on your Linux system using the useradd command. This is a fundamental task for any systemadmin.

First, let's create a new user with the username newuser. Execute the following command:

sudo useradd -m newuser

The -m option is crucial; it ensures that a home directory is automatically generated for the newly created user. This is important for the user to have a dedicated space for their files and configurations.

Example output:

No output, as the command simply creates the user without any feedback.

To confirm the successful creation of the user account, use the id command:

id newuser

Example output:

uid=1001(newuser) gid=1001(newuser) groups=1001(newuser)

The output confirms the user newuser has been successfully created. The output displays a unique user ID (UID) of 1001 and a primary group ID (GID) of 1001. This information is essential for user identification and permission management within the Linux environment.

To simulate logging in as the new user, you can switch to the new user's context using the su command:

su - newuser

Example output:

No output, as the command simply switches the user context.

You are now operating as the newuser user. Verify this by executing the whoami command:

whoami

Example output:

newuser

Excellent! You have successfully established a new user account on your Linux system. This is a foundational step in systemadmin tasks.

Assign a Password to the New User

In this section, you will learn the critical task of assigning a secure password to the newly created user account.

First, return to the labex user, which possesses sudo privileges. This is necessary for managing user accounts.

exit

Now, use the passwd command to establish a password for the newuser account. Security is paramount in systemadmin, so choose a strong password.

sudo passwd newuser

You will be prompted to provide and confirm the new password for the newuser account. Ensure the passwords match to avoid login issues.

Example output:

Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

To validate that the password has been successfully set, attempt to switch to the newuser account utilizing the su command:

su - newuser

You will be prompted to enter the password you just created. Upon entering the correct password, you should be logged in as the newuser user.

Example output:

Password:
[newuser@labex-host ~]$

Great job! You have successfully secured the new user account by assigning a password. This is a vital security practice for any systemadmin.

Manage User Account Properties

In this section, you will learn to manage a range of user account properties, including the user's home directory location, the default login shell, and the User ID (UID). These properties allow for fine-grained control over user environments.

First, let's examine the existing properties of the newuser account:

sudo usermod -L newuser
sudo usermod -c "New User" newuser
sudo usermod -d /home/newuser newuser
sudo usermod -s /bin/bash newuser

The usermod command is a powerful tool for modifying user account attributes. Let's break down the options employed in the commands above:

  • -L: Locks the user's password, preventing login. This is useful for temporarily disabling accounts.
  • -c "New User": Sets the user's comment field (typically the user's full name or a brief description). This is mainly for administrative purposes.
  • -d /home/newuser: Defines the user's home directory as /home/newuser. This determines where the user's personal files are stored.
  • -s /bin/bash: Assigns the user's login shell to /bin/bash. The login shell is the command interpreter the user interacts with.

Now, let's confirm that the changes have been applied:

id newuser

Example output:

uid=1001(newuser) gid=1001(newuser) groups=1001(newuser) comment=New User

The output validates that the user's properties have been updated as anticipated. The comment field now reflects "New User".

Next, let's unlock the user's password, enabling login again:

sudo usermod -U newuser

The -U option unlocks the user's password, restoring the user's ability to log in.

Congratulations! You have successfully mastered the management of various properties of the newuser account. This is crucial for customizing user environments in systemadmin.

Summary

In this lab, you have acquired the essential skills for user management on a Linux system. You learned how to create a new user account using the useradd command, assign a secure password to the new user account with the passwd command. Furthermore, you gained proficiency in switching to the new user's context with the su command and verifying the user's identity using the whoami command. Lastly, you learned how to revert to the user with sudo privileges and configure a password for the new user account, including how to lock and unlock user accounts. This hands-on experience is invaluable for any aspiring or current systemadmin working with Linux environments.

400+ Linux Commands