如何在Linux(可能还有其他unix)中列出一个组的所有成员?
当前回答
下面的命令将列出属于<your_group_name>的所有用户,但只列出由/etc/group数据库管理的用户,不包括LDAP、NIS等。它也只适用于次要组,它不会列出将该组设置为主要组的用户,因为主要组存储为/etc/passwd.文件中的GID(数字组ID)
grep <your_group_name> /etc/group
其他回答
lid -g groupname | cut -f1 -d'('
使用Python列出组成员:
import grp;打印grp.getgrnam (GROUP_NAME)[3]”
参见https://docs.python.org/2/library/grp.html
有一个方便的Debian和Ubuntu包叫做“members”,它提供了这个功能:
描述:显示一个组的成员;默认为所有成员 Members是groups的补充:groups表示指定用户所属的组,Members表示用户 属于特定群体的 ... 你可以请求主要成员,次要成员,都在 一行,每一行都在单独的行上。
不幸的是,据我所知,没有好的、可移植的方法来做到这一点。如果您尝试解析/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";
}
}
Zed的实现应该扩展到其他一些主要UNIX上。
有人可以使用Solaris或HP-UX硬件吗?没有测试这些案例。
#!/usr/bin/perl
#
# Lists members of all groups, or optionally just the group
# specified on the command line
#
# Date: 12/30/2013
# Author: William H. McCloskey, Jr.
# Changes: Added logic to detect host type & tailor subset of getent (OSX)
# Attribution:
# The logic for this script was directly lifted from Zed Pobre's work.
# See below for Copyright notice.
# The idea to use dscl to emulate a subset of the now defunct getent on OSX
# came from
# http://zzamboni.org/\
# brt/2008/01/21/how-to-emulate-unix-getent-with-macosxs-dscl/
# with an example implementation lifted from
# https://github.com/petere/getent-osx/blob/master/getent
#
# 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";
# Only run on supported $os:
my $os;
($os)=(`uname -a` =~ /^([\w-]+)/);
unless ($os =~ /(HU-UX|SunOS|Linux|Darwin)/)
{die "\$getent or equiv. does not exist: Cannot run on $os\n";}
my $wantedgroup = shift;
my %groupmembers;
my @users;
# Acquire the list of @users based on what is available on this OS:
if ($os =~ /(SunOS|Linux|HP-UX)/) {
#HP-UX & Solaris assumed to be like Linux; they have not been tested.
my $usertext = `getent passwd`;
@users = $usertext =~ /^([a-zA-Z0-9_-]+):/gm;
};
if ($os =~ /Darwin/) {
@users = `dscl . -ls /Users`;
chop @users;
}
# Now just do what Zed did - thanks Zed.
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";
}
}
如果有更好的方法来分享这个建议,请告诉我;我考虑了很多方法,这就是我想到的。
推荐文章
- 在Bash中检查变量是否存在于列表中
- 查看PS命令的全部输出
- Linux命令将域名转换为IP
- 如何从命令行通过mysql运行一个查询?
- 在创建守护进程时执行双fork的原因是什么?
- 如何从命令行在windows中找到mysql数据目录
- 有效地测试Linux上的端口是否打开?
- 如何从另一个文件A中删除文件B中出现的行?
- 对以制表符分隔的文件进行排序
- 使用sudo时未找到命令
- 当有命令行参数时,如何使用GDB分析程序的核心转储文件?
- 如何使用查找命令从列表中查找所有具有扩展名的文件?
- 如何强制从另一个SSH会话分离屏幕?
- 如何将文件指针(file * fp)转换为文件描述符(int fd)?
- Linux Bash中双&和分号有什么区别?