How to list all users in a Linux group?

后端 未结 20 1169
春和景丽
春和景丽 2020-12-07 06:51

How do I list all members of a group in Linux (and possibly other unices)?

相关标签:
20条回答
  • 2020-12-07 07:43

    I think the easiest way is the following steps, you won't need to install any package or software:

    1. First, you find out the GID of the group that you want to know the users, there are a lot of ways for that: cat /etc/group (the last column is the GID) id user (the user is someone who belongs to the group)

    2. Now you will list all the user on the file /etc/passwd, but you will apply some filters with the following sequel of commands to get just the members of the previous group.

    cut -d: -f1,4 /etc/passwd |grep GID (the GID is the number you got from the step 1)

    cut command will select just some "columns" of the file, the parameter d sets the delimiter ":" in this case, the parameter -f selects the "fields" (or columns) to be shown 1 and 4 in out case (on the file /etc/passwd, the 1º column is the name of the user and the 4º is the GID of the group which the user belongs), to finalize the |grep GID will filter just the group (on the 4º column) that you had chosen.

    0 讨论(0)
  • 2020-12-07 07:44
    lid -g groupname | cut -f1 -d'(' 
    
    0 讨论(0)
  • 2020-12-07 07:45

    The following shell script will iterate through all users and print only those user names which belong to a given group:

    #!/usr/bin/env bash
    getent passwd | while IFS=: read name trash
    do
        groups $name 2>/dev/null | cut -f2 -d: | grep -i -q -w "$1" && echo $name
    done
    true
    

    Usage example:

    ./script 'DOMAIN+Group Name'
    

    Note: This solution will check NIS and LDAP for users and groups (not only passwd and group files). It will also take into account users not added to a group but having group set as primary group.

    Edit: Added fix for rare scenario where user does not belong to group with the same name.

    Edit: written in the form of a shell script; added true to exit with 0 status as suggested by @Max Chernyak aka hakunin; discarded stderr in order to skip those occasional groups: cannot find name for group ID xxxxxx.

    0 讨论(0)
  • 2020-12-07 07:47

    The following command will list all users belonging to <your_group_name>, but only those managed by /etc/group database, not LDAP, NIS, etc. It also works for secondary groups only, it won't list users who have that group set as primary since the primary group is stored as GID (numeric group ID) in the file /etc/passwd.

    awk -F: '/^groupname/ {print $4;}' /etc/group
    
    0 讨论(0)
  • 2020-12-07 07:47

    Here is a script which returns a list of users from /etc/passwd and /etc/group it doesn't check NIS or LDAP, but it does show users who have the group as their default group Tested on Debian 4.7 and solaris 9

    #!/bin/bash
    
    MYGROUP="user"
    
    # get the group ID
    MYGID=`grep $MYGROUP /etc/group | cut -d ":" -f3`
    if [[ $MYGID != "" ]]
    then
      # get a newline-separated list of users from /etc/group 
      MYUSERS=`grep $MYGROUP /etc/group | cut -d ":" -f4| tr "," "\n"`
      # add a newline
      MYUSERS=$MYUSERS$'\n'
      # add the users whose default group is MYGROUP from /etc/passwod 
      MYUSERS=$MYUSERS`cat /etc/passwd |grep $MYGID | cut -d ":" -f1`
    
      #print the result as a newline-separated list with no duplicates (ready to pass into a bash FOR loop)
      printf '%s\n' $MYUSERS  | sort | uniq
    fi
    

    or as a one-liner you can cut and paste straight from here (change the group name in the first variable)

    MYGROUP="user";MYGID=`grep $MYGROUP /etc/group | cut -d ":" -f3`;printf '%s\n' `grep $MYGROUP /etc/group | cut -d ":" -f4| tr "," "\n"`$'\n'`cat /etc/passwd |grep $MYGID | cut -d ":" -f1`  | sort | uniq
    
    0 讨论(0)
  • 2020-12-07 07:49
    getent group insert_group_name_here | awk -F ':' '{print $4}' | sed 's|,| |g'
    

    This returns a space separated list of users which I've used in scripts to populate arrays.

    for i in $(getent group ftp | awk -F ':' '{print $4}' | sed 's|,| |g')
        do
            userarray+=("$i")
        done
    

    or

    userarray+=("$(getent group GROUPNAME | awk -F ':' '{print $4}' | sed 's|,| |g')")
    
    0 讨论(0)
提交回复
热议问题