Linux group member list

How can I find out which users are in a group within Linux?

I’ve recently been creating new users and assigning them to certain groups. I was wondering if there is a command that shows all the users assigned to a certain group? I have tried using the ‘groups’ command however whenever I use this it says ‘groups: not found’

That is the groups command. It is unlikely that you do not have it on Linux, since it is part of coreutils.

11 Answers 11

I prefer to use the getent command .

Since getent uses the same name service as the system, getent will show all information, including that gained from network information sources such as LDAP.

So for a group, you should use the following .

getent group name_of_group 

where name_of_group is replaced with the group you want to look up. Note that this only returns supplementary group memberships, it doesn’t include the users who have this group as their primary group.

There are a whole lot of other lookups that you can do . passwd being another useful one, which you’ll need to list primary groups.

The other answers doesn’t apply if you are not administrator and the group info is stored in other server.

This could be really confusing probably because of primary/secondary difference. I think this should be avoided in favor of sudo lid -g .I have a system where this answer lists 8 users in a group whereas sudo lid -g lists 10.

grep '^group_name_here:' /etc/group 

This only lists supplementary group memberships, not the user who have this group as their primary group. And it only finds local groups, not groups from a network service such as LDAP.

This could be really confusing probably because of primary/secondary difference. I think this should be avoided in favor of sudo lid -g .I have a system where this answer lists 8 users in a group whereas sudo lid -g lists 10.

This should NOT be the accepted answer. Modern Linux installations have multiple sources for user/group information — not just local /etc/passwd and /etc/group — e.g. nsswitch or sssd . Use getent passwd for user info & getent group for group information — this will cover all modern Linux configurations.

Easier to do groups [username]

If you want to list all local users and their local groups you can do

cat /etc/passwd | awk -F’:’ ‘< print $1>‘ | xargs -n1 groups

If you get «groups: command not found», it is likely you’ve edited your environmental path for the worse, to reset your path do PATH=$(getconf PATH)

It works for a particular group if | grep is added and gives the correct answer unlike getent group name_of_group or grep ‘^group_name_here:’ /etc/group

Instead of cat /etc/passwd , you should use gentent passwd so users in nis/ldap would still be listed. The only drawback is that it can take quite a while.

groupmems -g groupname -l

lists all users in the named group.

Читайте также:  Arch linux no root password

Note that groupmems is part of the shadow utils used on most Linux distros, however groupmems is currently absent from Debian and derivative (a bug now fixed but not included in any release yet (as of Nov 2016))

Also note that groupmems only deals with groups in /etc/group (not the ones in LDAP or other user database) and requires superuser privileges as it tries to open /etc/gshadow.

Despite the caveats mentioned above, this command is ideal for certain situations because it doesn’t require additional parsing of the output (i.e. cut and friends).

This could be really confusing probably because of primary/secondary difference. I think this should be avoided in favor of sudo lid -g . I have a system where this answer lists 8 users in a group whereas sudo lid -g lists 10.

groups command prints group memberships for a user. You can use lid command to list users in a group like:

Update: On Debian based distributions the command name differs as libuser-lid . Both commands are provided by libuser package as @chris-down mentioned.

$ sudo libuser-lid -g lpadmin kadir(uid=xxxx) 

What’s more, on Ubuntu 20.04 LTS, lid is part of the id-utils package. After installation it turned out that this lid does not support the -g option. I understand that Kadir answered 6 years ago, but maybe it’s time to update the information given here.

@LaryxDecidua id-utils manipulates id databases, it doesn’t work with files such as /etc/group or /etc/passwd . Its lid is not at all similar to libuser ’s.

I am surprised nobody mentioned

This command will give a list of groups the user is in.

Because — contrary to the title — the questioner wanted to know the users within a given group, not the groups of a given user, as detailed in the question. I now rephrased the title to match the contents.

Even though , is it different from the actual question, everyone will find this too as a useful information , I bet !

