If youβve ever used Windows, you might have created separate user accounts, like one for your annoying younger sibling, to prevent them from messing with your files. Similarly, in Linux, we can create users, but instead of using a graphical user interface (GUI), we do it via the command line interface (CLI). As a DevOps engineer, youβll primarily be working with CLI-based operating systems in your daily tasks, so mastering this is essential. π
Letβs dive into creating users and groups in Linux. If you donβt have a Linux machine ready for experimentation, refer to my previous blog on AWS Security Groups to set up an EC2 instance. Alternatively, you can use an online Linux terminal to practice for free. π
What is a User in Linux? π€
A user in Linux is an entity with certain privileges to perform specific tasks within the operating system.
Think of it like employees in a company. Some developers may only have access to files related to their projects, while a manager might have access to all project files and resources. π’
Types of Users in Linux π₯
There are two types of users in Linux:
Regular User: Can perform tasks like reading and executing files and programs in their home directory. They cannot modify system files unless they have sudo (superuser) permissions.
Superuser (Root User): Has administrative privileges, including creating and deleting users, installing or removing packages, and modifying system files. The root user can perform any task on the system. π₯
Creating Users in Linux π€
To create a user in Linux, use the following command:
sudo useradd whoavesh
This will create a user named whoavesh
on your Linux machine. Remember to use sudo
, or else you wonβt be able to create a user.
To set a password for the newly created user, run:
sudo passwd whoavesh
Useful Flags for useradd
Command π
If you want a home directory to be created automatically in
/home
, use:sudo useradd -m jhoncena
After execution, check the
/home
directory. Youβll notice thatwhoavesh
doesnβt have a directory because we didnβt use the-m
flag, whilejhoncena
does. πTo create a user with a specific shell, use:
sudo useradd -m -s /bin/bash undertaker
This will create a user
undertaker
with the Bash shell. By default, users are assigned thesh
shell unless specified otherwise. π₯οΈ
Deleting a User β
To delete a user along with their home directory, use:
sudo userdel -r jhoncena
Listing All Users π
To see a list of all users on the system, execute:
cat /etc/passwd
Understanding Groups in Linux π·οΈ
What are groups in Linux? Are they like WhatsApp groups where we share memes? Not quite (or maybe! π).
A group in Linux is a collection of users. Instead of assigning permissions to individual users one by one, you can assign them to a group and then add users to that group.
Types of Groups in Linux π
Primary Group: Every user has a primary group, which is created automatically with the same name as the username. For example, when we created the user
undertaker
, a group namedundertaker
was also created by default.Secondary Groups: Users can be part of additional groups apart from their primary group.
Using groups simplifies system administration. For instance, if you have ten developers working on a new web application and need to grant them read and write permissions, instead of assigning permissions one by one, you can create a group, set permissions for that group, and then add all ten developers to it. π¨βπ»π©βπ»
Creating a Group π οΈ
To create a new group, run:
sudo groupadd p1websiteAssets
Adding Users to a Group β
To add whoavesh
and undertaker
to the p1websiteAssets
group, execute:
sudo usermod -aG p1websiteAssets whoavesh
sudo usermod -aG p1websiteAssets undertaker
Here, -aG
means "append to the group."
Removing a User from a Group β
Letβs say undertaker
got a higher package at WWE and left the company. To remove them from the p1websiteAssets
group, run:
sudo gpasswd -d undertaker p1websiteAssets
Listing All Groups π
To view all groups on the system, execute:
cat /etc/group