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
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
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.