Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display Members of Group

Status
Not open for further replies.

Zugdud

IS-IT--Management
Feb 26, 2003
158
US
Howdy folks, is there a command or way to display which users are member of a group?
 
Oh, this is on redhat 9 distribution of linux
 
cat /etc/passwd | grep "group id" | awk -F ':' '{ print $1 }'

cheers.
 
Followed by:
grep <groupID/groupname> /etc/group | awk -F: '{print $4}'

I hope that helps.

Mike
 
Thanks for the follow ups, when I try to run the first command:

cat /etc/passwd | grep 57173 | awk -F ':''{print}'

I don't get any output, it just goes to the next line. I also tried running this command after it:

grep 57173 /etc/group | awk -F:'{print$4}'

And it outputs a blank line, If i run that command with print$1 it outputs the groupname for the group id
 
Sorry, the first command I did run with print$1. So


cat /etc/passwd | grep 57173 | awk -F ':''{print$1}'
 
Whoops nevermind i was able to figure out what you guys were doing and got it to work with:

grep 57173 /etc/passwd | awk -F: '{print$5}'

thanks for the help!
 

Hi don't forget to check both files, the /etc/passwd will only give you members for whom the group is their primary.

You say the second command (inspecting /etc/group) only showed the groupname so we know that no user has this as their secondary group...yet. But I guess things can change!

Dubbs.
 

Or you can use the groups(1M) command!!! I'm a solaris man, but I've just checked a redhat system and it's there as well.
 

Sorry, ignore that last post. doesn't do want you want. Someone delete it!

Dubbs.
 

On solaris you can run the following:

logins -g [groupname]

Obviously it's better to use utility like this than a script.

Not sure if RH has a logins(1M). can you confirm?
 
Code:
include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <grp.h>


int
main (int argc, char **argv)
{
  int v = 0;
  char buf[200];
  struct group *grp;

  if (argc < 2)
    {
      printf ("Error: Please specify a group name\n");
      return -1;
    }
  grp = getgrnam (argv[1]);
  while (*grp->gr_mem != NULL)
    {
      sprintf (buf, "%d: %s", v++, *grp->gr_mem);
      printf ("%s for group %s\n", buf, grp->gr_name);
      grp->gr_mem++;
    }
  return 0;
}
 
Why using cat, grep, ... when awk can do it himself ?
awk -F: '$4==57173{print $5}' /etc/passwd


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
One of the reasons I like IBM's AIX:

lsgroup <name of group>

I know that doesn't help you.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top