Solved

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

Posted on 2004-08-15
12
267 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +2
12 Comments
 
LVL 40

Accepted Solution

by:
jlevie earned 50 total points
ID: 11805624
> "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
ID: 11805815
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
ID: 11806341
cut -d: -f1 /etc/passwd |xargs -n 1 id |grep neighbor
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 22

Expert Comment

by:pjedmond
ID: 11806374
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
ID: 11806379
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
ID: 11808618
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
 

Author Comment

by:gopikrish
ID: 11814111
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
ID: 11815371
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
ID: 11815858
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
ID: 11816838
In awk, $4 is the 4th field in the line (record) and $0 is the whole line
0
 
LVL 22

Expert Comment

by:pjedmond
ID: 11822360
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
ID: 11828167
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

688 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