Introduction
Welcome to the User Account Creation lab. In any multi-user operating system like Linux, managing user accounts is a fundamental administrative task. It involves creating accounts for new users, setting their passwords, granting them appropriate permissions, and removing accounts when they are no longer needed.
In this lab, you will get hands-on experience with the complete lifecycle of a user account. You will learn and use the following essential commands:
useradd: To create a new user account.passwd: To set or change a user's password.usermod: To modify an existing user account, such as adding it to a group.su: To switch to another user account.userdel: To delete a user account.
By the end of this lab, you will be proficient in performing basic user management tasks on a Linux system.
Create User with useradd -m newuser Command
In this step, you will create a new user account named newuser. We will use the useradd command, which is the standard utility for adding users on Linux systems.
The -m option is crucial here. It tells useradd to create the user's home directory, which is typically /home/username. Without this option, the user would be created without a home directory, which can cause issues.
Since creating a user is an administrative action, you must preface the command with sudo. In this LabEx environment, you can use sudo without a password.
Execute the following command in your terminal to create the user:
sudo useradd -m newuser
After the command runs, you won't see any output if it's successful. To verify that the user's home directory was created, you can list the contents of the /home directory:
ls /home
You should see the newuser directory listed in the output, alongside the default labex user's home directory.
labex newuser
Set Password with passwd newuser Command
Now that the newuser account exists, it needs a password. Without a password, the user cannot log in. We will use the passwd command to set one.
Like useradd, setting another user's password requires administrative privileges, so you must use sudo.
Run the following command. You will be prompted to enter and then re-enter the new password. For this lab, you can use a simple password like password. Note that when you type the password, nothing will appear on the screen for security reasons.
sudo passwd newuser
The terminal will prompt you for the password twice. After you've entered it correctly both times, you will see a confirmation message.
New password:
Retype new password:
passwd: password updated successfully
The newuser account is now secured with a password and is ready for login.
Add to Sudo Group with usermod -aG sudo newuser
In this step, you will grant administrative privileges to newuser. On Ubuntu and other Debian-based systems, this is typically done by adding the user to the sudo group. Members of this group can execute commands with sudo.
We'll use the usermod command, which is used to modify a user's account details.
- The
-a(append) option is used to add the user to a group without removing them from other groups. - The
-G(groups) option specifies the group to add the user to, which in this case issudo.
Run the following command to add newuser to the sudo group:
sudo usermod -aG sudo newuser
To verify that newuser is now a member of the sudo group, you can use the groups command:
groups newuser
The output will show all the groups newuser belongs to. You should see sudo in the list.
newuser : newuser sudo
Switch User with su - newuser Command
Now it's time to test the new account. You can switch from your current labex user to newuser using the su (substitute user) command.
The - flag is important. It starts a login shell for the new user, which means the environment will be set up as if newuser had logged in directly. This includes changing the current directory to the user's home directory (/home/newuser) and loading their shell profile.
Run the following command to switch to newuser. You will be prompted for the password you set in Step 2.
su - newuser
After entering the password, your terminal prompt will change to indicate that you are now logged in as newuser (you may see a container ID in the hostname). You can confirm this with the whoami command:
whoami
The output should be:
newuser
To test the sudo privileges you granted, try running a command with sudo. You will be prompted for the user's password:
sudo whoami
After entering the password, this command should output root, confirming that newuser can perform administrative tasks.
[sudo] password for newuser:
root
When you are finished, type exit to return to your original labex user session.
exit
Delete User with userdel -r newuser Command
The final step in the user account lifecycle is deletion. When an account is no longer needed, you should remove it to maintain system security and cleanliness. The userdel command is used for this purpose.
It's good practice to use the -r option with userdel. This option removes the user's home directory and mail spool along with the user account itself. If you omit -r, the user's files will be left behind on the system.
As this is an administrative task, you need to use sudo. Run the following command to completely remove newuser and all associated files:
sudo userdel -r newuser
This command may produce a warning message about the mail spool not being found (this is normal in containerized environments). To verify that the user has been deleted, you can once again check the contents of the /home directory.
ls /home
You will see that the newuser directory is now gone.
labex
This confirms that the user account and its home directory have been successfully removed.
Summary
Congratulations on completing the lab! You have successfully performed the essential tasks of Linux user account management.
In this lab, you have learned how to:
- Create a new user with
useradd -m, ensuring a home directory is also created. - Set a user's password using the
passwdcommand to enable logins. - Grant administrative rights by adding a user to the
sudogroup withusermod -aG. - Switch to another user's session using
su -to test the account and its permissions. - Completely remove a user and their home directory with
userdel -r.
These commands are the foundation of user management on any Linux system. Mastering them is a key step toward becoming a proficient Linux administrator.



