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:
- Unix group name – first field
- Unix group ID (gid) – third field
- 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
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
Sandeep Sagar says
"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 says
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 says
Hi Gleb
Thanks for the reply.
Sorry its my fault, I got the whole scenario wrong 🙁
Thanks again for your time and patience.
Sandeep
Mohan says
1st command doesn't return the group, were as 2nd is returning the group, can you let me knwo what could have gone wrong and what is doifference between them ?
1 Command
# getent group | grep XXX | wc -l
0
2 Command
# getent group XXX
XXX:*:100947: