Inspect Group and User Memberships with grep and groups
In this step, you'll learn more efficient ways to inspect a user's group memberships. While we have already used grep on the /etc/group file to see the members of a specific group, there are more direct methods to view all the groups a particular user belongs to. This is a common task for system administrators to verify permissions and configurations.
First, let's use grep again, but this time to find every secondary group the user labex is a member of. By searching for the username in the /etc/group file, you can see all the group entries where labex is listed as a member.
Execute this command in your terminal:
grep labex /etc/group
The output will show every line in /etc/group that contains the string "labex". This will include the research group we added the user to, as well as any other default secondary groups. Your output may include additional groups depending on your system configuration.
sudo:x:27:labex
ssl-cert:x:121:labex
labex:x:5000:
public:x:5002:labex
research:x:5003:labex
While this works, a more direct and user-friendly command for this task is groups. This command is specifically designed to list all the groups (both primary and secondary) for a given user.
To see all the groups the labex user belongs to, run the following command:
groups labex
This command provides a clean, one-line summary of the user's group affiliations.
labex : labex sudo ssl-cert public research
In this output, the name before the colon (labex) is the user being queried. The list after the colon shows all the groups. The first group in the list (labex) is the user's primary group. All subsequent groups (sudo, ssl-cert, public, research) are the secondary groups. This command is often the quickest way to get a complete picture of a user's group memberships.