Linux

Linux Tutorial: Managing Users and Groups

Linux Tutorial Preview

Introduction

In the previous tutorial, we learned that everything in Linux revolves around one thing, FILES. Well, users and groups are the entities responsible for manipulating those files. This tutorial is a basic introduction to managing users and groups through the command line interface (CLI). If your coming from a backend engineering role, you may be familiar with CRUD operations. Well, I have organized this tutorial to show you how to perform CRUD operations on users and groups from the CLI. By the end of this tutorial, you will know how to add new users, manage groups, modify users and groups, and delete users and groups when necessary. In doing so, you'll gain a solid foundation in user and group management, a core aspect of Linux system administration.

Creating Users and Groups

First, let's create a group called web admin.

groupadd web-admin

Then let's create a user named rob. Later in this tutorial we will add that user to the newly created group but for now let's look at the useradd command in detail:

useradd -m rob # --create-home

The useradd command creates a user, and the -m flag will create a home directory for that user. We can look at the home directories for all users in the system in the /home folder.

ls /home
>>> rob

The su command will allow us to switch to the specified user. So, let's switch users to our newly created user.

su rob

We can now use the change directory command to show that the newly created home directory is in fact the users home directory:

cd ~/ && pwd
/home/rob

Now let's try to switch users back to the root user with su root. You will be prompted for a password. Type in a password and you will see that the authentication will fail. This is because we have not set a password for the root user. In the mean time, the exit command will exit out of the current user and return to the root user.

exit
>>>@root...

We can set the password for a user with:

passwd root

You will be prompted to enter your password and retype your password. Once your password is set, you will be free to switch to the root user.

Reading Users and Groups

You can read all of the users on your system by viewing the /etc/group file with the cat command like so:

cat /etc/group

root:x:0:
www-data:x:33:
rob:x:1001:rob

I limited the output to these 3 groups because of their importance; however, your list will be much longer. As you can see, there are 4 strings separated by a semicolon in each line of output. Let's break down what each section means:

  1. Group Name
  2. Password - Usually blank
  3. Group ID (GID)
  4. List of Users in the group

For example, root:x:0: indicates that the group's name is user, the password is not set, the group id is 0, and there are currently no users inside the group.

www-data user group is configured by default on most linux distributions. This user group is meant for users who are managing web servers. For example, if I have deployed a web application with Flask, then I might use www-data to poke around on the server instead of using the root user.

I also wanted to note that by default when you create a user a user group is also created for that user by default. As you can see rob:x:1001:rob indicates their is a group named rob with the user rob added to the group.

Updating

Now, let's update the group we created earlier in this tutorial to include our newly created user, rob. We can do this with the usermod command displayed below:

usermod -G web-admin,rob rob

The usermod command stands for user modification and allows us to modify a given user on our Linux system. The -G flag is expecting a comma delimited list of groups to set on a given user. It's important to note that the -G flag acts as a set operation meaning it will remove the user from any groups not specified in the list as well as add the user to all of the groups present in the list.

How to Delete a User in Linux

Finally, we can remove users with:

userdel rob

By default this will also delete the group associated with that user. Additionally, we can remove a group with:

groupdel web-admin

Conclusion

In this tutorial, we explored the core aspect of Linux system administration: managing users and groups through the CLI. Just like CRUD operations in backend engineering, we demonstrated how to create, read, update, and delete users and groups. By learning how to add new users, manage groups, modify them, and remove them when necessary, you’ve built a solid foundation in user and group management. With this knowledge, you’re now equipped to handle one of the most fundamental responsibilities in Linux!

Previous
Linux Basics
Next
chmod