Linux Show All Members of a Group Command

last updated in Categories , , , , , ,

I am a new Linux user and created a couple of groups on the server. I need to find out all members of a group called “ftponly”. How do I list all members of a group on Linux or Unix-like systems?

The /etc/group file is a text file that defines the groups on the Linux and Unix based systems. You can simply query this file to find and list all members of a group.

Linux Show All Members of a Group Commands

  1. /etc/group file – User group file
  2. members command – List members of a group
  3. lid command (or libuser-lidon newer Linux distros) – List user’s groups or group’s users

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

Use the grep command as follows:
$ grep 'grpup-name-here' /etc/group
$ grep 'ftponly' /etc/group
$ grep -i --color 'ftponly' /etc/group

Sample outputs:

ftponly:x:1001:raj,vivek,archana,sai,sayali

To get just a list of all members of a group called ftponly, type:

awk -F':' '/ftponly/{print $4}' /etc/group

Linux: List all members of a group using members command

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

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

Fig. 01: members command in action on Linux show all members of a group command
Fig. 01: members command in action to list members in a group

In this example the members command displays a space-separated list of group member names on screen.

Linux: 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 sudo libuser-lid -g ftponly
Sample outputs:

 raj(uid=1001)
 vivek(uid=1002)
 archana(uid=1003)
 sai(uid=1004)
 sayali(uid=1005)

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

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

 adm(gid=4)
 cdrom(gid=24)
 sudo(gid=27)
 dip(gid=30)
 plugdev(gid=46)
 lpadmin(gid=109)
 nixcraft(gid=1000)
 sambashare(gid=124)

Conclusion

Now you know how to use various commands on Linux 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

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Notable Replies

  1. That is cool indeed. Maybe run it via shell linter too. It gave some warnings:

    In list_users.sh line 13:
    Red="\033[0;31m"          # Red
    ^-- SC2034: Red appears unused. Verify it or export it.
    
    
    In list_users.sh line 20:
    echo -e "$Bar${Bold}[ System User Groups ]${ResetTxt}$Bar"
         ^-- SC2039: In POSIX sh, echo flags are undefined.
                 ^-- SC2154: Bold is referenced but not assigned.
    
    
    In list_users.sh line 28:
    echo -e "$Bar${Bold}[ System User Accounts ]${ResetTxt}$Bar"
         ^-- SC2039: In POSIX sh, echo flags are undefined.
    

    See

Continue the discussion www.nixcraft.com

2 more replies

Participants

Historical Comment Archive

19 comment

  1. thrid command at the top has a typo

    $ grep -i color ‘ftponly’ /etc/group

    missing (–)color

    $ grep -i –color ‘ftponly’ /etc/group

    Might confuse newbies

  2. You might prefer use the getent since the group database could be on LDAP, NIS e etc:
    $ getent group ftponly

  3. Please be aware that some of these commands will not show a user where their primary group is the one you are concerned with – as the primary group is listed in /etc/passwd

  4. Many of these require commands to be installed and as stated, don’t necessarily show a users’s primary group.

    This would work for that, though:
    grep group_name /etc/group |cut -d':' -f3 |xargs -Ix grep x /etc/passwd |cut -d ':' -f1

    1. does this command only show the users with group_name as their primary group? Or does it show all users with that group (either primary or secondary?)

      Thanks!

  5. how can i print alphates in matrix
    a b c d e f
    g h i j k l m
    n o p q r s

    this way please help me

    1. Try this:

      COUNTER=0; for i in {a..z}; do COUNTER=$((COUNTER+1)); echo -n “$i “; if ! (( COUNTER % 5)); then echo “”; fi ; done; echo “”

      It will make the following output:

      a b c d e
      f g h i j
      k l m n o
      p q r s t
      u v w x y
      z

  6. Hi,

    I am finding that accomplishing simple tasks in Linux like getting a list of members of a group is not complicated or complex enough, which makes me feel inadequate because I cannot confound ‘n00bs’ with the arcane and difficult nature of Linux. Could anyone provide some more complicated ways of achieving these same results?

    Cheers.

    1. Sysadmins are lazy, they don’t want to do a lot of work to complete a single task.

      But I suppose that writing a script that loops the groups file, line by line, searching for the separators, then looping the strings letter by letter, and printing the result, would be a quite complicated way to archive mostly the same result as using grep to get the line from the groups file.

      But maybe it’s just me that don’t have a need to scare people from using linux? :P

    Have a question? Post it on our forum!