小智 239
getent group <groupname>;
Run Code Online (Sandbox Code Playgroud)
它可以在Linux和Solaris上移植,并且可以与本地组/密码文件,NIS和LDAP配置一起使用.
Zed*_*Zed 102
不幸的是,我知道没有好的,可移植的方法来做到这一点.如果您尝试解析/ etc/group,正如其他人所建议的那样,您将错过将该组作为其主要组的用户以及通过UNIX平面文件以外的机制(即LDAP,NIS,已添加到该组)的任何人pam-pgsql等).
如果我自己绝对必须这样做,我可能会反过来做:id用来获取系统上每个用户的组(这会将所有源都提供给NSS),并使用Perl或类似的东西来维护哈希发现每个组的表都注意到该用户的成员身份.
编辑:当然,这会给您留下类似的问题:如何获取系统上每个用户的列表.由于我的位置仅使用平面文件和LDAP,因此我可以从两个位置获取列表,但这可能适用于您的环境,也可能不适用.
编辑2:传递给我的人提醒我getent passwd将返回系统中所有用户的列表,包括来自LDAP/NIS /等的用户,但 getent group仍然会遗漏仅通过默认组条目成员的用户,这样就激励我写这个快速黑客.
#!/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";
}
}
Run Code Online (Sandbox Code Playgroud)
小智 40
使用Python列出组成员:
python -c"import grp; print grp.getgrnam('GROUP_NAME')[3]"
请参阅https://docs.python.org/2/library/grp.html
小智 39
lid -g groupname | cut -f1 -d'('
Run Code Online (Sandbox Code Playgroud)
Jos*_*lli 25
以下命令将列出属于的所有用户<your_group_name>,但仅列出由/etc/group数据库管理的用户,而不是LDAP,NIS等.它也仅适用于辅助组,它不会列出将该组设置为主要组的用户,因为主要组是存储为GID(数字组ID)在文件中/etc/passwd.
grep <your_group_name> /etc/group
Run Code Online (Sandbox Code Playgroud)
Did*_*set 16
以下命令将列出属于的所有用户<your_group_name>,但仅列出由/etc/group数据库管理的用户,而不是LDAP,NIS等.它也仅适用于辅助组,它不会列出将该组设置为主要组的用户,因为主要组是存储为GID(数字组ID)在文件中/etc/passwd.
awk -F: '/^groupname/ {print $4;}' /etc/group
Run Code Online (Sandbox Code Playgroud)
Paw*_*ski 12
以下shell脚本将遍历所有用户并仅打印属于给定组的用户名:
#!/usr/bin/env bash
getent passwd | while IFS=: read name trash
do
groups $name 2>/dev/null | cut -f2 -d: | grep -i -q -w "$1" && echo $name
done
true
Run Code Online (Sandbox Code Playgroud)
用法示例:
./script 'DOMAIN+Group Name'
Run Code Online (Sandbox Code Playgroud)
注意:此解决方案将检查NIS和LDAP用户和组(不仅passwd和group文件).它还会考虑未添加到组但将组设置为主组的用户.
编辑:添加了针对罕见场景的修复,其中用户不属于具有相同名称的组.
编辑:以shell脚本的形式编写; 按照@Max Chernyak aka hakunin建议的状态添加true退出; 丢弃以便偶尔跳过那些.0stderrgroups: cannot find name for group ID xxxxxx
您可以在一个命令行中执行此操作:
cut -d: -f1,4 /etc/passwd | grep $(getent group <groupname> | cut -d: -f3) | cut -d: -f1
Run Code Online (Sandbox Code Playgroud)
以上命令列出了将groupname作为主要组的所有用户
如果还要列出具有groupname作为其辅助组的用户,请使用以下命令
getent group <groupname> | cut -d: -f4 | tr ',' '\n'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
354753 次 |
| 最近记录: |