How to List Groups on Linux
Introduction
Before we begin talking about how to list groups on Linux, let's briefly understand – What is a Group ?
A group on Linux is a gathering of users. The fundamental goal of groups is to specify a set of privileges for a certain resource that may be shared across the users inside the group, such as read, write, or execute access. To take advantage of the rights granted by an existing group, users can be added to it.
In this tutorial, we will show you how to list groups on Linux. We will also address a few FAQs on how to list groups on Linux.
Advantages of Groups
- Access Control: Groups enable efficient management of user access to files and resources.
- Simplified Permissions: Setting permissions at the group level reduces administrative efforts for granting/restricting access.
- Enhanced Security: Assigning users to appropriate groups improves security by ensuring only authorized access to sensitive data.
- Efficient Collaboration: Groups facilitate teamwork by allowing multiple users to access and work on shared files simultaneously.
- Streamlined Administration: Managing permissions and user accounts becomes easier as group membership simplifies administrative tasks.
Linux Groups
A user can be a member of one of two types of groups:
- The primary group, or login group - is the one that is assigned to the user's files that are created by the user. The primary group's name is usually the same as the user's name. Every user must be a member of only one primary group.
- Secondary or supplementary group - used to grant a set of users certain privileges. A user may belong to 0 or more secondary groups.
List all Groups a User is a Member of
There are several methods for determining which groups a user belongs to.
The /etc/passwd
file contains the primary user's group, and the /etc/group
file contains the supplementary groups, if any.
Using cat, less, or grep to list the contents of those files is one technique to find the user's groups. Another alternative is to use a command that provides information about the users and groups in the system.
Using the groups
command
The groups command is the most memorable command for listing all groups
in which a user is a member. The command will output a list of all groups to which the currently logged-in user belongs if it is run without an argument:
groups
The primary group is the first one.
john adm cdrom sudo dip plugdev lpadmin sambashare
Provide the username as a parameter to the groups command to retrieve a list of all groups
to which a given person belongs:
groups vegastack
The first group, like before, is the primary group.
vegastack : vegastack sudo
Using the id
command
The id
command displays information about a user and their groups. If the username is not specified, the current user's information is displayed.
To acquire information on the user vegastack, for example, type:
id vegastack
The command displays the user ID (uid
), primary group (gid
), and secondary groups for the user (groups
)
Output
uid=1001(vegastack) gid=1001(vegastack) groups=1001(vegastack),27(sudo)
Use the -n
option to print only the names instead of the numbers. The -g
option prints only the primary group, while -G
prints all groups.
The following program prints the names of the groups to which the current user belongs:
id -nG
Output
john adm cdrom sudo dip plugdev lpadmin sambashare
List All Members of a Group
Use the getent group
command followed by the group name to get a list of all members of a group.
For example, if you wanted to find out who was in a group called developers
, you could run the following command:
getent group developers
The command will print the group and all of its members if the group exists:
Output
developers:x:126:frank,mary
If there isn't an output, the group isn't active.
List All Groups
Simply open the /etc/group
file to see all the groups on the system. Each line in this file corresponds to a single group.
less /etc/group
Another approach is to use the getent
command, which displays entries from databases specified in the /etc/nsswitch.conf
file, such as the group
database, which we may query for a list of all groups.
Type the following command to receive a list of all groups:
getent group
The output is the same as when the contents of the /etc/group
file are displayed. getent
will display all groups from both the /etc/group
file and the LDAP database if you are using LDAP for user authentication.
You may also use awk or cut to print just the first field, which contains the group's name:
getent group | awk -F: '{ print $1}'
getent group | cut -d: -f1
FAQs to List Groups on Linux
What is the primary group of a user?
A user's primary group is the group that is assigned to them by default when their user account is created.
How do I list only the group names without additional information?
You can extract just the group names without additional details using the command cut -d: -f1 /etc/group
or getent group | cut -d: -f1
.
How can I filter and list specific groups based on a keyword?
To list specific groups that match a keyword, use the command getent group | grep "<keyword>"
. It will display all groups containing the specified keyword.
Can I list the groups in a sorted order?
Yes, you can list the groups in a sorted order using the command cat /etc/group | sort
or getent group | sort
. It will alphabetically sort and display the group list.
How do I list the primary group of a user?
To find the primary group of a user, run the command id -gn <username>
. It will output the name of the user's primary group.
How can I list the members of a specific group?
To display the members of a particular group, use the command getent group <groupname>
. It will show the group information, including its members.
How can I see the group ID (GID) associated with a group?
To view the Group ID (GID) assigned to a group, use the command getent group <groupname>
. It will display the group information, including its GID.
Conclusion
You learned how to find out which groups a user belongs to in this tutorial. Any Linux distribution, including Ubuntu, CentOS, RHEL, Debian, and Linux Mint, can use the same commands.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.