How do I list the groups that a UNIX user is a member of?

79

11

With pts m groupname I can list the users in a group. How can I do the opposite - that is, list all the groups that a user is a member of?

(I need this to find the name of a group that I know a user who is a member of, but I don't know the group name...)

Clarification:
This question is complicated by the fact that I am not the system administrator, and that I don't have root permissions (far from it). Instead, I am part of a group of "moderators" that administrate a small part of the system - to be specific, the physics branch of the student union at my university's central IT system.

Within our branch, we have a bunch of different access groups for people who are in charge of things. In this case, one of two people responsible for something (it doesn't matter what) have been replaced, and I was looking to examine the other user to find out what access rights I should give the replacement.

I have now been able to solve the immediate problem (the new guy not being able to access a folder) by examining the folder to see who has access, and there picking the group from the list. However, there might be more privileges that this user should have that I don't know of, so the question is still relevant for me, albeit not so acute.

Tomas Aschan

Posted 2010-07-26T19:09:49.470

Reputation: 2 294

Answers

6

Found it - way later, but I did! =)

Just as

$>pts m [group name]

lists all the members in a group,

$>pts m [user name]

lists all the groups a user is member of. It was too simple :P

Tomas Aschan

Posted 2010-07-26T19:09:49.470

Reputation: 2 294

3pts isn’t a standard command. – user2284570 – 2015-12-13T13:52:27.740

1@user2284570: Perhaps not. But it was clearly stated in the question (the very first thing, in fact, and with syntax highlighting) that it's pts that this question concerns. – Tomas Aschan – 2015-12-14T07:27:01.367

105

You can also use the groups command:

[root@ftp ~]# groups root
root : root bin daemon sys adm disk wheel

if all else fails there is also good old grep:

[root@ftp ~]# grep root /etc/group
root:x:0:root
bin:x:1:root,bin,daemon
daemon:x:2:root,bin,daemon
sys:x:3:root,bin,adm
adm:x:4:root,adm,daemon
disk:x:6:root
wheel:x:10:root,admin

John T

Posted 2010-07-26T19:09:49.470

Reputation: 149 037

1grep ^root: /etc/group might be better. – Paused until further notice. – 2010-07-26T20:17:44.497

no it is not as it only lists one of the groups root is a member of

maybe with a bit of awk its more clear :)

grep root /etc/group|awk -F: '{ print $1 }' – matthias krull – 2010-07-26T20:55:48.577

3Examining /etc/group sounds nice, but if your system is using a centralized directory (NIS, LDAP, ActiveDirectory, ?), you won't find a complete list of groups there. Only local group definitions would be in that file. – Slartibartfast – 2010-07-27T05:42:01.743

The command groups kcz only gives me usr, although I know that this user is member of a group called fkm. What is missing? – Tomas Aschan – 2010-08-02T21:06:43.120

Not sure, pastebin your /etc/group @Tomas. – John T – 2010-08-02T22:14:35.250

@John; I have very little privileges on this system - it's the central IT system at my university, and I have responsibility only for the small physics branch of the student union. – Tomas Aschan – 2010-08-04T10:06:00.953

@Tomas if you don't have permission what will you accomplish by knowing a user's group memberships? Maybe this is better handled by the server administrator. – John T – 2010-08-05T02:25:59.853

@slart well that is something the user would have to mention in their question. There is no one-size-fits-all answer, obviously. – John T – 2010-08-05T02:27:02.910

@John: Please see my update for clarification. – Tomas Aschan – 2010-08-05T10:14:47.607

groups has been superseded by id on some systems; id -Gn root would give the same output as in this answer. – Seamus – 2019-05-17T03:02:18.180

28

$ id [username]

coneslayer

Posted 2010-07-26T19:09:49.470

Reputation: 7 494

"$: Command not found". – Tomas Aschan – 2010-08-02T21:07:09.187

8The $ represents the command prompt. Start typing with id. – coneslayer – 2010-08-03T11:08:31.810

Still doesn't give me much: uid=[five digits](kcz) gid=30(usr) – Tomas Aschan – 2010-08-05T10:16:39.897

it is showing uid= gid= groups= so the third thing I'm getting is the list of groups a user belongs to. – MKJ – 2017-09-01T14:24:20.780