如何在Linux(可能还有其他unix)中列出一个组的所有成员?


当前回答

getent group groupname | awk -F: '{print $4}' | tr , '\n'

这有三个部分:

1 - getent group groupname显示“/etc/group”文件中组的行。替代cat /etc/group | grep groupname。

2 - awk只打印用','分隔的单行中的成员。

3 - tr将','替换为新行,并在一行中打印每个用户。

4 -可选:如果用户太多,你也可以使用另一个管道排序。

问候

其他回答

getent group <groupname>;

它可以在Linux和Solaris上移植,并且可以使用本地组/密码文件、NIS和LDAP配置。

你可以在一个命令行中完成:

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

上面的命令列出了所有使用groupname作为主组的用户

如果还希望列出具有groupname的用户作为次要组,则使用以下命令

getent group <groupname> | cut -d: -f4 |  tr ',' '\n'

下面是一个非常简单的awk脚本,它考虑了其他答案中列出的所有常见陷阱:

getent passwd | awk -F: -v group_name="wheel" '
  BEGIN {
    "getent group " group_name | getline groupline;
    if (!groupline) exit 1;
    split(groupline, groupdef, ":");
    guid = groupdef[3];
    split(groupdef[4], users, ",");
    for (k in users) print users[k]
  }
  $4 == guid {print $1}'

我将它用于支持ldap的设置,运行在任何具有符合标准的getent和awk的设备上,包括solaris 8+和hpux。

不幸的是,据我所知,没有好的、可移植的方法来做到这一点。如果您尝试解析/etc/group,就像其他人建议的那样,您将会错过那些将该组作为主要组的用户,以及通过UNIX平面文件以外的机制(即LDAP、NIS、pam-pgsql等)添加到该组的任何用户。

如果我必须自己做这件事,我可能会反过来做:使用id获取系统上每个用户的组(这将拉出对NSS可见的所有源),并使用Perl或类似的东西为发现的每个组维护一个哈希表,并指出该用户的成员。

编辑:当然,这给您留下了一个类似的问题:如何获得系统上每个用户的列表。由于我的位置只使用平面文件和LDAP,所以我只能从两个位置获得一个列表,但对于您的环境可能是这样,也可能不是这样。

编辑2:有人提醒我,getent passwd将返回系统中所有用户的列表,包括来自LDAP/NIS/等的用户。,但getent组仍然会错过仅通过默认组条目成为成员的用户,所以这激发了我写这个快速破解。


#!/usr/bin/perl -T
#
# Lists members of all groups, or optionally just the group
# specified on the command line
#
# Copyright © 2010-2013 by Zed Pobre (zed@debian.org or zed@resonant.org)
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#

use strict; use warnings;

$ENV{"PATH"} = "/usr/bin:/bin";

my $wantedgroup = shift;

my %groupmembers;
my $usertext = `getent passwd`;

my @users = $usertext =~ /^([a-zA-Z0-9_-]+):/gm;

foreach my $userid (@users)
{
    my $usergrouptext = `id -Gn $userid`;
    my @grouplist = split(' ',$usergrouptext);

    foreach my $group (@grouplist)
    {
        $groupmembers{$group}->{$userid} = 1;
    }
}

if($wantedgroup)
{
    print_group_members($wantedgroup);
}
else
{
    foreach my $group (sort keys %groupmembers)
    {
        print "Group ",$group," has the following members:\n";
        print_group_members($group);
        print "\n";
    }
}

sub print_group_members
{
    my ($group) = @_;
    return unless $group;

    foreach my $member (sort keys %{$groupmembers{$group}})
    {
        print $member,"\n";
    }
}

在UNIX(与GNU/Linux相反)中,有listusers命令。有关listusers,请参阅Solaris手册页。

注意,这个命令是开源家宝项目的一部分。我认为它在GNU/Linux中是缺失的,因为RMS不相信组和权限。: -)