Users and Groups in Linux πŸ–₯️ - 90 Days of DevOps Challenge (Week-2)

Users and Groups in Linux πŸ–₯️ - 90 Days of DevOps Challenge (Week-2)

Β·

4 min read

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
Β