LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-27-2007, 01:18 PM   #1
bg108
LQ Newbie
 
Registered: Nov 2007
Posts: 17

Rep: Reputation: 0
how to list all members of a group


Hi,

I created a group named "support" with 2 users assigned to this group
Thus, group support now has two membes, for example tim and tony.

Which command do I use to list all the members belonged to group "support" ???

(groups tim or group tony) will display support as their group, but I want to show the other way around. I'm using Ubuntu dis.

Please help, thx
 
Old 11-27-2007, 01:25 PM   #2
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 31
Hi, I'm on Ubuntu 7.10 and I've access to the /etc/group file. Is something like the output of
Code:
grep ^support /etc/group
enough for your needs?

Regards

Last edited by enemorales; 11-27-2007 at 01:26 PM. Reason: code formatting
 
1 members found this post helpful.
Old 11-27-2007, 01:44 PM   #3
bg108
LQ Newbie
 
Registered: Nov 2007
Posts: 17

Original Poster
Rep: Reputation: 0
Hi,

Thanks for your reply, I found that when trying to do
For example
useradd -g support tim

And I go and look at the file /etc/group, the user "tim" was not added to the end of the group line, like this:
support:x:1001:

It means that the useradd command didn't update /etc/group file eventhough if I issue the command:
groups tim => it does show it belongs to the group "support"

And that's why your command doesn't work. Do you have any ideas why it doesn't update the group file ???

I'm logged in as root user, using Ubuntu 7.10 Desktop ed.

Last edited by bg108; 11-27-2007 at 01:45 PM.
 
Old 11-27-2007, 02:15 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

There is a possibility that the group in question is a users primary group. You need to check the /etc/group file and the /etc/passwd if you want to be 100% sure. To my knowledge there is no command to do this, you need to write it yourself.

The fact that the group is represented as a number in the /etc/passwd file makes this a bit harder.

You need to:

1) get the number from the /etc/group file (and also get the users attached to it)
2) check to see if the found group number is present in the /etc/passwd file.

Something like this (rough example):
Code:
#!/bin/bash

srchGroup="$1"

# get the corresponding line from /etc/group
for thisLine in "`grep "^${srchGroup}:" /etc/group`"
do
  # get the parts of interest 
  grpNumber="`echo ${thisLine} | cut -d":" -f3`"
  grpUsers="`echo ${thisLine} | cut -d":" -f4 | sed 's/,/ /g'`"
done

# check /etc/passwd
pwdUsers="`awk -F":" '$4 ~ /'${grpNumber}'/ { printf("%s ",$1) }' /etc/passwd`"

echo "0 ${srchGroup}" # given at commandline
echo "1 ${grpNumber}" # from /etc/group
echo "2 ${grpUsers}"  # from /etc/group
echo "3 ${pwdUsers}"  # from /etc/passwd
echo "All users: ${grpUsers} ${pwdUsers}"
Example run:
Code:
$ ./show.group.users internet
0 internet
1 500
2 testuser anotheruser
3 druuna jade 
All users: testuser anotheruser druuna jade
PS: I just noticed your previous reply, you already noticed the passwd file part

Hope this is of use.
 
Old 02-04-2010, 04:54 AM   #5
dandv
LQ Newbie
 
Registered: Feb 2010
Location: Silicon Valley, CA
Distribution: Ubuntu, TinyCore
Posts: 4

Rep: Reputation: 0
Lightbulb How to do it for numeric group IDs

To list all users in a group "mygroup" (tested on RHEL):

Code:
grep :`grep ^mygroup /etc/group | cut -d: -f3`: /etc/passwd
 
Old 04-26-2010, 05:17 PM   #6
rupert160
LQ Newbie
 
Registered: Apr 2010
Posts: 2

Rep: Reputation: 0
The answer is in the /etc/group

The /etc/group file lists all the users against each group.
This is a simple script you can run to find users where xxx is the group name:

Code:
   cat /etc/group | grep --regex "^xxx:.*" | awk -F: '{print $4}'
however I recommend you place the following script in your ~/.bashrc file (or /etc/bash.bashrc file if you want everyone to use it):

Code:
members()
{
   cat /etc/group | grep --regex "^$1:.*" | awk -F: '{print $4}'
}
otherwise you can install an actual "members" function that does the same thing:

Code:
   sudo apt-get install members
 
0 members found this post helpful.
Old 04-26-2010, 07:04 PM   #7
frndrfoe
Member
 
Registered: Jan 2008
Distribution: RHEL, CentOS, Ubuntu
Posts: 379

Rep: Reputation: 38
If you are (or not) using a directory service...

Code:
$ getent group support

Last edited by frndrfoe; 04-26-2010 at 07:18 PM.
 
0 members found this post helpful.
Old 04-26-2010, 08:08 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,009

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
+1 to druuna's reply as the issue is searching the /etc/group file will not tell you if someone has this as their primary group.
 
0 members found this post helpful.
Old 02-13-2012, 08:52 AM   #9
mallikaone
LQ Newbie
 
Registered: Feb 2012
Posts: 1

Rep: Reputation: Disabled
awesome, thank you!

Quote:
Originally Posted by frndrfoe View Post
If you are (or not) using a directory service...

Code:
$ getent group support
Just what I was looking for!
 
Old 10-03-2012, 08:59 AM   #10
gaq07ar
LQ Newbie
 
Registered: Oct 2012
Posts: 1

Rep: Reputation: Disabled
Answer

Hi there, my friend. I was having the same trouble, i'm just getting started on this.

What you have to do is,

getent group support
(and you'll see the users in the group)
e.g.:
support:x:1002:
(you have nothing)

And then you have to modify the users group entries like this

sudo usermod -G support tim
sudo usermod -G support tony
getent group support
support:x:1002:tim,tony


Regards!
 
Old 09-27-2013, 10:14 AM   #11
avarus
Member
 
Registered: Apr 2004
Location: Oxford, UK
Distribution: Ubuntu, Debian, various
Posts: 230
Blog Entries: 5

Rep: Reputation: 33
One-liner version

Hi,

I'm posting this reply as I found this answer when Google searching but all the answers given are lacking something. Reading /etc/group and /etc/passwd directly is not portable as systems can use other authentication mechanisms. Also the Bash script given is a bit wordy and it has a bug matching the group number so if you ask for users in group 0 you get users in group 100 101 1000 etc.

Other forums have Perl scripts and any number of other suggestions, but Perl is a bit of a sledgehammer to crack a nut.

Here's mine:

Code:
members(){ getent passwd | awk -F":" '$4 == "'`getent group "$1" | cut -d: -f3`'" { printf("%s ",$1) }' ; getent group "$1" | cut -d: -f4 | tr , ' ' ; }
Or slightly better, in that it only runs "getent group" once and warns of non-existent groups, and wordier:

Code:
members(){
   g_line=`getent group "$1"`
   [ -n "$g_line" ] || { echo "No such group $1." >&2 ; return 1 ; }
   getent passwd | awk -F":" '$4 == "'`echo "$g_line" | cut -d: -f3`'" { printf("%s ",$1) }'
   echo "$g_line" | cut -d: -f4 | tr , ' '
}
 
Old 11-23-2013, 06:54 PM   #12
alfred2g
LQ Newbie
 
Registered: May 2009
Posts: 6

Rep: Reputation: 0
groupmems -g "group name" --list/-l
 
Old 11-24-2013, 03:26 AM   #13
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@alfred2g: That command is rather new and is only available on some of the most current distro's.

These I could check:
- Debian 6/7 and RHEL 5 do not have this command,
- RHEL 6 and Slackware 14 do.
 
  


Reply

Tags
group, list, members, users



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Listing the members of a group armandino Linux - General 5 06-24-2007 06:43 PM
listing the members of a group bondoq Linux - Server 3 02-25-2007 05:10 PM
all winbind members of same group paul_mat Linux - Networking 0 01-16-2006 05:46 PM
winbind: group members? eantoranz Linux - Software 0 11-01-2005 08:47 AM
Group members and rights EERookie Linux - Newbie 0 06-03-2004 08:19 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration