1833059 Members
2708 Online
110049 Solutions
New Discussion

which users in a group?

 
SOLVED
Go to solution
Enrico Venturi
Super Advisor

which users in a group?

Hello guys,
how can I get from command line the users belonging to a group??

thanks
Enrico
10 REPLIES 10
Pete Randall
Outstanding Contributor

Re: which users in a group?

grep "group name" /etc/group


Pete

Pete
Enrico Venturi
Super Advisor

Re: which users in a group?

Yes... eh eh eh...
I meant: some commands giving a list of users, and not just a string like dba::202:bmml,alcatel,snml ...
I'd prefere as output:
bmml
alcatel
snml
Pete Randall
Outstanding Contributor

Re: which users in a group?

Well, there is the groups command but that's not exactly what you're looking for, more like the opposite of what you're looking for. It looks like you might have to come up with a C program using getgrent().


Pete

Pete
Peter Nikitka
Honored Contributor

Re: which users in a group?

Hi,

use the 'groups' command:
groups : my groups I am member at
groups usernam

see 'man groups'

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
A. Clay Stephenson
Acclaimed Contributor

Re: which users in a group?

Try "logins -m".

Here's another approach that will list:
UserGroup1Group2 ...

#!/usr/bin/sh
logins | awk '{print $1}' | while read U
do
echo "${U}\t\c"
groups -l ${U} | while read G
do
echo "${G}\c"
done
echo
done
If it ain't broke, I can fix that.
Sandman!
Honored Contributor
Solution

Re: which users in a group?

Try the awk construct below:

# awk -F: '$1=="dba" {for(i=1;i<=split($4,z,",");++i) print z[i]}' /etc/group
Victor BERRIDGE
Honored Contributor

Re: which users in a group?

Greetings,

What about:
grep /etc/group|cut -d : -f 4|sed y/,/\\n/


All the best
Victor
Peter Nikitka
Honored Contributor

Re: which users in a group?

Sorry,

just realized, what you really want.
From a CLI sight of view, it depends which name resolution is set in ns_switch.conf: files, NIS, ...
It seems not deifficult to me. to modify my NIS-example for other resolution types:
(ypmatch GRPNAM group.byname; ypcat passwd) |
awk -F: 'NR==1 {group=$1;gid=$3;i=split($4,grp,",");next}
$4==gid {grp[++i]=$1}
END {if(i) {for(j=1;j<=i;j++) print grp[j]}}'

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
OldSchool
Honored Contributor

Re: which users in a group?

for a in `grep /etc/group | cut -f 4 -d : | sed "s/,/ /g'`
do
echo $a
done
Sp4admin
Trusted Contributor

Re: which users in a group?

Hello,

just do a long listing or grep for the group.

ll /etc/groups

grep group name /etc/groups

sp,