[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

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

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
gopikrish
Asked:
gopikrish
  • 3
  • 3
  • 3
  • +2
1 Solution
 
jlevieCommented:
> "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
 
gopikrishAuthor Commented:
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
 
MysidiaCommented:
cut -d: -f1 /etc/passwd |xargs -n 1 id |grep neighbor
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
pjedmondCommented:
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
 
pjedmondCommented:
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
 
GnsCommented:
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
 
gopikrishAuthor Commented:
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
 
MysidiaCommented:
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
 
gopikrishAuthor Commented:
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
 
MysidiaCommented:
In awk, $4 is the 4th field in the line (record) and $0 is the whole line
0
 
pjedmondCommented:
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
 
GnsCommented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now