cut -d: -f1,4 /etc/passwd | grep $(getent group | cut -d: -f3) | cut -d: -f1 

I disagree. Because it reads users in /etc/passwd, this will not work with other nsswitch modules that access LDAP etc.

Didn’t work correctly for me: I got 4 members in a group whereas sudo lid -g lists 8. @Bhavik The accepted answer is not correct either.

Works nicely, especially if cut -d: -f1,4 /etc/passwd is replaced with getent passwd | cut -d: -f1,4 . As many people have pointed it out, getent will query non-local information sources.

Some will tell you to install libuser (for ‘lid’) or members (for ‘members’). But building upon the answer https://unix.stackexchange.com/a/349648/77959 which handled this issue with login group membership I found another group not being covered by that script. So — here’s the best of both approaches combined:

#!/bin/bash if [ $# -eq 1 ]; then gid=`getent group "$1"|cut -d: -f3` list_a=`cut -d: -f1,4 /etc/passwd | grep ":$gid$" | cut -d: -f1` list_b=`getent group "$1"|cut -d: -f4|sed 's/,/\n/g'` echo -e "$list_a\n$list_b"|grep -v "^$"|sort|uniq else echo "pass me a group to find the members of" fi 

It worked correctly on my system unlike answers involving getent or grep ‘^group_name_here:’ /etc/group

Читайте также:  Linux profile add path

OP phrased the question to exclude the possibility of using the groups command. Since that is part of coreutils on Linux, either (a) it was removed, or (b) OP is mistyping the name.

OP could have used groups like this, for instance:

for name in $(cut -d: -f1 /etc/passwd);do groups $name|grep -w sudo|awk '';done 

One suggested answer just grep’s for the group name in /etc/group . Sometimes that works as intended.

A slightly better use of grep takes into account the syntax of /etc/group :

group_name:password:GID:user_list 

so that only the part before the first colon is a valid group-name. A plain grep without regard to syntax can (and will) pick up misleading matches from the file. Use regular expressions to make the grep match exactly what is needed:

grep -E '^users:' /etc/group |sed -e 's/^.*://' 

or using a shell variable:

grep -E '^'$groupname':' /etc/group |sed -e 's/^.*://' 

However, that only lists those not in a default group. To add those, you need to take into account the password file, e.g., by extracting the group-id number from /etc/group , and printing the users whose default group matches from /etc/passwd , e.g.,

You could do the same thing using just grep and sed, but it is more work than using awk.

Another suggested answer proposed using getent , which also is likely to be on a Linux machine (with Debian, it is part of GNU libc). However a quick check of that shows it providing only the /etc/group content.

I (like most) do not have libusers or lid installed, so I cannot comment on whether it satisfies OP’s conditions.

There is also the id program, which gives group information. Someone might expand on that as a possible answer.

Источник

Linux List Users/Members in Group Commands & Syntax

Linux List Users/Members in Group Commands & Syntax

In Linux, there is number of users and groups in the system who use and operate the system. The group is nothing but a logical mechanism to manage a collection of users.

There are two types of groups in Linux:,

The Primary group is the main group that the operating system assigns to files that are created by the user. Each user must belong to a primary group.

The Secondary group is any group other than your primary group. Users can belong to up to 15 secondary groups.

If you are a Linux system administrator then it is an essential part of you to find how to list users of a specific group.

In this tutorial, we will show you how to find and list all members of a group in Linux.

Basic Syntax of Groups Command

The basic syntax of groups command is shown below:

You can display all the groups available in your system with the following command:

You should get the following output:

vyom adm cdrom sudo dip plugdev lpadmin sambashare vboxusers chrome-remote-desktop

List All Groups Available in Your System

The simple and easiest way to list all the group available in your system with the following command:

You should see the following output:

