Get List of Groups in Linux

Get List of Groups in Linux

When working with Linux system as a administrator, may need to get a list of all groups in the system or to count the number of groups. This tutorial explains how to do that.

Get list of groups

The /etc/group is a text file which stores details about groups in Linux system. File can be viewed with cat command:

cat /etc/group

An example of an /etc/group file:

root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:syslog,john
......
systemd-network:x:102:
systemd-resolve:x:103:
systemd-timesync:x:104:
crontab:x:105:
......

Each line contains information of one group which has 4 fields separated by colons (:):

  • Group name (e.g. adm)
  • Encrypted password (x represents that the password located in the /etc/gshadow file)
  • GID - group ID number (e.g. 4)
  • Username list - a list of users who are members of the group (e.g. syslog,john)

Check if group exists

Various operations can be performed with /etc/group file. We can check whether a group exists in Linux system using the following command:

cat /etc/group | grep -w adm

If the group exists, the command will print group information. No output means that group doesn't exist.

Get only group names

Using the cut command we can print only the first field which contains the group name:

cut -d: -f1 /etc/group

Get the number of groups

Use wc command can print the number of groups in the system as follows:

cat /etc/group | wc -l

Leave a Comment

Cancel reply

Your email address will not be published.