Linux list group members

Linux Show All Members of a Group Command

Linux Show All Members of a Group Commands

    Tutorial details
    Difficulty level Easy
    Root privileges Yes
    Requirements None
    Est. reading time 1m
  1. /etc/group file – User group file
  2. members command – List members of a group
  3. lid command (or libuser-lid on newer Linux distros) – List user’s groups or group’s users

There are two types of groups in Linux:

  • Primary group – is the main group that is associated with user account. Each user is a member of exactly one primary group.
  • Secondary group – used to provide additional rights to user. For example, access to the dvd/cdrom drive can be granted with help of cdrom group.

Linux: List all members of a group using /etc/group file

Use the grep command or cat command/more command as follows:
$ grep ‘grpup-name-here’ /etc/group
$ grep ‘ftponly’ /etc/group
$ cat /etc/group
$ less /etc/group
$ grep -i —color ‘ftponly’ /etc/group

We can also type the compgen command or getend command to list all group names on Linux:
$ compgen -g
$ getent group
To get just a list of all members of a group called ftponly , type the following awk command:

Display group memberships for each Linux user

Want to see group memberships for each given USERNAME under Linux? The syntax is as follows for the groups command:
groups
groups
groups vivek
The following outputs indicates that the user named ‘vivek’ is part of four groups including ‘vivek’ primary group:

Linux List all members of a group using members command

Warning: members command is not installed on most Linux distros. Use yum command or apt-get command/apt command to install the same:
$ sudo apt-get install members

To outputs members of a group called ftponly, enter:
$ members
$ members ftponly

Fig. 01: members command in action to list members in a group

How to list all users in a Linux group using lid command

You can displays information about groups containing user name, or users contained in group name using lid command as follows.

Warning: lid command is not installed on most distros. Use yum command or apt-get command to install the same:
$ sudo apt-get install libuser

To see users contained in group named ‘ftponly’:

# lid -g ftponly
Please note that newer version of libuser renamed the lid command to libuser-lid . Thus, use it as follows:
$ sudo libuser-lid -g ftponly
Sample outputs:

To show information about groups containing user named ‘nixcraft’:

Use lid command or libuser-lid command on Linux to show all members of a group named nixcraft:
# lid nixcraft
OR
$ sudo libuser-lid nixcraft
Sample outputs:

See lid command man page for more information.

How to list groups in Linux

To see all users, run less command/more command:
less /etc/group
OR
more /etc/group
Another option is to type the following getent command:
getent group
For example, locate the members of a group with the name vboxusers, run:
getent group vboxusers
Sample outputs indicating vivek and raj users are part of vboxusers group:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Finally, you can use the id command to display real and effective user and group IDs:
id
id vivek
id -nG raj # show all group IDs for raj user
id -ng raj # show only effective group ID for raj user

Conclusion

Now you know how to use various Linux commands to show all members of a group. I suggest you read the man pages for more info by typing the following man command:
$ man libuser-lid
$ man members

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

How to List All Users of a Group in Linux

I have already shown you to list all the users in Linux command line. This quick tip is related and yet different from that.

I presume that you are a bit familiar with the concept of groups and users in Linux. There are several groups and a variety of users in a Linux system. A group can have multiple members while a user can be a member of several groups.

You can check which groups a particular user belongs to and you can also find all the users of a group.

List all users of a group in Linux

In this quick tutorial, I’ll show you different ways to list users in a group in Linux command line.

1. List members of a group in Linux using /etc/group file

The group information is contained in the file /etc/group. You can view the content of this file and look for the information about the members.

Normally, this file has entry in the following format:

Here’s the explanation of the fields:

  • adm is the group name
  • x represents password field (you won’t see password in clear text of course)
  • 4 is the Group ID aka GID
  • syslog and abhishek are the users belonging to the group adm

If you find manual searching for a group in the file difficult, you can use a combination of the grep command and the cut command.

The above command looks for all the lines starting with the specified group name and then the cut command extract the fourth column separated with : delimiter. The result is just the name of the group members.

2. List group members in Linux with getent command

getent is a multipurpose command that is used to query from database files in the /etc directory. So you can use it to query the /etc/group file and get the users of the specified group in the following manner:

This will display the line matching the group name and in here you can see the members of the group:

3. List users in a group using ‘members’ command

There is a tiny command line tool that simplifies the process of listing all the members of a specific group.

