How To Tell What Group Unix User Is In

Have been getting this question asked a lot in the search engine queries for Unix Tutorial lately, and realised in all this time I haven’t created a separate page for the groups command – one of the most basic yet most useful Unix commands ever!

groups Command in Unix

Just like most of Unix/Linux commands, groups does exactly what it says: it shows you a list of Unix groups. Specifically, it shows you groups that your own user belongs to, or lists groups of any other user you specify.

In simplest form, you simply type groups without any parameters:

greys@s2:~ $ groups
greys libvirt

On this s2 server, I’m a member of my own group called greys and also belong to libvirt group which lets me manage kvm virtualization.

Of course, you can specify your own user implicitly as well.

This is what groups I’m in on MacOS:

greys@maverick:~ $ groups greys
 staff com.apple.sharepoint.group.1 everyone localaccounts _appserverusr admin _appserveradm _lpadmin com.apple.sharepoint.group.2 com.apple.sharepoint.group.3 _appstore _lpoperator _developer _analyticsusers com.apple.access_ftp com.apple.access_screensharing com.apple.access_ssh

groups is a basic Unix command

I’ve added groups to the Basic Unix Commands page and also created it a page today: groups command. Can’t believe this truly deserving Unix command escaped the chance to be in the Basic Commands hall of fame for so long!

Other Ways To List User Groups

I’ve actually taken the time ot refresh a really old but great post of mine today: 3 Ways to List Groups for a User in Linux – go read and and let me know what you think. It talks about using id command (quite basic) and getent command (far more advanced) for listing groups.

See Also




How to identify what Unix groups are available on your system

Today, I’d like to answer one of the oldest questions I have in my incoming UnixTutorial questions  email folder. Please leave comments if you need any more help with researching Unix groups on your system.

How to confirm what Unix groups are available

If you remember, a while ago I’ve introduced you to the getent command. It’s a great way of querying various information databases about your systems’ users, groups and some other objects. Here’s how you would use the command to get a full list of Unix groups known to your system:

ubuntu# getent group
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:
tty:x:5:
disk:x:6:
lp:x:7:
mail:x:8:
news:x:9:
uucp:x:10:
man:x:12:
proxy:x:13:
kmem:x:15:
dialout:x:20:
fax:x:21:
...

This is an abridged output, but I hope you get the idea. This output helps you confirm the following:

  1. Unix group name – first field
  2. Unix group ID (gid) – third field
  3. All the usernames of members for various groups – fourh field, unfortunatelly empty for all the groups in my example.

How to confirm the members of a Unix group

Using the same getent command, you can query the groups database using a group name. In my example below, I’m confirming the membership of a mygroup Unix group:

ubuntu# getent group mygroup
mygroup:x:1002:user1,greys,user2
As you can see, mygroup has 3 users: user1, greys and user2.

How to determine the number of Unix groups known to your system

One more thing you can learn about your Unix groups using getent command is to confirm the overall number of Unix groups – some scenarious require you to have this number. Here’s how you would use getent together with the wc command to confirm the number of groups:

ubuntu# getent group | wc -l
62
That’s it for today, let me know if there’s anything else you’d like to learn about this topic!

See also: