Unix Technical Forum

Getting the list of members in a group.

This is a discussion on Getting the list of members in a group. within the Linux Operating System forums, part of the Unix Operating Systems category; --> This appears to be a simple question, but I have never gotten round to find out due to webmin. ...


Go Back   Unix Technical Forum > Unix Operating Systems > Linux Operating System

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-19-2008, 06:11 AM
voipfc
 
Posts: n/a
Default Getting the list of members in a group.


This appears to be a simple question, but I have never gotten round to
find out due to webmin.

How do you list the members of group from the command line?

/voipfc

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-19-2008, 06:11 AM
Lew Pitcher
 
Posts: n/a
Default Re: Getting the list of members in a group.

On Jun 20, 6:59 am, voipfc <voi...@googlemail.com> wrote:
> This appears to be a simple question, but I have never gotten round to
> find out due to webmin.
>
> How do you list the members of group from the command line?


The simplest way would be to
cat /etc/group
and look at the fourth field

A bit more complex would be
grep 'groupname' /etc/group
and look at the fourth field

And, you could
awk -F : '$1 ~ /groupname/ { print $4}' /etc/group
and look at the output


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-19-2008, 06:11 AM
Davide Bianchi
 
Posts: n/a
Default Re: Getting the list of members in a group.

On 2007-06-20, Lew Pitcher <lpitcher@teksavvy.com> wrote:
> On Jun 20, 6:59 am, voipfc <voi...@googlemail.com> wrote:
> The simplest way would be to
> cat /etc/group
> and look at the fourth field


All those have a problem: they don't give you all the members for a
group.

If I do this on my system:

# id
uid=1000(davide) gid=100(users)
groups=100(users),11(floppy),17(audio),18(video),1 9(cdrom)

# grep users /etc/group
users::100:

So I'm member of the group 'users' but I do not appear in the group file
associated to that group.

Davide

--
Microsoft seems to have gotten a lot of mileage out of the C2 rating for
NT with no network connection. I wonder if a B3 rating for Linux with no
power cord might be of value.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-19-2008, 06:11 AM
Lew Pitcher
 
Posts: n/a
Default Re: Getting the list of members in a group.

On Jun 20, 8:21 am, Davide Bianchi <davideyeahs...@onlyforfun.net>
wrote:
> On 2007-06-20, Lew Pitcher <lpitc...@teksavvy.com> wrote:
>
> > On Jun 20, 6:59 am, voipfc <voi...@googlemail.com> wrote:
> > The simplest way would be to
> > cat /etc/group
> > and look at the fourth field

>
> All those have a problem: they don't give you all the members for a
> group.
>
> If I do this on my system:
>
> # id
> uid=1000(davide) gid=100(users)
> groups=100(users),11(floppy),17(audio),18(video),1 9(cdrom)
>
> # grep users /etc/group
> users::100:
>
> So I'm member of the group 'users' but I do not appear in the group file
> associated to that group.


Actually, your results reflect a (common) problem with setup. It
appears that the "primary group" value from the various users recorded
in your /etc/password have not been carried over to your /etc/group
file.

If you are in this situation, then the process of obtaining a complete
listing for a group becomes a bit more complex. I don't think that
there is a specific tool that will answer your question in this case,
but a shell script could be crafted to do what you want.




Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-19-2008, 06:12 AM
Chris Cox
 
Posts: n/a
Default Re: Getting the list of members in a group.

Lew Pitcher wrote:
> On Jun 20, 8:21 am, Davide Bianchi <davideyeahs...@onlyforfun.net>
> wrote:
>> On 2007-06-20, Lew Pitcher <lpitc...@teksavvy.com> wrote:
>>
>>> On Jun 20, 6:59 am, voipfc <voi...@googlemail.com> wrote:
>>> The simplest way would be to
>>> cat /etc/group
>>> and look at the fourth field

>> All those have a problem: they don't give you all the members for a
>> group.
>>
>> If I do this on my system:
>>
>> # id
>> uid=1000(davide) gid=100(users)
>> groups=100(users),11(floppy),17(audio),18(video),1 9(cdrom)
>>
>> # grep users /etc/group
>> users::100:
>>
>> So I'm member of the group 'users' but I do not appear in the group file
>> associated to that group.

>
> Actually, your results reflect a (common) problem with setup. It
> appears that the "primary group" value from the various users recorded
> in your /etc/password have not been carried over to your /etc/group
> file.


This is NOT a problem. It is designed this way. To get a view
into all groups and "membership" ... you'll have to look at both
/etc/passwd (where just a gid for the primary is found) and
/etc/group (where you'll find gid to name mappings and supplemental
group membership).

>
> If you are in this situation, then the process of obtaining a complete
> listing for a group becomes a bit more complex. I don't think that
> there is a specific tool that will answer your question in this case,
> but a shell script could be crafted to do what you want.
>


For example (off the top of my head)... No warranties...

#!/bin/sh

#
# Sort /etc/group and /etc/passwd ALPHABETICALLY by gid
#
sort -t: +2 /etc/group >/tmp/group-sorted.out
sort -t: +3 /etc/passwd >/tmp/passwd-sorted.out

#
# Join on the gid field and cut out just the username and the gid
#
join -t: -1 4 -2 3 /tmp/passwd.sorted /tmp/group.sorted |
cut -d: -f2,8 >/tmp/primary-group.out

#
# Read the username and primary group, find supplemental groups for
# each username and do final output.
#
while IFS=: read username pgrp;do
#
# Grep supplemental groups, but remove the primary
# group if found again (that is non standard btw)
#
sgrps=`grep "[,]*${username}[,]*" /etc/group |
cut -d: -f1 | grep -v "^${pgrp}\$"`
#
# Separate groups with commas, remove trailing
# comma.
#
allgrps=`echo -e "$pgrp\n$sgrps\c" |
tr '\012' ',' | sed 's/,$//'`
#
# Output the result
#
echo "$username:$allgrps"
done </tmp/primary-group.out

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 01-19-2008, 06:12 AM
Keith Keller
 
Posts: n/a
Default Re: Getting the list of members in a group.

On 2007-06-20, voipfc <voipfc@googlemail.com> wrote:
>
> How do you list the members of group from the command line?


Again, I'm surprised not to see this suggestion:

getent group

This does suffer from the same drawback as looking solely at /etc/group,
in that it won't tell you where a userid has his primary gid listed in
/etc/passwd but not in /etc/group. But it has the advantage that it
will list all available groups, if your box is an NIS or LDAP (pam_ldap
or nss_ldap) client.

--keith

--
kkeller-usenet@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
see X- headers for PGP signature information

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:50 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com