Linux useradd Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to create a new user account, assign a password to the new user, and manage user account properties using the useradd command and related tools on a Linux system. The lab covers the essential steps for user and permission management, including creating a new user, setting a password, and verifying the user's account. The instructions provided in the lab are straightforward and easy to follow, making it a practical learning experience for Linux users.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux/UserandGroupManagementGroup -.-> linux/whoami("`User Identifying`") linux/UserandGroupManagementGroup -.-> linux/id("`User/Group ID Displaying`") linux/UserandGroupManagementGroup -.-> linux/useradd("`User Adding`") linux/UserandGroupManagementGroup -.-> linux/usermod("`User Modifying`") linux/UserandGroupManagementGroup -.-> linux/passwd("`Password Changing`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/UserandGroupManagementGroup -.-> linux/su("`User Switching`") subgraph Lab Skills linux/whoami -.-> lab-422985{{"`Linux useradd Command with Practical Examples`"}} linux/id -.-> lab-422985{{"`Linux useradd Command with Practical Examples`"}} linux/useradd -.-> lab-422985{{"`Linux useradd Command with Practical Examples`"}} linux/usermod -.-> lab-422985{{"`Linux useradd Command with Practical Examples`"}} linux/passwd -.-> lab-422985{{"`Linux useradd Command with Practical Examples`"}} linux/sudo -.-> lab-422985{{"`Linux useradd Command with Practical Examples`"}} linux/su -.-> lab-422985{{"`Linux useradd Command with Practical Examples`"}} end

Create a New User Account

In this step, you will learn how to create a new user account on your Linux system using the useradd command.

First, let's create a new user named newuser using the following command:

sudo useradd -m newuser

The -m option ensures that a home directory is created for the new user.

Example output:

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

To verify that the user was created, you can use the id command:

id newuser

Example output:

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

The output shows that the new user newuser has been created with a unique user ID (UID) of 1001 and a primary group ID (GID) of 1001.

Next, 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.

Now, you are logged in as the newuser user. You can verify this by running the whoami command:

whoami

Example output:

newuser

Great! You have successfully created a new user account on your Linux system.

Assign a Password to the New User

In this step, you will learn how to assign a password to the new user account you created in the previous step.

First, switch back to the labex user, who has sudo privileges:

exit

Now, use the passwd command to set a password for the newuser account:

sudo passwd newuser

You will be prompted to enter and confirm the new password for the newuser account.

Example output:

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

To verify that the password has been set, you can try to switch to the newuser account using the su command:

su - newuser

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

Example output:

Password:
[newuser@labex-host ~]$

Great! You have successfully assigned a password to the new user account.

Manage User Account Properties

In this step, you will learn how to manage various properties of a user account, such as the user's home directory, login shell, and user ID (UID).

First, let's check the current 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 used to modify user account properties. Let's go through the options used in the above commands:

  • -L: Locks the user's password, preventing the user from logging in.
  • -c "New User": Sets the user's comment field (usually the user's full name).
  • -d /home/newuser: Sets the user's home directory to /home/newuser.
  • -s /bin/bash: Sets the user's login shell to /bin/bash.

Now, let's verify the changes:

id newuser

Example output:

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

The output shows that the user's properties have been updated as expected.

Next, let's unlock the user's password:

sudo usermod -U newuser

The -U option unlocks the user's password, allowing the user to log in again.

Great! You have successfully managed various properties of the newuser account.

Summary

In this lab, you learned how to create a new user account on your Linux system using the useradd command, and how to assign a password to the new user account using the passwd command. You also learned how to switch to the new user's context using the su command, and how to verify the user's identity using the whoami command. Finally, you learned how to switch back to the user with sudo privileges and set a password for the new user account.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like