As a Linux system administrator, one of your main tasks involves user and group management. In this guide, we’ll explain how to add, modify, delete, and manage users and groups using commands such as useradd, usermod, userdel, groupadd, and groupdel.
Understanding User and Group Concepts
Every user on a Linux system has a unique user ID and belongs to at least one group. User information is stored in the /etc/passwd file, while group information is in the /etc/group file. Groups, denoted by unique group IDs, allow users with the same computing needs to be categorized for easier permission management.
Adding a User
Adding a user in Linux involves the useradd command. The syntax is straightforward – useradd followed by the username. For example, to add a user called ‘purevoltage’, you’d run:
sudo useradd purevoltage
You can also specify additional options such as the home directory (-d), login shell (-s), and more.
Modifying a User
Modifying user details can be achieved using the usermod command. For instance, to add a user to another group, you use the -aG options, which stands for append to group:
sudo usermod -aG sudo purevoltage
This command adds ‘purevoltage’ to the ‘sudo’ group, thus granting sudo privileges.
Deleting a User
The userdel command is used to remove a user:
sudo userdel purevoltage
By default, this doesn’t remove the user’s home directory. If you wish to delete it as well, use the -r option:
sudo userdel -r purevoltage
Adding a Group
Creating a new group involves the groupadd command. For example, to create a group named ‘developers’:
sudo groupadd developers
Deleting a Group
To delete a group, utilize the groupdel command:
sudo groupdel developers
This will remove the ‘developers’ group.
Understanding /etc/passwd and /etc/group
User and group information is stored in the /etc/passwd and /etc/group files respectively. You can view these files with a simple cat command:
Remember, effective user and group management is crucial in maintaining a secure and efficient Linux system. Keep practicing these commands to enhance your skillset. For more Linux essentials, visit our Linux Basics series.