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

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
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
That's it for today, let me know if there's anything else you'd like to learn about this topic!

See also:

  • Sandeep Sagar

    "getent group mygroup" works only with Linux right? To find the primary group on a Solaris box I always do "su – myuser" and then "id", is there a smarter way?

  • Gleb Reys

    Hi Sandeep,

    The getent command will work on most Unix-like systems, and it definitely works in Solaris.

    Also, the easier way to confirm a primary group is to simply do "id myuser" instead of your approach.

    Hope this helps!

  • Sandeep Sagar

    Hi Gleb

    Thanks for the reply.

    Sorry its my fault, I got the whole scenario wrong 🙁
    Thanks again for your time and patience.