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 |
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 Regards |
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. |
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 Code:
$ ./show.group.users internet Hope this is of use. |
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 |
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}' Code:
members() Code:
sudo apt-get install members |
If you are (or not) using a directory service...
Code:
$ getent group support |
+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.
|
awesome, thank you!
Quote:
|
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! |
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 , ' ' ; } Code:
members(){ |
groupmems -g "group name" --list/-l
|
@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. |
All times are GMT -5. The time now is 07:14 AM. |