Internet Software

How to List Groups in Linux?

How to List Groups in Linux
Written by prodigitalweb

A group in Linux is referred to as a collection of users. Defining a set of privileges such as reading, writing, or executing permission for a given resource that is possible to be shared within the group among the users — is the main purpose of the groups. In order to use the privileges it grants, it is possible to add users within the existing group. Let’s have a look at this article to learn how to list groups in Linux.

Everything You Need To Know About Groups In Linux:

Groups indicate the users’ collection with their permissions to the system. Dividing groups is possible based on the permissions of the group level. For instance, a few users may have several permissions to the system.

Details which are related to the users’ groups will be stored at the location /etc/group. As a user, you will be able to see this file and information like encrypted passwords, group names, group identification, & user names.

We have given here an example of a group with the user:

coding_ninjas:r:1004:ninja

Hence, the group name is ‘coding_ninjas’, and the encrypted password is ‘r’. Whereas, ‘1004’ indicates the group identification number while the user’s name is ‘ninja’.

Linux Groups:

A user can belong to two types of groups:

Primary Or Login Group: This group is assigned to the files which the user creates. Normally, the primary group’s name is the same as the user’s name. Every user needs to belong to exactly one primary group.

Secondary Or Supplementary Group: In order to grant some privileges to a set of users, this group is used. Hence, you should know that a user is able to be a member of zero or more secondary groups.

How To List Groups In Linux:

To learn how to list groups in Linux, check this section. Many ways are there to list groups in Linux, and these are as follows:

Use /etc/group File:

You can see the contents of the file /etc / group with the cat command in the text editor. In this file, you can see information about users’ groups in the Linux system. With the help of the command, you can list all the groups. You will see multiple lines in the output. Every line has its encrypted passwords, group names, group identification, & user name.

cat /etc/group

Let’s check this example of the output where the groups are listed using the cat command:

root:x:0:

users:x:100:

developers:x:1000:user1,user2,codingninja

ninjas:x:2000:user3,codingninja,codingninja2

Hence, the/ etc/ group file lists four groups:

You can see here the “root” group containing a group ID (GID) of 0. Besides, it does not list any specific users.

Next, in the example, you will see the “users” group containing a GID of 100. It also doesn’t list any particular users.

The next group called “developers” contains a GID of 1000. It has members like “user1,” “user2,” and “codingninja”.

The “ninjas” group includes a GID of 2000. It has these members: “user3,” “codingninja,” & “codingninja2”.

This file includes all the group information that is available on the system. It is possible to use the file to see the group list with the help of a simple command.

Cat, less, or grep commands can be used to list and open the file’s contents.

less /etc/group

Output:

root:x:0:

daemon:x:1:

bin:x:2:

sys:x:3:

adm:x:4:syslog,andy

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:andy

floppy:x:25:

tape:x:26:

sudo:x:27:andy

audio:x:29:pulse

dip:x:30:andy,batman

www-data:x:33:

It lists the whole groups on the Linux system.

Use The ‘cut’ & ‘sort’ Commands:

You can use the ‘cut’ & ‘sort’ commands to extract the group names & sort them in the file /etc / group. The process is used to not display extra information about the groups.

cut -d: -f1 /etc/group | sort

We have given here an example of the output where groups are listed using the ‘cut’ & ‘short’ commands:

codingninja

codingninja2

developers

ninjas

root

users

In the above-mentioned example, group names are extracted from the file using the cut command. You can see the example where the group names are sorted alphabetically using the sort command. “codingninja” & “codingninja2” are the two groups listed first. And these are followed by “developers,” “ninjas,” “root,” & “users.”

Use The ‘awk’ Command:

You can use the command to list group names from /etc/group file. Hence, you are able to manipulate the data & extract this in a powerful manner.

awk -F: ‘{print $1}’ /etc/group

In this example, you can see the groups listed using the ‘awk’ command:

codingninja

codingninja2

developers

ninjas

root

users

Hence, the ‘awk’ command with the ‘-F’ option specifies the field separator as “:”. Print the first field from every line of the ‘/etc/group’ file — the ‘{print $1}’ statement tells awk. After that, the output is sorted alphabetically.

Use The ‘getent’ Command:

The term getent stands for “get entries”. This Linux command is used to view the contents of system information files called databases. Some good instances of databases are /etc/group, /etc/passwd, & /etc/shadow files.

It is simple to use this command to see user group information on Linux. Your task is to type getent followed by the file which you are willing to see. Hence, it will be the group file.

