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
-
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.
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.