How to List All Groups in Linux?

Linux List All Groups.png

Linux groups are a collection of users. They are meant to easily provide privileges to a group of users. In this tutorial, we will look at various ways to list all groups in Linux.

2 Ways to List All Groups in Linux

  1. /etc/group file
  2. getent command

1. /etc/group file

The /etc/group file contains all the local groups. So, we can open this file and look at all the groups.

root@localhost:~# cat /etc/group
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:syslog
tty:x:5:
disk:x:6:
lp:x:7:
mail:x:8:
news:x:9:
...
Image 6
Image 6

If you are looking for a specific group, then use the grep command to filter it out.

root@localhost:~# cat /etc/group | grep sudo
sudo:x:27:journaldev,test
root@localhost:~#

2. getent command

Linux getent command fetch entries from databases supported by the Name Service Switch libraries. We can use it to get all the groups information from the group database.

root@localhost:~# getent group
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:syslog
tty:x:5:
...
Image 7
Image 7

Let’s look at some more examples of listing all the groups in Linux.

Linux List All Group Names

We can use cut command to print only the group names. This is useful when we are looking for a specific group name presence in a shell script.

root@localhost:~# cut -d: -f1 /etc/group
root
daemon
bin
sys
adm
tty
...
Image 8
Image 8

We can use cut command with the getent command too.

root@localhost:~# getent group | cut -d: -f1
root
daemon
bin
sys
adm
tty
disk
...
Image 9
Image 9

The cut command is splitting every line using the colon (:) delimiter. Then the first field, which is the group name, is selected using the -f1 option.

Listing All Group Names in Alphabetical Order

The above commands output can be passed to the sort command to print the output in natural sorting order.

root@localhost:~# getent group | cut -d: -f1 | sort
adm
audio
backup
bin
cdrom
crontab
daemon
...
Image 10
Image 10

Count of All the Linux Groups

If you are interested in the count of the linux groups, use the following commands.

root@localhost:~# cat /etc/group | grep -c ""
68
root@localhost:~# getent group | grep -c ""
68
root@localhost:~#
Image 11
Image 11

List All Groups of a User

We can use the groups command to get all the groups of a user.

root@localhost:~# groups journaldev
journaldev : journaldev sudo test_users test_users_pwd
root@localhost:~# 

root@localhost:~# groups root
root : root
root@localhost:~#
Image 12
Image 12

List Groups of the Current User

If you run the groups command without any user input, it will print the groups of the current user.

root@localhost:~# groups
root
root@localhost:~# su - journaldev
journaldev@localhost:~$ groups
journaldev sudo test_users test_users_pwd
journaldev@localhost:~$ 
Image 13
Image 13

List User Groups Along with Group ID

We can use id command to print the user information. This command lists all the groups along with their group id.

root@localhost:~# id journaldev
uid=1002(journaldev) gid=1003(journaldev) groups=1003(journaldev),27(sudo),1004(test_users),1007(test_users_pwd)
root@localhost:~# 
root@localhost:~# id root
uid=0(root) gid=0(root) groups=0(root)
root@localhost:~# 
Image 14
Image 14

List All Users of a Group

We can use the getent command or the /etc/groups file to get all the users that belongs to a group.

root@localhost:~# getent group sudo
sudo:x:27:journaldev,test
root@localhost:~# 
root@localhost:~# getent group sudo | cut -d: -f4
journaldev,test
root@localhost:~# 
Image 15
Image 15

Conclusion

The getent command and /etc/group file can be used to get all the Linux groups details. We can use them alongside cut and sort command to present the output in a better way.

References

How can I list all user groups on Linux?

You can use the ‘cut’ command in the terminal to list all groups present on your Linux machine. This command extracts information from a specific text file that contains user and group details.

Is there a specific user associated with a group in Linux?

In Linux, each user belongs to at least one primary group. Additionally, users can also be part of secondary groups, granting them a set of specific privileges.

What is the method to list all groups on a Linux distribution like Ubuntu?

To list all groups on a Linux distribution such as Ubuntu, you can use the ‘cut’ command in the terminal. This efficient method provides an overview of all groups that exist on your system.

How can I explain group management on Linux to a new administrator?

Group management in Linux involves associating users with specific groups to define a set of permissions. It is essential for efficient user management and ensuring appropriate access control on the operating system.

What is the rule for listing all user groups on Linux?

The ‘cut’ command, used in combination with appropriate arguments, allows you to list all groups currently existing on a Linux machine. This text manipulation rule is helpful for system administrators for quick group management.

Can you explain how to list users and groups on your Linux terminal?

By using the ‘cut’ command, you can effectively list all users and groups present on your Linux system. This method aids in understanding the user-group associations on the operating system.