Skip to main content

Command Palette

Search for a command to run...

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

Updated
โ€ข4 min read
Users and Groups in Linux ๐Ÿ–ฅ๏ธ - 90 Days of DevOps Challenge (Week-2)
A

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:

  1. 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.

  2. 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 that whoavesh doesnโ€™t have a directory because we didnโ€™t use the -m flag, while jhoncena 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 the sh 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 ๐Ÿ”„

  1. 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 named undertaker was also created by default.

  2. 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