
{{ $('Map tags to IDs').item.json.title }}
How to Manage Users with useradd and userdel
Managing users in a Linux system is a critical task for system administrators. The useradd
and userdel
commands allow you to create and remove user accounts effectively. This tutorial will guide you through using these commands for user management.
1. Creating a New User with useradd
The useradd
command is used to create new user accounts. The basic syntax is:
sudo useradd [options] username
For example, to create a new user named john
, you would run:
sudo useradd john
This command creates a user john
without a home directory or specified shell.
1.1. Setting Home Directory and Shell
You can specify a home directory and shell during user creation:
sudo useradd -m -d /home/john -s /bin/bash john
In this command:
- -m: Creates the user’s home directory if it does not exist.
- -d: Sets the home directory.
- -s: Specifies the login shell.
2. Setting User Password
After creating a user, set a password with:
sudo passwd john
You’ll be prompted to enter and confirm the new password for the user.
3. Viewing User Information
To view details about a user, you can use:
id john
This command displays the user’s UID, GID, and groups they belong to.
4. Removing a User with userdel
The userdel
command is used to remove user accounts. The basic syntax is:
sudo userdel username
For example, to remove the user john
, run:
sudo userdel john
This removes the user but does not delete their home directory.
4.1. Removing a User and Their Home Directory
To delete a user along with their home directory and mail spool, use the -r
option:
sudo userdel -r john
This command ensures that all files related to the user are also removed.
5. Conclusion
By following this tutorial, you have learned how to manage users on a Linux system using the useradd
and userdel
commands. Proper user management is essential for maintaining system security and user access. Continue exploring additional options and commands for comprehensive user management to optimize your Linux system administration!