-   Linux - Newbie (
-   -   how to list all members of a group (

bg108 11-27-2007 02:18 PM

how to list all members of a group

I created a group named "support" with 2 users assigned to this group
Thus, group support now has two membes, for example tim and tony.

Which command do I use to list all the members belonged to group "support" ???

(groups tim or group tony) will display support as their group, but I want to show the other way around. I'm using Ubuntu dis.

Please help, thx

enemorales 11-27-2007 02:25 PM

Hi, I'm on Ubuntu 7.10 and I've access to the /etc/group file. Is something like the output of

grep ^support /etc/group
enough for your needs?


bg108 11-27-2007 02:44 PM


Thanks for your reply, I found that when trying to do
For example
useradd -g support tim

And I go and look at the file /etc/group, the user "tim" was not added to the end of the group line, like this:

It means that the useradd command didn't update /etc/group file eventhough if I issue the command:
groups tim => it does show it belongs to the group "support"

And that's why your command doesn't work. Do you have any ideas why it doesn't update the group file ???

I'm logged in as root user, using Ubuntu 7.10 Desktop ed.

druuna 11-27-2007 03:15 PM


There is a possibility that the group in question is a users primary group. You need to check the /etc/group file and the /etc/passwd if you want to be 100% sure. To my knowledge there is no command to do this, you need to write it yourself.

The fact that the group is represented as a number in the /etc/passwd file makes this a bit harder.

You need to:

1) get the number from the /etc/group file (and also get the users attached to it)
2) check to see if the found group number is present in the /etc/passwd file.

Something like this (rough example):



# get the corresponding line from /etc/group
for thisLine in "`grep "^${srchGroup}:" /etc/group`"
  # get the parts of interest
  grpNumber="`echo ${thisLine} | cut -d":" -f3`"
  grpUsers="`echo ${thisLine} | cut -d":" -f4 | sed 's/,/ /g'`"

# check /etc/passwd
pwdUsers="`awk -F":" '$4 ~ /'${grpNumber}'/ { printf("%s ",$1) }' /etc/passwd`"

echo "0 ${srchGroup}" # given at commandline
echo "1 ${grpNumber}" # from /etc/group
echo "2 ${grpUsers}"  # from /etc/group
echo "3 ${pwdUsers}"  # from /etc/passwd
echo "All users: ${grpUsers} ${pwdUsers}"

Example run:

$ ./ internet
0 internet
1 500
2 testuser anotheruser
3 druuna jade
All users: testuser anotheruser druuna jade

PS: I just noticed your previous reply, you already noticed the passwd file part ;)

Hope this is of use.

dandv 02-04-2010 05:54 AM

How to do it for numeric group IDs
To list all users in a group "mygroup" (tested on RHEL):


grep :`grep ^mygroup /etc/group | cut -d: -f3`: /etc/passwd

rupert160 04-26-2010 06:17 PM

The answer is in the /etc/group
The /etc/group file lists all the users against each group.
This is a simple script you can run to find users where xxx is the group name:


  cat /etc/group | grep --regex "^xxx:.*" | awk -F: '{print $4}'
however I recommend you place the following script in your ~/.bashrc file (or /etc/bash.bashrc file if you want everyone to use it):


  cat /etc/group | grep --regex "^$1:.*" | awk -F: '{print $4}'

otherwise you can install an actual "members" function that does the same thing:


  sudo apt-get install members

frndrfoe 04-26-2010 08:04 PM

If you are (or not) using a directory service...


$ getent group support

grail 04-26-2010 09:08 PM

+1 to druuna's reply as the issue is searching the /etc/group file will not tell you if someone has this as their primary group.

mallikaone 02-13-2012 09:52 AM

awesome, thank you!

Originally Posted by frndrfoe (Post 3948669)
If you are (or not) using a directory service...


$ getent group support

Just what I was looking for!

gaq07ar 10-03-2012 09:59 AM

Hi there, my friend. I was having the same trouble, i'm just getting started on this.

What you have to do is,

getent group support
(and you'll see the users in the group)
(you have nothing)

And then you have to modify the users group entries like this

sudo usermod -G support tim
sudo usermod -G support tony
getent group support


avarus 09-27-2013 11:14 AM

One-liner version

I'm posting this reply as I found this answer when Google searching but all the answers given are lacking something. Reading /etc/group and /etc/passwd directly is not portable as systems can use other authentication mechanisms. Also the Bash script given is a bit wordy and it has a bug matching the group number so if you ask for users in group 0 you get users in group 100 101 1000 etc.

Other forums have Perl scripts and any number of other suggestions, but Perl is a bit of a sledgehammer to crack a nut.

Here's mine:


members(){ getent passwd | awk -F":" '$4 == "'`getent group "$1" | cut -d: -f3`'" { printf("%s ",$1) }' ; getent group "$1" | cut -d: -f4 | tr , ' ' ; }
Or slightly better, in that it only runs "getent group" once and warns of non-existent groups, and wordier:


  g_line=`getent group "$1"`
  [ -n "$g_line" ] || { echo "No such group $1." >&2 ; return 1 ; }
  getent passwd | awk -F":" '$4 == "'`echo "$g_line" | cut -d: -f3`'" { printf("%s ",$1) }'
  echo "$g_line" | cut -d: -f4 | tr , ' '

alfred2g 11-23-2013 07:54 PM

groupmems -g "group name" --list/-l

druuna 11-24-2013 04:26 AM

@alfred2g: That command is rather new and is only available on some of the most current distro's.

These I could check:
- Debian 6/7 and RHEL 5 do not have this command,
- RHEL 6 and Slackware 14 do.

All times are GMT -5. The time now is 04:08 AM.