Who, and with Whom?
Charly's Column – User Groups
This time Charly investigates the three most frequently asked questions about user groups.
Most people know that every user on a Linux system is also a member of at least one user group. Today we want to look into the three most frequently asked questions about groups: Which groups exist, how many members does a group have and who are those members, and to what groups does a specific user belong?
First off, let's find out which groups exist on our system. There are several ways to do this. One of them is to use the groups
command without further parameters; another one is provided by compgen -g
. The getent group
(Listing 1, line 1) and cat /etc/group
commands also return the same result, with some additional information, including the group password. There is usually an x
here, which means that /etc/gshadow
takes care of that. This is followed by the numeric group ID and a comma-separated list of members.
Listing 1
Users and Groups
01 $ getent group 02 root:x:0: 03 daemon:x:1: 04 sys:x:3: 05 admin:x:4:syslog,charly 06 [...] 07 $ sudo apt install libuser 08 $ sudo libuser-lid -g adm 09 syslog(uid=104) 10 charly(uid=1000) 11 $ groups charly 12 charly : charly adm cdrom sudo dip plugdev lxd lpadmin sambashare 13 $ id charly 14 uid=1000(charly) gid=1000(charly) groups=1000(charly),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),110(lxd),115(lpadmin),116(sambashare) 15 $ grep charly /etc/group | awk -F: '{ print $1 }' 16 adm 17 cdrom 18 sudo 19 dip 20 plugdev 21 lxd 22 charly 23 lpadmin 24 sambashare 25 $ grep charly /etc/group | cut -f1 -d: 26 [...]
The next thing is to find out which members belong to a group. In principle, we have already done this, because the getent group
and cat /etc/group
commands provide this information as well.
Often, however, you need the information to process it in a program. It would be good if you didn't have to disassemble the strings using awk
or cut
. A list with one username per line would be far easier to handle. To generate such a list, I first install the libuser package (line 7). Now I have the libuser-lid
command at my disposal, but I have to call it with sudo
(line 8). The numerical user ID also appears in the output of the command. If desired, this can be disabled with the -n
parameter.
Finally, the whole thing in reverse gear: Now I want to know the groups to which a certain user belongs, for example charly. This can be done quickly and easily with the groups charly
command (line 11). If you need more information, the id charly
command (line 13) will provide it. This output also shows the numerical IDs.
Here, too, a list would be the object of desire, with one group name per line. I don't know of a native command for this, but awk
helps reliably (line 15) and returns the desired listing. If the awk
syntax seems too unwieldy, just use cut
instead (line 25) for identical results. Many roads lead to Rome here.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.
-
VirtualBox 7.1.4 Includes Initial Support for Linux kernel 6.12
The latest version of VirtualBox has arrived and it not only adds initial support for kernel 6.12 but another feature that will make using the virtual machine tool much easier.
-
New Slimbook EVO with Raw AMD Ryzen Power
If you're looking for serious power in a 14" ultrabook that is powered by Linux, Slimbook has just the thing for you.