The members command is usually not installed in all the systems so you have to install it on your own.

On Debian/Ubuntu based systems, you can install it using the following command:

If the command is not found in Ubuntu, you should enable the universe repository and try it again.

Once you have the command installed, you can run it like this:

For example, if you want to check which users have sudo access, you can use the members command like this:

And the output will list all the users of the sudo group.

That’s it…

See, it was absolutely simple to get the users belonging to a group. You learned three ways to do it.

Which method did you like the most? Or, do you use some other way to list group members in Linux? Why not share it with us here?

Источник

How To List The Members Of A Group In Linux

All users in a Linux system must be a member of at least one group. This group is known as Primary group. If an user doesn’t have a primary group, he/she can’t able to login. Apart from the primary group, the users can be a member of additional groups as well. The primary group setting is stored in «/etc/passwd file». The primary group’s name is specified in the 4th field of this file. The settings of other (secondary) groups are stored in «/etc/group» file. This tutorial explains different ways to find and list all groups and list the members of a group in Linux and Unix-like operating systems.

List all groups in Linux

Before listing all users belongs to a group, let us first find the list of available groups using «compgen» command. Compgen is BASH built-in to manipulate the programmable completion facilities.

To list all available groups in a Linux system, run «compgen» command with -g option like below:

Sample output:

List all groups in Linux using compgen command

You can also get the list of all groups using «getent» command:

Sample output:

List all groups in Linux using getent command

The first field in the «/etc/group» file is the name of the group. So, we can use «awk» or «cut» commands to print only the first field that contains the group’s name like below:

If you want to view the groups page by page, use «more» or «less» command like below:

Now, let us check the members of a group.

List the members of a Group in Linux

There are a few ways to find the group members in Linux. The methods we used here to identify the members of a group are given below:

  1. using «/etc/group» file,
  2. using «getent» command,
  3. using «groupmems» command,
  4. using «members» command,
  5. using «lid» command.

1. List the members of a group using /etc/group file

When a group is created, the group’s information is stored in the «/etc/group» file. Let us take a look at the contents of this file using «cat» command:

Sample output:

View Linux groups information

As I mentioned earlier, the first field in «/etc/group» is reserved for the name of the group. As you can see in the above output, a Linux system may contain several groups.

To view the members of a specific Group in a Linux machine, use «grep» command to filter the group details from the «/etc/group» file like below:

The above commands displays the users belongs to the group named «sudo».

List the members of a Group in Linux using grep command

As you see in the above output, the «sudo» group contains only one member named «sk».

2. View the members of a group using «getent» command

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

To find the members of a given group in Linux using «getent» command, run:

This command displays members of the «sudo» group.

Find the members of a group in Linux using getent command

One notable advantage of getent command is it not only lists the local users but all users in all configured userdb backends, for example LDAP, on a given system.

3. Print users in a group using «groupmems» command

The «groupmems» command allows a user to administer their own group membership list without superuser privileges. It is part of the shadow utils package.

To print the members of a group using «groupmems» command, run:

Print the members of a group using groupmems command in Linux

This command has two drawbacks. It only deals with groups in «/etc/group» file but not the ones in LDAP or other user databases. It also requires superuser privileges as it tries to open «/etc/gshadow» file.

4. Display group members using «members» command

As the name explicitly says, the «members» commands basically displays the members of a Group in Linux. The «members» command is not available by default in most Linux distributions. You need to install it using your distribution’s package manager. For example, on Debian-based systems, you can install it using command:

Once installed, you can display the members of a given group in Linux using «members» command like below:

Display group members using members command in Linux

By default, the «members» command displays all users. Use -p or -s to display only the members of a primary group or secondary group.

5. List all users belongs to a group using «libuser-lid» command

The «libuser-lid» command used to displays information about groups containing user name, or users contained in group name.

The «libuser-lid» command is also not available by default in many Linux distributions. The «libuser» provides this command, so you need to install it using your distribution’s package manager. For example, on Debian, Ubuntu, install «libuser» package using command:

To list all users in a group, run:

Please note that this command requires superuser privileges to run.

Sample output:

List all users in a group in Linux using libuser-lid command

For more details, refer the manual pages of the respective command.

That’s all for now. These are a few different methods to find the list of users in a Group in Linux. Hope this helps.

Источник

Читайте также:  Ufs explorer mac os
Оцените статью