Find all groups in linux

How to list all groups a user is a member of

Before delving into the 5 ways, let’s first understand some basics:

Adding a user to an existing group is one of the typical tasks of a Linux administrator.

A group is a collection of users. The main purpose of the group is to define a set of privileges to their members within the group.

It can be a difficult task if you want to assign a set of privileges to multiple users without a group. This is where the group comes in handy.

All system users are listed in the /etc/passwd file, the groups are listed in the /etc/group file, and the actual password is stored in the /etc/shadow file.

No matter what command we use, it will fetch information from these files.

There are two types of groups in Linux:

What is primary group?

The primary group is the main group associated with the user account. Each user must be a member of a single primary group.

What is secondary group?

The secondary or supplementary group is used to grant additional rights to the user. Each user can become a member of multiple secondary groups.

What is /etc/passwd file

“/etc/passwd” is a text file containing every user information that is required to login to the Linux system. It maintains useful information about users such as username, password, user ID, group ID, user information, home directory and shell.

Each user profile in the password file is a single line with seven fields as shown below:

$ grep "daygeek" /etc/passwd daygeek:x:1000:1000:daygeek. /home/daygeek:/bin/bash

What is /etc/group file

“/etc/group” is a text file that defines which groups a user belongs to. We can add multiple users in the same group.

Linux has three permission levels which define how users can access it. These levels are user, group and others, which controls a users access to other users’ files and folders.

/etc/group file maintains useful information about the group such as group name, group password, group ID (GIT) and membership list. Each group details is shown in a single line with four fields as shown in the ‘method #5’ listed below.

Читайте также:  Команда service start linux

The following seven commands will help you find out which groups a user belongs to in Linux.

  • groups: Show All Members of a Group.
  • id: Print user and group information for the specified username.
  • lid or libuser-lid: It display user’s groups or group’s users.
  • getent: Get entries from Name Service Switch libraries.
  • compgen: compgen is bash built-in command and it will show all available commands for the user.
  • members: List members of a group.
  • /etc/group file: Also, we can grep the corresponding user’s groups from the /etc/group file.

Now let’s delve into the 5 methods which can be used to find the list of groups a user is part of in Linux:

Method-1: Using groups command

The ‘groups’ command is widely used by Linux admin to list all groups a user is a member of. It prints the information of the given user’s primary and supplementary groups as shown below:

$ groups daygeek daygeek : daygeek adm cdrom sudo dip plugdev lpadmin sambashare

Run ‘groups’ command without any arguments to display the list of groups associated with the current user as shown below:

$ groups daygeek adm cdrom sudo dip plugdev lpadmin sambashare

Method-2: Using id command

The id command stands for identity. It prints real and effective user, group, and supplementary group information such as username, UID, group names and GUID as shown below:

$ id daygeek uid=1000(daygeek) gid=1000(daygeek) groups=1000(daygeek),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),118(lpadmin),128(sambashare)

Just run the ‘id’ command to view group information about the current user as shown below:

$ id uid=1000(daygeek) gid=1000(daygeek) groups=1000(daygeek),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),118(lpadmin),128(sambashare)

Method-3: Using lid command

The lid or libuser-lid command displays information about groups containing user name, which requires sudo privileges.

You should run the libuser-lid command instead of the lid on newer systems.

$ sudo libuser-lid daygeek adm(gid=4) cdrom(gid=24) sudo(gid=27) dip(gid=30) plugdev(gid=46) lpadmin(gid=116) daygeek(gid=1000) sambashare(gid=126)

Method-4: Using the getent command

The getent command displays entries from databases supported by the Name Service Switch libraries, which are configured in ‘/etc/nsswitch.conf’:

$ getent group | grep daygeek adm:x:4:syslog,daygeek cdrom:x:24:daygeek sudo:x:27:daygeek,2gadmin dip:x:30:daygeek plugdev:x:46:daygeek lpadmin:x:116:daygeek daygeek:x:1000: sambashare:x:126:daygeek

The above command shows the group name and all other members associated with that group. Use the below customized command format to print only groups for a given user:

$ getent group | grep daygeek | awk -F: '' adm cdrom sudo dip plugdev lpadmin daygeek sambashare

Run the below command to print only the primary group information of the user:

$ getent group daygeek daygeek:x:1000:

Method-5: Using the ‘/etc/group’ file

User groups information can be filtered from the ‘/etc/group’ file using grep command as shown below:

