DEV Community

Cover image for Linux Series: Understanding Groups in Ubuntu
Adams Adebayo
Adams Adebayo

Posted on • Originally published at olodocoder.hashnode.dev

Linux Series: Understanding Groups in Ubuntu

Every goal of every organization has to do with growth in one way or another, which is apparent in the design of Linux with the addition of groups. In fact, an organization is defined as "an organized group of people with a particular purpose..." see what I did there? 😉.

In this article, you will learn everything you need to know to start managing groups on your Linux servers. In the next section, we will start by explaining what groups are in Linux.

For this article, I have set up an Ubuntu desktop virtual machine on my system. However, you should be able to follow along with any Linux system you have, either virtual or physical.

Groups

A group in Linux is a directory of users accessing specific resources on your server. It is used to efficiently control users' access to resources while avoiding confusion. Every file or directory in Linux has a user and a group that owns it.

Listing Groups

You can use the groups command to list all the groups you have access to like so:

groups
Enter fullscreen mode Exit fullscreen mode

You can access the groups that a particular user has access to by adding the username after the command like so:

groups kunlea
Enter fullscreen mode Exit fullscreen mode

kunlea groups

Yes, kunlea has a group called kunlea. That was not a mistake. By default, every user on the server is added to the users groups.

Accessing all Groups

You can access all the groups on the server by reading the /etc/group file like so:

cat /etc/group
Enter fullscreen mode Exit fullscreen mode

The command above should output a long list of groups like so:

cat group

Again, the user names you see are actually groups. Each line in the result above has multiple columns separated by a colon :. Let's explore what each column (of the user we created in the previous section) means.

  1. The first column is the group name
  2. The second column is the password represented by an x for security reasons. (Using group passwords is NOT recommended)
  3. The third column is the user ID (GID).
  4. The fourth column is a comma-separated list of users of each group. This can be seen in the third entry in the image above, where adams is a member of the lpadmin group.

Note: Most of the groups didn't have members listed because each user is a member of his or her own group.

Creating Groups

Creating a group is relatively straightforward in Linux with the groupadd command like so:

sudo groupadd employees
Enter fullscreen mode Exit fullscreen mode

The command above will create a group with no user, and you can confirm the creation with this command:

cat /etc/group | grep employees
Enter fullscreen mode Exit fullscreen mode

The command above should return the following:

group search

Adding Users to Groups

You can easily add users into groups with the usermod command like so:

sudo usermod -aG employees kunlea
Enter fullscreen mode Exit fullscreen mode

The command above uses the a flag to append the new group instead of replacing all the groups with the employees group. It also uses the G flag to specify the new group as a secondary group. The employees group should now have one member, kunlea like so:

group members

Note: If you want to make the new group the user's primary group, change the G to a lowercase g.

Removing Users from a Group

Ubuntu provides a gpasswd command that you can use to remove users from groups like so:

sudo gpasswd -d kunlea employees
Enter fullscreen mode Exit fullscreen mode

The command above should remove kunlea from the employees group:

remove users

Deleting Groups

Deleting groups in Linux is also relatively straightforward with the groupdel command:

sudo groupdel employees
Enter fullscreen mode Exit fullscreen mode

The command above will delete the employees group from your server.

Conclusion

And that's it! You just learned how to manage groups on your Linux server. You learned how to create groups, delete groups, add users to groups, and much more.

Please feel free to leave a comment to correct, suggest, or even teach me something new! 😉

Finally, remember to follow me here on Dev, LinkedIn, and Twitter. Thank you so much for reading, and I'll see you in the next one!

Top comments (0)