How to List Groups in Linux?

Highlights

  • Groups in Linux are a collection of users and have the purpose of privileges management.
  • For efficiency, you would want to create groups, list and sort the existing ones, and more.
  • I have covered you all with how specific commands will let you do so.
Advertisement
Linux
TechLatest is supported by readers. We may earn a commission for purchases using our links. Learn more.

Linux, an open-source software (Operating system), has multiple advanced features that conventional OS doesn’t offer. One of these is Linux List groups.

Advertisement

With Linux also, you can add multiple users to access your PC. However, you would want their privileges and access rights to vary. 

Like who will get read privileges, which users can write (edit or modify data), who can execute advanced commands, and similar.

Here’s where listing groups help. These support multiple users into groups for efficient permissions and access rights management. For this purpose, different commands are used, and as a rule of thumb, you can execute them in the Terminal. 

Groups in Linux 

Just like Linux helps to well-categorize users and control their access rights similarly, groups are also correctly set up. Thus, there are two kinds of Linux groups. 

Primary or Login Group 

Whenever you, as a Linux user, create any files, then those are included in Primary or login groups. Its group names are easy to remember, mainly representing the user’s name. Every user should have only one primary group. 

Secondary or Supplementary Group 

Secondary groups help to assign privileges and access rights to one or multiple users. Every user can have zero to multiple secondary groups they are a part of! 


How To Create User Groups on Linux?

If you don’t want to view or list all groups in Linux but rather create newer ones, you can do that too! However, a generic user cannot do so; hence you need to be a superuser to possess administrative privileges. 

After opening the Terminal, execute the following command:

Advertisement
sudo groupadd <groupname>

Substitute groupname with the name of the group that you want to keep. Besides, you should have sudo (administrative privileges, as mentioned earlier). 

It will create a new group! 

For instance, using 

sudo groupadd authors 

This will create a new group of authors. Further, you also need to assign users to the group with the usermod command! 

Advertisement

For example:

sudo usermod -a -G authors john

The command will add user “john” to the group “authors.” 

Similarly, you can create multiple groups in Linux. 


What Are The Ways for Listing Linux Groups? 

There are three ways to list Linux groups depending on the information they retrieve and their simplicity. 

Advertisement

1. With /etc/group File

One of the most used commands for listing Linux groups is the /etc/group command. It defines the groups present in Linux. The information it fetches includes group names, group IDs, passwords, and members for the respective ones. 

cat /etc/group
cat etc group - List Groups in Linux

The command displays the group names, colons, and additional column information. All you have to do is execute the command in Terminal. However, you would need help to figure out the group names!

cat /etc/group | cut -d: -f1
cat etc group cut -d -f1 - List Groups in Linux

You can use the cut -d -f1 syntax to simplify the information retrieval process. The command displays only the name of group names and not additional information.

cat /etc/group | wc -l

The wc command with the cat /etc group helps you to count all the local groups on Linux. 

Advertisement

2. With getent Command 

In terms of Linux commands, getent stands for fet entries. It retrieves the Linux group databases, which include passwords, usernames, and such data.

To use the command for a user group, type it and the group file name who’s data you want to fetch.

Here’s an instance of the same: 

getent group
getent group - List Groups in Linux

Further, it’s noteworthy that the getent, etc command differs as this one fetches output from databases like LDAP. Next, it retrieves information even from passwords and similar directories. Besides, the, etc command retrieves information in a usual manner. 

Advertisement
getent group | cut -d: -f1

Similar to the etc group, which displays a more extended output where names are difficult to interpret, the same goes for getent. Therefore, you can also use the -d, -f1 syntax along with it. 


3. The Compgen Command 

Compgen is an inbuilt Linux command for displaying all the active and registered groups within the system. 

Here’s how you have to execute the command: 

compgen -g

Here -g represents the groups that you have to display! 

Advertisement

How To View Group List For A Particular User Using Groups On Linux?

Apart from retrieving information on Linux List groups, you can also view group lists from a specific group! 

  • Open Terminal or Shell on Linux
  • Execute the groups <username> command 

Instead of a username, you have to substitute the name of the respective user. 

For instance: 

Executing the command:

Advertisement
$ groups john

will give the output:

john: john adm sudo

Here, john is the user for which the group information is fetched. John, adm, and sudo are the group names to which the user john belongs! 


How to Sort User Groups in Linux?

You can sort user groups in Linux alphabetically, and the getent command helps with the purpose. 

Execute the following command for it: 

Advertisement
getent group | cut -d: -f1 | sort

It fetches the group information using the getent command, parses the output using cut, and then sorts it alphabetically with “sort.”

For example:

adm
audio
cdrom
dialout
docker
...

These are all the user groups in Alphabetical Order. Groups at certain levels aren’t usually accessible, and you need administrative privileges! 


Frequently Asked Questions 

  • Can all groups in Linux be listed in alphabetical order?

    Yes, it is possible to list all groups in Linux in alphabetical order. 

  • How can I assign a user to a specific group in Linux?

    You can use the “usermod -a -G groupname username” command to assign a user to a specific group in Linux. Here, groupname will be the group for which you want to add the user, and username will be the user’s name  


Wrapping Up – List Groups in Linux

If you wanted to learn more about listing groups in Linux, you would have got your answer. The OS uses it to manage users and their privileges effectively. There are two types of groups in Linux: Primary and Secondary.

Advertisement

Further, to fetch information about list groups, three types of commands are available: get, getent, and compgen.

You can even create user groups with the groupadd command. The groups <username> command lets you view all the groups linked to a particular user on Linux. 

Do you have any doubts? Feel free to share them in the comments below!

Further Reading:

Advertisement
Leave a Comment
Related Topics
Subscribe
Notify of
guest
0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
Advertisement