In Linux, there’s a concept of users (those who use the system), and then there’s the concept of groups, which is basically a logical mechanism to manage a collection of users. To give you a basic idea, groups make it easy to manage users with, say, same security and access privileges. It’s worth knowing that a user can be part of different groups.
If you are a Linux command line newbie, and want to learn a way through which you can print the groups a user belongs to, then you’ll be glad to know there exists a little command line utility – dubbed groups – that lets you do just that. In this tutorial we will quickly discuss how this tool works.
Note: All the commands and instructions mentioned here have been tested on Ubuntu 16.04LTS.
Linux groups command
In layman’s terms, the groups command prints the name of groups a user is part of. Here’s its syntax:
groups [OPTION]… [USERNAME]…
Here’s how the official documentation describes this tool:
Print group memberships for each USERNAME or, if no USERNAME is specified, for the current process
(which may differ if the groups database has changed).
How groups command works?
The groups command is very easy to use – all you have to do is to pass the user-name of the user whose groups you’re interested in knowing.
Following screenshot shows the command in action:
So you can see that the user ‘himanshu’ is part of several groups.
What if no username is passed as input?
The groups command works irrespective of whether you provide it a username or not. In case there’s no username, the tool displays group memberships for the current process.
For example, in a user shell – with user ‘himanshu’ logged-in – the command produced the following output:
And when executed from withing the root shell, the following output was produced:
How to get more information about groups command?
To get more information about groups, use the standard –help and –version command line flags.
Agreed, groups is a very basic command line tool. But it’s handy for situations where-in you just need to know about the groups a user belongs to. There’s nothing much to explain about this tool, but still, if you want, you can take a look its man page by heading here.