Managing Users and Groups in Linux
Practical Linux Guide to Manage Users and Groups
Table of contents
In the vast world of Linux, one of the fundamental aspects of system administration is managing users and groups. Understanding how to create, modify, and delete user accounts and groups is crucial for maintaining a secure and well-structured environment. In this article, I will explore the essentials of user and group management in Linux, providing you with practical knowledge and tips to help you navigate this vital aspect of system administration.
Managing Users
Creating Users
To create a new user account in Linux, we have a trusty command called useradd
To add a user named adil to the system, open the terminal and type:
sudo useradd adil
By default, this command creates a user with the given name and also creates a primary group with the same name as the user ( in our case adil ).
Once the account is created, you can set the user's password using the passwd
command:
sudo passwd adil
You can list available users in your system by reading the /etc/passwd
file. This file is like the database of Linux users :
In the above screenshot, I have limited the output to show only the last 10 lines using the tail
command.
Modifying Users
To modify user account properties, such as the home directory, login shell, or full name, we can use the usermod
command. Let's say you want to change adil's home directory to '/home/devopsisfun' and set its login shell to /bin/bash. Here's how you can do it:
sudo usermod -d /hom/devopsisfun -s /bin/bash adil
$HOME is an environmental variable that stores the current user's home directory location, so I showed you the before and after proof of the above command.
The reason why I switched to ec2-user is that the user adil doesn't have sudo privileges so he cannot execute the command. I'll explain more about root user and sudo privileges in another article.
You can use the usermod --help
command to check available options for modifying user details.
With the head
command, I limited the output to show only the first 10 lines.
Deleting Users
When a user account is no longer needed, it is essential to remove it from the system to maintain good user management practices. The command to delete a user account is userdel
For example, to delete adil's account, you would run:
sudo userdel adil
As you can see the user adil is deleted but it's home directory is still there. To delete the user and his directory at the same time use the userdel
command with -r
option:
sudo userdel -r adil
Okay so first let's add again user adil. And it's home directory would be default ( /home/adil ).
Make sure you don't accidentally delete the root user ๐ .
Managing Groups
Creating Groups
Groups play a vital role in Linux, allowing users with similar permissions or responsibilities to be organized together. Creating a group can be accomplished using the groupadd
command. For instance, to create a group named devopsisfungrp use the following command:
sudo groupadd devopsisfungrp
You can list all available groups by reading the /etc/groups file.
Adding and Removing Users from Groups
Once a group is created, you can add users to it using the usermod
command with the '-aG' flag. Let's assume we want to add adil to the 'devopsisfungrp' group:
sudo usermod -aG devopsisfungrp adil
We can see that devopsisfungrp has only one user that is adil. We can add multiple users to a group and also a user to multiple groups. So it is an N*N relationship.
Now to remove a user from a group, we use the gpasswd
command. For example, to remove adil from the 'devopsisfungrp' group:
sudo gpasswd -d adil devopsisfungrp
You can also see available options in gpasswd
command with the --help option.
You can delete a group with groupdel
command:
sudo groupdel devopsisfungrp
Conclusion
Effectively managing users and groups is essential for maintaining a secure and organized Linux system. With the commands and techniques shared in this article, you now have a solid foundation for user and group management in Linux. Whether you're a system administrator or a curious Linux enthusiast, mastering these concepts will help you maintain a well-structured and controlled environment. Remember to explore further and refer to the official documentation for more advanced features and possibilities. Happy system administration! ๐
Thanks for reading.....