How to List Groups in Linux

We may receive a commission for purchases made through the links on our site. This helps us keep everything up and running.
How to List Groups in Linux

A guide to different commands and techniques available in Linux to display groups

Within the Linux ecosystem the group of users is referred to as”Group”. When an individual is included in a group, we define the rights of the Linux user. Groups also determine what files as well as settings and folders users should have access to.

In simplest words, groups allow you discover and assign privileges such as write ( r), read ( r) and write ( w) and execute ( x) on a shared resource among the users. You are able to modify the permissions when needed.

Knowing which groups the user’s account is part of can help you understand the rights a specific user is granted and can also alter the permissions as needed.

This brief tutorial will assist you in finding the groups to which users belong by using very basic commands and strategies.

Important Pre-requisites

Before you dive into the course it is beneficial to know the basics of some of the ideas. I would suggest that beginners study these concepts first.

Groups: A collection of users of the Linux system. One user may be part in more than one. A group determines what rights users have.

Principal Group Primary Group: The primary group that is associated to the account of the user. Every user has to be a part of a single primary group. The group is created when that the account for user is created . Once the account is set up, the user is then automatically added to the group. Usually, it is the same name as the group’s primary name is identical to that of the individual user.

Secondary Group:Secondary groups are optional, and users may or might not belong to an additional group. It’s used to give certain privileges to the users. The user could be member of several secondary groups.

the /etc/group file in Linux the membership of groups is managed through the /etc/group file. It’s a text file that contains the groups’ names and the members of the groups.

The /etc/passwd file The file is a record of every user account in the system. There is an entry for one account per line in the file.

Utilizing groups command group command

Utilizing the group command is a easy way to display the groups that the user is currently a part of. It is also possible to make use of this command to display the groups belonging to a particular user who is registered on the system.


Syntax:

groups


Output:

gaurav@ubuntu:~$ groups gaurav adm cdrom sudo dip plugdev lpadmin sambashare gaurav@ubuntu:~$

Locating a group of users with a specific user.


Syntax:

 groups [username groups [username 


Example:

gaurav@ubuntu:~$ groups tomcat tomcat : tomcat lpadmin sambashare gaurav@ubuntu:~$ 

In this case I’ve listed the groups that the user identified as Tomcat is a part of.

Utilizing the ID command

The ID command, you can see the information about the group of the user. It displays parameters such as the uid (user ID), gid (group ID) as well as the listing of groups which the user belongs.


Syntax:

id [username]


Example:

gaurav@ubuntu:~$ id tomcat uid=1002(tomcat) gid=1002(tomcat) groups=1002(tomcat),113(lpadmin),128(sambashare) gaurav@ubuntu:~$

ID command, when not used with an argument, returns the information on the group of the user currently in use.


Example:

gaurav@ubuntu:~$ id uid=1000(gaurav) gid=1000(gaurav) groups=1000(gaurav),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),128(sambashare) gaurav@ubuntu:~$

Here, the group details of the user currently in use is displayed.

Utilizing the /etc/group file

As we discussed in the block that is pre-requisite We know that the the /etc/group file is a complete list of details of the groups that are available in the operating system. It is possible to use this file to browse the groups’ list by using a command that is simple like this.

You can make use of the cat, less or the grep command to display the contents of this file.

 less  less 


Output:

root:x:0: daemon:x:1: bin:x:2: sys:x:3: adm:x:4:syslog,gaurav 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: voice:x:22: cdrom:x:24:gaurav floppy:x:25: tape:x:26: sudo:x:27:gaurav audio:x:29:pulse dip:x:30:gaurav,batman www-data:x:33:

This list will include all groups that are available on Linux. Linux system.

List all Groups that can be accessed using the the getent command

receiveent command is able to show a list of all groups available that are available on the Linux system. The output is comparable to the contents of the the /etc/group file.

Utilizing the the getent group command, you can display the entries of databases that are configured in the /etc/nsswitch.conf file.


Syntax:

getent group


Example:

gaurav@ubuntu:~$ getent group root:x:0: daemon:x:1: bin:x:2: sys:x:3: adm:x:4:syslog,gaurav tty:x:5: disk:x:6: lp:x:7: mse dip:x:30:gaurav,batman :x:39: stmp:x:43: video:x:44: sasl:x:45: plugdev:x:46:gaurav staff:x:50: games:x:60: users:x:100: 106: crontab:x:107: vahi:x:120: bluetooth:x:121: scanner:x:122:saned colord:x:123: pulse:x:124: pulse-access:x:125: rtkit:x:126: saned:x:127: trinity:x:1000: sambashare:x:128:gaurav mongodb:x:130:mongodb guest-tqrhc7:x:999: guest-piinii:x:998: scala:x:997: sbt:x:996: guest-oi9xaf:x:995: tomcat:x:1001: tomcat7:x:132: tomcat8:x:133: geoclue:x:105: gdm:x:134: mysql:x:129: couchdb:x:131: temporary:x:1002:

To find the group of the user you are looking for, make use of the following command.

getent group | grep [username]


Example:

gaurav@ubuntu:~$ getent group | grep gaurav adm:x:4:syslog,gaurav cdrom:x:24:gaurav sudo:x:27:gaurav dip:x:30:gaurav,batman plugdev:x:46:gaurav lpadmin:x:113:gaurav gaurav:x:1000: sambashare:x:128:gaurav gaurav@ubuntu:~$ 

The groups that are that are associated with Gaurav the user are now displayed in the terminal.

Utilizing the libuser-lid command

libuser-lidcommand provides details about groups that contain the user’s name or included in the group name.

Note:This operation will require sudo privileges. Otherwise, you’ll encounter an error like this:

There is no username is specified, Error initiating libuser: Not executing with superuser rights

If the libuser-lid tool isn’t available on your system You can try this command for installation.

To Ubuntu as well as Debian Users:

sudo apt-get update
sudo the following: apt-get install libuser

To CentOS, Fedora and other distros:

sudo yum to install the libuser


Syntax:

sudo libuser-lid [username]


Example:

gaurav@ubuntu:~$ sudo libuser-lid gaurav 


Output:

adm(gid=4) cdrom(gid=24) sudo(gid=27) dip(gid=30) plugdev(gid=46) lpadmin(gid=113) trinity(gid=1000) sambashare(gid=128)

All groups are listed that are linked to the entered username.

Conclusion

In this easy tutorial, we’ve learned to display the groups that are available on Linux systems. The instructions in this tutorial are applicable to all Linux distributions exactly the same way.

Related