Solved

Problem in Getting members of a group using "grep" command

Posted on 2004-08-15
12
255 Views
Last Modified: 2010-04-20
I wanted to see the members of a particular group and a person suggested me to do
"grep neighbor /etc/group" where neighbor is the group name.But when I did that it showed "neighbor:*:205:"
 So it didn't show any names?
 He also said I can do in a script as follows...
 #!/bin/bash
# Pass group name as an argument.
echo -n "Members of $1: "
grep $1 /etc/group| cut -d: -f4
 
   When I executed the above script from my home directory using "bash scriptname" it asked for "Members of :" and when I entered group name as neighbor, nothing happened ?

  So any ideas why that problem is occuring please?

0
Comment
Question by:gopikrish
  • 3
  • 3
  • 3
  • +2
12 Comments
 
LVL 40

Accepted Solution

by:
jlevie earned 50 total points
Comment Utility
> "grep neighbor /etc/group" where neighbor is the group name.But when I did that it showed "neighbor:*:205:"

Will only show the users that are members of that group if they've been explicitly listed as members in the group file, which isn't necessary and hasn't been done in this case. Various accounts may have their login group as 205 in /etc/password, which will make them members of that group. To find all accounts with a login group ID of 205 you can execute:

grep -e ".*:[0-9]*:205:.*" /etc/passwd
0
 

Author Comment

by:gopikrish
Comment Utility
No actually someone mentioned one cool command which listed all member names of a particular group but I forgot that :(
  It was said by a member from Grex shell account.So if anyone knows that please share it here.Thanks.
0
 
LVL 23

Expert Comment

by:Mysidia
Comment Utility
cut -d: -f1 /etc/passwd |xargs -n 1 id |grep neighbor
0
 
LVL 22

Expert Comment

by:pjedmond
Comment Utility
Line extracted from /etc/password:

angela:x:504:504::/home/angela:/bin/bash

This gives username:x means a shadowed password:userid:PRIMARY groupid::home directory:login shell

Line extracted from /etc/group:

mail:x:12:mail,postfix

This gives group name:x means shadowed password(man gpasswd):group id:comma seperated members of the group

postfix:x:89:

also exists in /etc/group

...but because it requires access to some files that mail owns, it is made a member of the mail group as well.

Traditionally, there were circumstances where all users were made members of the users group, and so the approach that you were using (for the neighbor group ) would have worked in thos circumstances. Your approach would ahve got:

users:x:500:user1,user2,user3,user4 etc

Nowadays, due to heightened security concerns etc, whenever a new person is added, they get created as a default a brand new group for each person. It therefore requires a concious effort to enable them to have the same group privileges as someone else.

HTH:)

0
 
LVL 22

Expert Comment

by:pjedmond
Comment Utility
I suppose that I'd better mention that this appears to be the default approach for RH 8.0, before someone starts flaming....and there is no reason why everyone shouldn't have group 500 in the /etc/passwd file, instead of the

users:x:500:user1,user2,user3,user4 etc

in /etc/group

I guess it depends on the distribution?
0
 
LVL 20

Expert Comment

by:Gns
Comment Utility
Um, are you looking for something like the AIX command "lsgroup" then? There is work in progress on something along those lines in the GNU sysutils project (http://savannah.gnu.org/projects/sysutils ... and look at http://savannah.gnu.org/cgi-bin/viewcvs/sysutils/sysutils/src/ for (alpha grade) source)...
One could probably make some more or less intelligent script to get hols of and list group memberships (both primary and secondary), to somewhat emulate this functionality:-).

-- Glenn
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:gopikrish
Comment Utility
Well, "members groupname" as well as
"cat groupname /etc/passwd | grep :205"
where 205 is the group name id
They both worked ! No one was able to mention that :) Anyway thanks for suggestions.
0
 
LVL 23

Expert Comment

by:Mysidia
Comment Utility
That doesn't actually solve the problem of displaying all the members of a group though.

/etc/passwd  gives login group only
/etc/groups   lists only additional group memberships of users

cat groupname /etc/passwd...   isn't right, that should give an error message about the file
"groupname" not existing

grep ':205'  /etc/passwd    would list login groups, but you can also get spurious entries, for
example if a user has UID 205 or realname 205  they will be shown.

awk -F: '{if ($4=="205"){print $0}}' /etc/passwd

Will show you all passwd file entries where GID is 205, and no spurious entries
0
 

Author Comment

by:gopikrish
Comment Utility
I see.So can you explain what it does please? I dont understand that and also why 4 is used in that $4 and 0 is used in $0 ? Thanks.
0
 
LVL 23

Expert Comment

by:Mysidia
Comment Utility
In awk, $4 is the 4th field in the line (record) and $0 is the whole line
0
 
LVL 22

Expert Comment

by:pjedmond
Comment Utility
JUst curious is:

members

a part of any specifc distribution. I've never seen it before. Having said that, we all have our own way of doing things and perhaps mine jut doesn't need the members command?
0
 
LVL 20

Expert Comment

by:Gns
Comment Utility
Hm, "members"... Not usually part of any install I've seen recently, but I have this very very faint recollection of seeing some odd shell builtin like that some time.... long long time ago:-).

-- Glenn
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now