Bird
Raised Fist0
Linux CLIscripting~5 mins

useradd and userdel in Linux CLI - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes you need to add or remove users on a Linux system to control who can log in and use the computer. The commands useradd and userdel help you create new user accounts and delete existing ones easily from the command line.
When you want to create a new user account for a friend or colleague to use the computer.
When you need to remove a user who no longer needs access to the system.
When setting up a server and you want to add users with specific permissions.
When cleaning up old user accounts to keep the system secure.
When automating user management in scripts for multiple machines.
Commands
This command creates a new user named alice on the system. We use sudo to run it with the necessary permissions.
Terminal
sudo useradd alice
Expected OutputExpected
No output (command runs silently)
This command sets a password for the new user alice so she can log in.
Terminal
sudo passwd alice
Expected OutputExpected
Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully
This command checks the details of the user alice to confirm she was created correctly.
Terminal
id alice
Expected OutputExpected
uid=1001(alice) gid=1001(alice) groups=1001(alice)
This command deletes the user alice from the system. It removes the user account but leaves the home directory by default.
Terminal
sudo userdel alice
Expected OutputExpected
No output (command runs silently)
This command checks if the user alice still exists after deletion. It should show an error.
Terminal
id alice
Expected OutputExpected
id: β€˜alice’: no such user
Key Concept

If you remember nothing else from this pattern, remember: useradd creates a user account, userdel removes it, and you must set a password separately for login access.

Common Mistakes
Running useradd without sudo or root permissions
The command will fail because only administrators can add users.
Always use sudo before useradd or run as root.
Deleting a user with userdel without removing their home directory
The user's files remain on the system, which may cause clutter or security issues.
Use userdel -r to remove the user and their home directory together.
Not setting a password after creating a user
The user cannot log in without a password set.
Run passwd username right after useradd to set a password.
Summary
Use sudo useradd username to create a new user account.
Set the user's password with sudo passwd username for login access.
Remove a user with sudo userdel username, and add -r to delete their home directory too.

Practice

(1/5)
1. What does the useradd command do in Linux?
easy
A. Lists all users on the system
B. Deletes an existing user account
C. Changes the password of a user
D. Creates a new user account

Solution

  1. Step 1: Understand the purpose of useradd

    The useradd command is used to create new user accounts on a Linux system.
  2. Step 2: Differentiate from other commands

    userdel deletes users, passwd changes passwords, and listing users is done by other commands like cat /etc/passwd.
  3. Final Answer:

    Creates a new user account -> Option D
  4. Quick Check:

    useradd = create user [OK]
Hint: Remember: add means create, del means delete [OK]
Common Mistakes:
  • Confusing useradd with userdel
  • Thinking useradd lists users
  • Mixing useradd with password commands
2. Which of the following is the correct syntax to create a user with a home directory?
easy
A. useradd -r username
B. useradd username
C. useradd -m username
D. userdel -m username

Solution

  1. Step 1: Identify the option for creating home directory

    The -m option with useradd creates the user's home directory.
  2. Step 2: Check other options

    -r is for system users or removing files with userdel, so it is incorrect here.
  3. Final Answer:

    useradd -m username -> Option C
  4. Quick Check:

    -m means make home directory [OK]
Hint: Use -m with useradd to create home folder [OK]
Common Mistakes:
  • Using userdel instead of useradd
  • Forgetting the -m option for home directory
  • Using -r option with useradd
3. What will be the result of this command sequence?
useradd -m alice
userdel alice
medium
A. User alice is deleted but home directory remains
B. User alice and home directory are deleted
C. User alice is created but deletion fails
D. Syntax error occurs

Solution

  1. Step 1: Create user alice with home directory

    useradd -m alice creates user alice and a home folder.
  2. Step 2: Delete user alice without -r option

    userdel alice deletes the user but does not remove the home directory unless -r is used.
  3. Final Answer:

    User alice is deleted but home directory remains -> Option A
  4. Quick Check:

    userdel without -r keeps files [OK]
Hint: userdel needs -r to remove home directory [OK]
Common Mistakes:
  • Assuming home directory is deleted without -r
  • Thinking userdel fails without -r
  • Confusing useradd options with userdel
4. You run userdel -r bob but get an error saying user bob does not exist. What is the likely cause?
medium
A. The home directory is missing
B. User bob was never created
C. You need to use useradd to delete users
D. The -r option is invalid with userdel

Solution

  1. Step 1: Understand the error message

    The error says user bob does not exist, so the system cannot delete a non-existent user.
  2. Step 2: Check command validity

    userdel -r bob is valid syntax to delete user and home directory, so the error is not due to syntax.
  3. Final Answer:

    User bob was never created -> Option B
  4. Quick Check:

    User must exist before deletion [OK]
Hint: Check if user exists before deleting [OK]
Common Mistakes:
  • Thinking -r is invalid option
  • Trying to delete users with useradd
  • Assuming home directory missing causes error
5. You want to create a script that adds a user with a home directory and then deletes the user along with all their files. Which commands should you use in order?
hard
A. useradd -m username; userdel -r username
B. useradd username; userdel username
C. userdel -r username; useradd -m username
D. userdel username; useradd username

Solution

  1. Step 1: Add user with home directory

    Use useradd -m username to create the user and their home folder.
  2. Step 2: Delete user and all files

    Use userdel -r username to remove the user and their home directory and files.
  3. Final Answer:

    useradd -m username; userdel -r username -> Option A
  4. Quick Check:

    Add with -m, delete with -r for files [OK]
Hint: Add with -m, delete with -r to remove files [OK]
Common Mistakes:
  • Deleting before adding user
  • Forgetting -m when adding user
  • Forgetting -r when deleting user
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard