[Linux] Displaying User Group Memberships with the groups Command

目次

Overview

The groups command is used to display all group names to which a specific user or the current user belongs. It is primarily used to verify if a user has been correctly added to a group when troubleshooting access permission issues on file servers or when specific privileges, such as docker or sudo, are not working as expected.

Specifications (Arguments and Options)

Syntax

groups [username]

Note: Aside from displaying version information, this command essentially has no options.

Basic Usage

Displaying Your Own Groups

When executed without arguments, the command displays all groups for the currently logged-in user (e.g., “mori”). The first group listed is the primary group.

BASH

# Display groups for the current user (mori)
groups

Example Output

mori wheel docker

Displaying Groups for a Specific User

By specifying a username as an argument, you can check the group memberships of another user.

BASH

# Check groups for user mori
groups mori

Example Output

mori : mori wheel docker

Practical Commands

Using the id Command for Scripts

While the groups command formats output for human readability, the id -Gn command is more suitable if you need to extract just the group names for script processing.

BASH

# Display only group names (-n) for all groups (-G) using id
id -Gn mori

Example Output

mori wheel docker

Checking Behavior When Using sudo

If you run groups with sudo and no arguments, it displays the groups of the user executing the command (which defaults to root), not the original user. To check the groups of a standard user like “mori,” you must either run the command without sudo or explicitly specify the username as an argument.

BASH

# Execute via sudo (displays root's groups)
sudo groups

Example Output

root

Summary

The groups command is the most fundamental tool for verifying current user privileges. If you encounter issues where a user cannot perform an action despite seemingly being added to the correct group, execute this command first to verify their status. Please note that if you have just added a user to a group (e.g., using gpasswd -a), the output of this command will not update until the user logs out and logs back in to refresh their session information.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次