getent group

The output of this group is a bit different from the cat /etc/group command. The reason is that this command can pull ground information on your system from other similar databases. In order to parse the output, you are able to use the cut command that helps to display only group names:

getent group | cut -d: -f1

This command is versatile. You need to get all entries from the /etc/passwd file to list the names of every user on Linux. After that, you need to parse the output for user names. When you use the getent group command, it will show the entries from databases that are

configured in /etc/nsswitch.conf file.

Syntax:

getent group

Example:

andy@ubuntu:~$ getent group

root:x:0:

daemon:x:1:

bin:x:2:

sys:x:3:

adm:x:4:syslog,andy

tty:x:5:

disk:x:6:

lp:x:7:

mse

dip:x:30:andy,batman

:x:39:

stmp:x:43:

video:x:44:

sasl:x:45:

plugdev:x:46:andy

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:andy

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:

If you are looking for the group of a specific user, you need to use the following command.

getent group | grep [username]

Example:

andy@ubuntu:~$ getent group | grep gaurav

adm:x:4:syslog,andy

cdrom:x:24:andy

sudo:x:27:andy

dip:x:30:andy,batman

plugdev:x:46:andy

lpadmin:x:113:andy

andy:x:1000:

sambashare:x:128:andy

andy@ubuntu:~$

Now, all the groups that are associated with the user gaurav will be listed on the terminal.

List Users With The “who” & “users” Commands:

Details about users logged into the Linux machine are provided by the “who” command. Besides, with the help of the “users” command, you can check who is currently logged into Linux. You can use who & users commands to view all connected users.

Use The ‘id’ Command:

The ‘id’ command lists the group names of the logged-in user. You can use the command with the ‘-Gn’ option.

id -Gn

We have given here the example output of listing the groups using the id command.

user adm cdrom sudo dip plugdev lpadmin sambashare codingninja ninjas

Hence, the id -Gn command is used to list the group names of the logged-in user. The output has different group names like “user,” “adm,” “cdrom,” “sudo,” “dip,” “plugdev,” “lpadmin,” “sambashare,” “codingninja,” & “ninjas”.

With Libuser:

You can use the libuser tool to check groups of a particular user. The full form of the term is “library of users.” Using the command, it is possible to install libuser:

sudo apt install libuser

Run these commands to check the list of groups of a particular user.

sudo libuser-lid

#sudo libuser-lid ubuntu

sudo libuser-lid sudo

sudo libuser-lid root

Note:

  • There is no declaration about any user in the first command. Therefore, the command chooses ‘root’ automatically as the user.
  • The ‘ubuntu’ term is used in the second command as the user. You can see that all groups are associated with the user.
  • For the 3rd command, ‘sudo’ is not a correct user. It is the reason the command is not showing any groups.
  • You can see the ‘root’ user in the last command, which includes one group only.

How To List All Linux System Groups (GUI Method):

In most cases, users choose the CLI procedure for the purpose of listing all groups in your Linux system. But a few people like to use the graphical interface for the same.

In order to list all groups, you can use the GUI process. To do so, follow the below-mentioned steps.

  • Your first job is to open the Settings/ Command Center in the Linux distro.
  • Then, you need to choose “Users and Groups” from the “Administration” section.
  • After that, you can see a new window named Users Settings. Then, you have to tap on the “Manage Groups” option.
  • You have to scroll through the entire list of groups to check group names.

Conclusion:

Linux users should know that the Linux system contains groups & users in different files. Sometimes, you may need the details of a user along with the group they belong to. By applying the commands offered by Linux, it can be achieved. In order to get such information, running some commands is possible. You can also get the entire list of users who are on the Linux system, active users, & group names.

Frequently Asked Questions

How can you create & delete a group in Linux?

You can use the ‘groupadd’ command to generate a new group.

On the other hand, to delete a group, you are capable of using the ‘usermod’ command. Or you can modify the file ‘/etc/group’ manually.

How can you list all the groups of ‘/etc/group’?

You can choose different ways to list groups in Linux. But using the ‘cat’ command is the most basic way of listing all the groups of this file. Your only task is to type cat /etc/group in the text editor.

How can you list all the group names of Linux in a sorted manner?

You can use ‘cut’ and ‘sort’ commands in order to extract & sort the group names in this file / etc / group. You can use the process, if you don’t want to show additional information about the groups.

About the author

prodigitalweb