12

I want to know the groups of some user.

$dscl . list /groups filterByUSer theFindUser

Is this possible (even if I have to enumerate all the groups and then filter by user) in a straightforward manner?

19

Try the command given below.

id -Gn [user]

On my Mac, the output from id -Gn davidanderson is given below.

staff everyone localaccounts _appserverusr admin _appserveradm _lpadmin _appstore _lpoperator _developer _analyticsusers com.apple.access_ftp com.apple.access_screensharing com.apple.access_ssh 2 1

The delimiter could be changed from a space to a comma by entering the following.

groups="$(id -Gn davidanderson)"
groups="${groups// /,}"
echo "$groups"

Here, the output would be as follows.

staff,everyone,localaccounts,_appserverusr,admin,_appserveradm,_lpadmin,_appstore,_lpoperator,_developer,_analyticsusers,com.apple.access_ftp,com.apple.access_screensharing,com.apple.access_ssh,2,1

I am using High Sierra, macOs 10.13.4 and a bash shell.

| improve this answer | |
  • Is there a way to add a delimiter between the groups? – Ryan Gates Dec 30 '19 at 21:23
  • You mean other than a space? Like a comma maybe? – David Anderson Dec 30 '19 at 22:55
  • Thank you for your response. I ended up using id on its own. It printed the group id, the name in parentheses and used a comma as a separator. This would only work for looking up the current user. That solved my problem though. – Ryan Gates Dec 31 '19 at 15:40
3

To list all the groups to which a user belongs, type:

id [username]

[username] argument is optional. By default, the logged in user is assumed. The output will include the numeric user id uid, and the list of all the groups along with their group id gid, of which the user is member of. The first group in the output is the user's primary group.

To list just the group names type:

id -Gn [username]

To list just the group numbers type:

id -G [username]

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .