Users and Groups in Linux ๐ฅ๏ธ - 90 Days of DevOps Challenge (Week-2)

My academic journey is primarily driven by my passion for DevOps knowledge.
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 jhoncenaAfter execution, check the
/homedirectory. Youโll notice thatwhoaveshdoesnโt have a directory because we didnโt use the-mflag, whilejhoncenadoes. ๐To create a user with a specific shell, use:
sudo useradd -m -s /bin/bash undertakerThis will create a user
undertakerwith the Bash shell. By default, users are assigned theshshell 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 namedundertakerwas 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


