Beginners Guide for Groups Command in Linux

Linux TLDR
Last Updated:
Reading time: 2 minutes

The Linux system consists of multiple users and groups; there might be a large number of users in your system that can be easily managed by adding them to a specific group.

After adding them, you can easily assign different permissions and policies to that specific group of users and remove them when they are not required.

A good example is sudo. If you are not part of the sudo group, you will not be able to perform any system level changes similar to that.

You might be part of many other groups that you can list using the groups command.

Tutorial Details

DescriptionGroups
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitesgroups
Internet RequiredNo

Syntax of the Groups Command

The “groups” command takes one argument asking for a username, as shown.

$ groups [USERNAME]

Although you can specify multiple usernames by using the space as a separator.

Listing All Groups to Which a Specified User Belongs

Specify the “username” with the groups command to list all the groups to which the specified user belongs.

$ groups linuxtldr

Output:

Listing user linked to which group using groups command

If you do not specify the username“, the groups command will show the result for the currently logged-in user.

Display the Current User’s UID, GID, and Groups to Which They Belong

The id command is used to display information about the current user’s ID (UID), group ID (GID), and groups to which they belong.

It is followed by the type with an “=” sign, then the user or group ID, and the username.

$ id

Output:

Listing user linked to which group using groups command

Getting a Username Without a User ID

The “-n” and “-G” flags can be used with the “id” command to display only the name instead of a number.

$ id -nG

Output:

Printing only the group label using id command

Listing All Groups in Your System

Whenever you create a new group or record for existing groups is recorded at “/etc/group” file that you can read using the cat command.

$ cat /etc/group

Output:

Checking all group by reading the content of etc group file

Alternatively, you can use the getent command to get the same output as above.

$ getent group

Output:

Checking all group using the getent command

That was the end of the article.

If you have any thoughts, feel free to exchange them in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.