You can list all groups a user belongs to in a Linux (or Unix) shell in the following ways:
Using the id
Command
The id
command, when used without any flags, displays the user and its group information by default. However, to get only the groups (without additional user information), you can use the command with -Gn
flags:
id -Gn [user]
Where the "[user]
" is the (optional) username of the user you want to check group information for. When the username is omitted, it defaults to the logged-in user. For example, the following will output the names of the groups the logged-in user is a member of:
id -Gn
Similarly, to only see the groups for a specific user, you can specify the username as an argument to the id
command, for example, like so:
id -Gn your_username
Using the groups
Command
To list all groups a user is a member of, you can use the groups
command. It has the following syntax:
groups [user]
Where "[user]
" can be the username of the user you wish to output the groups for. For example:
groups your_username
If you omit the username, then it will display the groups for the logged-in user.
The man groups
command suggests that the groups
utility has been obsoleted by the id(1)
utility, and is equivalent to the "id -Gn
[user]
" command.
This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.