Linux is a potent operating system that’s used in many industries, including server administration, software development, and cybersecurity. Any Linux user or administrator must be able to manage user groups.
In this article, we will walk you through List Groups Linux, providing you with the skills necessary to successfully navigate the Linux group environment.
Linux Groups
List Groups in Linux is a logical collection of user accounts that enables effective permissions management and resource sharing. Each user account in Linux is linked to one or more groups, and groups offer a practical approach to arrange users according to shared characteristics, such as department, project, or level of access.
Linux group have two type of groups such as Primary and Secondary Groups. let see the difference of this two groups type.
Primary and Secondary Groups
Users might be a part of primary and secondary groups.
- The primary group is the one to which the user’s newly created files are assigned. Typically, the user’s name and the principal group’s name are same. There can only be one primary group per user.
- A secondary group is utilized to provide a group of user’s specific privileges. A user may belong to one or more secondary groups, but not zero.
Methods to List All Groups
There are various commands that you can use to List Groups available on your Linux system, depending on the distribution you are using.
List the Groups of a Specific User
To install libuser, run the following command:
sudo apt install libuser
This command assumes you’re running a Debian-based Linux system like Ubuntu. If you use a different distribution, you may need to modify the package manager command (e.g., yum, dnf, pacman, etc.).
On Linux, you may use a variety of command-line offerings to view a given user’s list of groups. Now to check the list of groups for a specific user, the Following run commands should be used:
sudo libuser-lid ubuntu cloudbooklet@ubuntu:~$ sudo libuser-lid ubuntu adm (gid=4) dialout (gid=20) cdrom (gid=24) audio (gid=29) dip (gid=30) video (gid=44) plugdev(gid=46) ubuntu (gid=1000) cloudbooklet@ubuntu:~$
Use cat command
To view the contents of the /etc/group file using the cat command, follow these steps:
- On your Linux computer, launch a terminal.
- Enter after you type the following command:
cat /etc/group cloudbooklet@ubuntu:~$ cat /etc/group root:x:0: daemon:x:1: bin:x:2: Sys:X:3: adm:x:4:syslog,cloudbooklet ityiexetsi disk:x:6: 8587/8 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:cloudbooklet floppy:x:25: tape:x:26: sudo:x:27:cloudbooklet audio:x:29:pulse dip:x:30:cloudbooklet www-data:x:33:
The output of the /etc/group file can be confusing at first because it contains a lot of information about each group. The first column of the file contains the name of the group, followed by a colon (:). The second column contains the group password, which is normally left blank or set to x. The group ID (GID) is a unique number that identifies the group in the third column. The fourth column contains a list of users who are group members.
You can use the cut command to view a prettified version of the file that displays only the group names. The following command will just display the file’s first column:
cat /etc/group | cut -d: -f1 cloudbooklet@ubuntu:~$ cat /etc/group | cut -d: -f1 root daemon bin Sys adm ityiexetsi disk 8587/8 mail news uucp man proxy kmem dialout fax voice cdrom floppy tape sudo audio dip www-data
The output of the /etc/group file can be confusing at first, but it can be simplified by using the cut command to display only the group names.
You can also count the total number of local groups on your system using the wc command. The following command will count the number of lines in the /etc/group file and display the result:
cat /etc/group | wc -l
Grouping Lists using getent Command
On Linux, the getent command displays the contents of system information files or databases. It provides a convenient way to retrieve information from various databases, such as /etc/group, /etc/passwd, and /etc/shadow. In this section, we’ll look at how to use getent to get group information from the group file.
To list all groups on your Linux system using the getent command, open a terminal and type the following command:
getent group cloudbooklet@ubuntu:~$ getent group root:x:0: daemon:x:1: bin:x:2: Sys:X:3: adm:x:4:syslog,cloudbooklet ityiexetsi disk:x:6: 8587/8 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:cloudbooklet floppy:x:25: tape:x:26: sudo:x:27:cloudbooklet audio:x:29:pulse dip:x:30:cloudbooklet www-data:x:33:
When you run this command, it will provide a list of all the groups that have been defined on your system. The group name, group password (usually “x” for shadowed groups), group ID (GID), and a list of individuals who belong to each group will be returned.
Here’s how to use the cut command to parse the output of the getent group command and display only the group names:
getent group | cut -d: -f1
Listing all Groups with Group Command
The fundamental syntax for the groups command is as follows:
groups username
The username is the user whose groups you want to display. If no username is specified, the command will list the groups for the current user. The command will provide a list of the groups to which the user belongs, one per line.
For example, to list the groups for the user cloudbooklet, run the following command:
groups cloudbooklet cloudbooklet@ubuntu:-$ groups cloudbooklet cloudbooklet: cloudbooklet adm cdrom sudo dip plugdev lpadmin xd sambashare cloudbooklet@ubuntu:~$
Use id Command
Using the id command, below is the command to obtain information on the user cloudbooklet:
id cloudbooklet
The command will provide a list of information about the user, including the user ID (UID), group ID (GID), home directory, and shell used by the user. The result will also list the groups to which the user belongs.
For example, the command’s output could be:
uid=1001(cloudbooklet) gid=1001(cloudbooklet) groups=1001(cloudbooklet),27(sudo)
Use the -n option to publish only the names rather than the numbers. Option -g prints only the primary group, while -G prints all groups.
The following command will print the names of the groups to which the current user belongs:
id -nG cloudbooklet adm cdrom sudo dip plugdev lpadmin sambashare
Also read: You might also find useful our guide on How to Add and Remove Users in Linux
Conclusion
Finally, this instruction has given newcomers a thorough understanding of how to view List Groups in Linux. Users can quickly access group information, such as group names, IDs, and memberships, by utilizing commands such as cut, getent, and id. Mastering these approaches will enable newcomers to manage user groups on their Linux systems efficiently.
Please feel free to share your thoughts and feedback in the comment section below.
How to List Groups in Linux: A Guide for Beginners