root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:syslog,vyom
tty:x:5:
disk:x:6:
lp:x:7:
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:vyom,user1,user2
floppy:x:25:
tape:x:26:
sudo:x:27:vyom
audio:x:29:pulse
dip:x:30:vyom
www-data:x:33:
backup:x:34:
operator:x:37:
list:x:38:
irc:x:39:
src:x:40:
gnats:x:41:
shadow:x:42:
utmp:x:43:
video:x:44:
sasl:x:45:
plugdev:x:46:vyom
staff:x:50:
games:x:60:
users:x:100:
nogroup:x:65534:
libuuid:x:101:
netdev:x:102:
crontab:x:103:
syslog:x:104:
fuse:x:105:
messagebus:x:106:
ssl-cert:x:107:postgres
lpadmin:x:108:vyom
scanner:x:109:saned
mlocate:x:110:
ssh:x:111:
utempter:x:112:
avahi-autoipd:x:113:
rtkit:x:114:
saned:x:115:
whoopsie:x:116:
avahi:x:117:
lightdm:x:118:
nopasswdlogin:x:119:
bluetooth:x:120:
colord:x:121:
pulse:x:122:
pulse-access:x:123:
vyom:x:1000:
sambashare:x:124:vyom
vboxusers:x:125:vyom
postgres:x:126:
docker:x:999:
mysql:x:127:
chrome-remote-desktop:x:128:vyom
snmp:x:129:
user1:x:1001:
user2:x:1002:

Читайте также:  Acronis boot disk linux

You can also see the sample output in the following screen:

You can list all available groups with the following commands:

List all Members of a Group with Command Line

There are several ways you can list all members of a specific group. In this section, we will show you different methods to find all members of a group.

List all Members of a Group with the /etc/group File

The /etc/group is a file that contains information about all groups. You can use cat with grep command as shown below:

cat /etc/group | grep -i groupname

For example, if you want to list all members of a group named cdrom, run the following command:

cat /etc/group | grep -i cdrom

You should see the following output:

You can also list all members of a group named cdrom using the awk command as shown below:

You should see the following output:

You can also see the sample output in the following screen:

List all Members of a Group with members Command

You can also print all members of a group with members command. By default, members command is not installed in most Linux operating systems. You can install it by running the following command:

After installing members package, you can all members of a specific group with the following command:

For example, to display all the members of a group named cdrom run the following command:

You should see the following output:

List all Members of a Group with lid Command

lid is an another command-line utility that can be used to provide information about groups containing usernames.

By default, lid utility is not installed in most Linux operating systems. You can install it by just running the following command:

Once installed, you can display all users in the group named cdrom with the following command:

You should see the following output:

vyom(uid=1000)
user1(uid=1001)
user2(uid=1002)

With lid command, you can also see the information of all groups that contain user named vyom as shown below:

You should see the following output:

adm(gid=4)
cdrom(gid=24)
sudo(gid=27)
dip(gid=30)
plugdev(gid=46)
lpadmin(gid=108)
vyom(gid=1000)
sambashare(gid=124)
vboxusers(gid=125)
chrome-remote-desktop(gid=128)

You can also see the sample output in the following screen:

Conclusion

In the above tutorial, we learned how to find and list all users of a specific group in Linux. We have also learned different commands to find all the members of a group with examples. I hope you have now enough knowledge to use these commands in the production environment.

Recent Posts

  • Forcepoint Next-Gen Firewall Review & Alternatives
  • 7 Best JMX Monitoring Tools
  • Best PostgreSQL Backup Tools
  • Best CSPM Tools
  • Best Cloud Workload Security Platforms
  • Best Automated Browser Testing Tools
  • Event Log Forwarding Guide
  • Best LogMeIn Alternatives
  • Citrix ShareFile Alternatives
  • SQL Server Security Basics
  • Cloud Security Posture Management Guide
  • Cloud Workload Security Guide
  • The Best JBoss Monitoring Tools
  • ITL Guide And Tools
  • 10 Best Enterprise Password Management Solutions

Источник

Оцените статью
Adblock
detector