0
0
Linux CLIscripting~5 mins

useradd and userdel in Linux CLI - Commands & Configuration

Choose your learning style9 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.