1. Overview

In this tutorial, we’ll learn how to check for user group privileges on a Linux system. We start by defining what user groups are and why they are important. Then, we’ll discuss different ways to check for user group privileges using some command-line tools. Lastly, we will cover how to modify user group permissions using some Linux commands.

2. Checking User Group Privileges

Before we delve into the topic, it’s essential we understand what user groups are and why they play a crucial role in Linux systems. User groups are collections of users who share common permissions to read, write, or execute files, directories, and system resources.

These groups are important for managing user access to different parts of the system. By assigning users to groups with specific privileges, we can control who can access and modify specific files and directories on a Linux system.

3. Using groups

The easiest way we can check for user group privileges on a Linux system is to use command-line tools such as groups. The groups command displays all the groups that a particular user belongs to.

Using the groups command is straightforward. We just need to supply the username:

$ groups john 
john sudo docker

As we can see, the output shows the username john followed by a list of groups the user john belongs to. In this example, the user john belongs to the john, sudo, and docker groups.

For every output of the groups command, the first group listed is the primary group for the user, followed by any additional groups the user belongs to. The primary group is typically the group that was created when the user account was initially set up.

4. Using id

Another command-line tool we can use is the id command. The id command shows both the user’s ID and the groups they are members of:

$ id eric 
uid=1000(eric) gid=1000(eric) groups=1000(eric),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),116(lpadmin),126(sambashare)

Here is what we have:

  • uid=1000 shows the user’s unique identifier (UID)
  • gid=1000 indicates the user’s primary group identifier (GID)
  • groups=1000(eric),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),116(lpadmin) displays all the groups that the user belongs to. Each group is represented by its GID, and its name is shown in parentheses. As we can see, the user eric belongs to several groups, including adm, cdrom, sudo, dip, plugdev, and lpadmin.

Let us note that the output of the id command includes the same information as the groups command. Additionally, the id command includes the user’s UID and GID.

5. Using getent

The getent command retrieves entries from databases supported by the Name Service Switch libraries. We can also use it to check if a user is a member of a specific group by using group followed by the groupname we want to check:

$ getent group groupname

Let us see an output sample:

$ getent group developers 
developers:x:1001:john,eric,sarah

In this example, we used the getent group command to retrieve information about the developers group. We can see the group name – developers, the group ID – 1001, and a comma-separated list of group members – john, eric, sarah.

The x we can see in the second field indicates that the group password is stored in the /etc/gshadow file. This is because groups can have a separate password, which is different from the password of the users who belong to the group.

Lastly, let us note that the output of the getent group command will vary depending on the group name and members of the group checked.

6. Managing User Group Privileges

In addition to managing user access to files and directories, Linux also provides us with a way to manage group privileges. By setting group permissions, we can control what members of a specific group can do with a file or directory.

To set group permissions, we can use the chmod command followed by the group symbol g and the permissions we want to set. For example, let’s add read and write permissions to the group that a file filename belongs to using:

$ sudo chmod g+rw filename

In this command:

  • chmod changes the permissions of a file or directory
  • g stands for group and specifies that the permissions being changed apply to the group that the file or directory belongs to
  • + is used to add permissions
  • r grants read permission, while w grants write permission

Let’s run the command on a file picture.jpg:

$ ls -l picture.jpg 
-rw-r--r-- 1 user user 0 Feb 21 12:00 picture.jpg
$ sudo chmod g+rw picture.jpg 
$ ls -l picture.jpg
-rw-rw-r-- 1 user user 0 Feb 21 12:00 picture.jpg

In this example, we initially view the permission on the file picture.jpg using the ls -l command. We can see that the file owner has read and write permissions (indicated by the rw- characters), while members of the file’s group and other users only have read permission (indicated by the r– characters).

Now, after we run the chmod command and view the permission again, the file picture.jpg now has read and write permissions for the group that the file belongs to in addition to its initial permissions.

Furthermore, we can also use the -R option to set group permissions recursively for all files and directories in a directory. Let’s see an example:

$ sudo chmod -R g+rw mydirectory/ 
$ ls -l 
drwxrwxr-x 2 user1 mygroup 4096 Feb 20 10:22 mydirectory/ 
-rw-rw-r-- 1 user2 mygroup 1024 Feb 20 10:22 mydirectory/myfile1.txt 
-rw-rw-r-- 1 user2 mygroup 2048 Feb 20 10:22 mydirectory/myfile2.txt 

In our output, we can see that the permissions of mydirectory have been modified to allow group members to read and write to the files within it. After running the command, we verify the changes using the ls -l command, which lists the contents of the current directory with additional information, including the owner and group ownership of each file and directory.

The mydirectory folder has the permissions drwxrwxr-x, meaning that the owner and group members have read, write, and execute permissions, while others can only read and execute. The two files within mydirectory have the same permissions with the exception that the owner is user2 and not user1.

7. Conclusion

In this article, we learned about the basics of user groups, why they are important, and different ways to check for user group privileges using command-line tools and system utilities. We also covered some useful commands for managing file and directory permissions, such as chmod.

By assigning users to specific groups with appropriate permissions, we can control access to system resources and ensure that sensitive data remains secure.

Remember, it’s essential to keep our systems secure by regularly reviewing user group memberships, limiting user access to sensitive files, and regularly reviewing file and directory permissions. With these best practices in mind, we can manage user groups and system resources more effectively and ensure our Linux system remains secure.

Comments are closed on this article!