$ grep daygeek /etc/group adm:x:4:syslog,daygeek cdrom:x:24:daygeek sudo:x:27:daygeek,2gadmin dip:x:30:daygeek plugdev:x:46:daygeek lpadmin:x:116:daygeek daygeek:x:1000: sambashare:x:126:daygeek

Use the below customized command format to print only groups for a given user:

$ grep daygeek /etc/group | awk -F: '' adm cdrom sudo dip plugdev lpadmin daygeek sambashare

Bonus Tip-1: Find out all groups using compgen command

Compgen is a bash built-in command that displays all groups in the Linux system:

$ compgen -g root daemon bin sys adm . . daygeek thanu renu sudha admin u1 u2

Bonus Tip-2: Listing members of a group using member command

The member command allows you to list members of a group in Linux:

$ members sudo daygeek 2gadmin

Closing Notes

In this guide, we have shown you several commands to list all groups a user is a member of in Linux.

Читайте также:  Delete users arch linux

If you have any questions or feedback, feel free to comment below.

Источник

How do I List All Groups in Linux

Linux systems may have several users that are divided into many groups. These groups are the collection of users with the same set of privileges like reading, writing, or executing permission for a particular file or resources shared among the users of that group. Linux allows you to add a new user or the existing user to the existing group for utilizing the privileges of that particular group that it will grant. We will learn about the various Linux groups and how to list all the members of the group.

Types of Groups in Linux

Linux has two types of groups that contain several users:

  • Primary or Login Group: it is the group associated with the files created by a specific user. The name for that primary group has the same name as the user’s name that will create that specific file. Each user must belong to exactly a single group.
  • Secondary or Supplementary Group: you can use this type of group to grant privileges to a set of users that belong to that group. A user can be assigned to no or more secondary groups.

Listing Users on Linux

For listing all the users present on the Linux system, you can run the cat command on the ‘/etc/passwd” file. This command will help in returning the number of users that are present on the Linux system.

Also, use the “less” or “more” command for navigating within the user’s list.

Listing Users Using the /etc/passwd File

For listing the usernames on the Linux system, you can use the “cat” command and then pipe the output to the “cut” command to isolate the usernames available in the first column in the list. Run the below-mentioned command as shown below.

Listing Usernames Using awk

For listing the usernames on the Linux system, use the “cat” command and then pipe the output to the “awk” command that works similar to the “cat” command.

Here we are using the “awk” interpreter, as shown below.

Listing Usernames Using getent

Use the getent command along with the “passwd” argument for listing the usernames available on Linux. Also, you can mention the optional user that you want to be displayed on the screen.

Читайте также:  Linux vlan интерфейс поднять

The getent command retrieves the entries from the Name Service Switch databases. It is a Unix utility for retrieving entries from various data sources. Check the list of the data sources available from the nsswitch.conf, which is stored at /etc.

If you want to list all the users with the help of the getent function, you can run the following command.

Listing the Connected Users on Your Linux Host

To get the list of the users connected to the Linux system, you can use the following command.

Using this command, you will provide the connected users’ list and the shell they are using.

Also, you can use the “users” command to get the same result as the “who” command, as shown below.

Listing Groups Using /etc/group File

Use the most commonly used “cat” command to get the list of the groups available in the “/etc/group” file. When you run the command, you will get the list of the groups.

But if you are looking for the group names that are present in the “/etc/group” file, use the cat command and then pipe the output to the “cut” command as shown below.

Also, if you want to isolate one group to check what users belong to that group, use the below command.

Listing Groups Using getent

You can use the “getent” command for listing the users on the Linux system.

If you do not provide the key, you will get the entire group file.

Listing Groups for the Current User

Using the “group” command will display a list of groups a specific user is in.

If you do not provide any argument, you will get the list of the groups for the user that runs the command.

Conclusion

The Linux system contains users and groups in different files. Sometimes it becomes important to get the user details and to which group they belong. Thus Linux offers some commands that will help you to achieve that. You can run some commands to get the user details and the group to which they belong. You can also get the complete list of users on the Linux system, active users, and groups names.

You can go through this article to get various commands for getting the list of all the groups in Linux and understand how they work.

About the author

Simran Kaur

Simran works as a technical writer. The graduate in MS Computer Science from the well known CS hub, aka Silicon Valley, is also an editor of the website. She enjoys writing about any tech topic, including programming, algorithms, cloud, data science, and AI. Travelling, sketching, and gardening are the hobbies that interest her.

Источник

Оцените статью
Adblock
detector