Linux终端中的Linux列表组成员

时间:2020-03-05 15:28:34  来源:igfitidea点击:

在本教程中,我们将查看不同方式如何在Linux中列出一个组的成员。
"/etc/group"文本文件存储组信息。
每行有一个条目,其中包含以下信息:

  • 组的名字
  • 密码
  • 组ID(GID)
  • 组用户列表

要拍摄我们所谈论的内容,我们将创建新用户,后来将它们添加到一个名为"OpenSource"的组中。

添加新用户

要将新用户添加到Linux运行以下命令:

# adduser
You'll be prompted to enter the usernanme passsword and other details such as Phone number. For instance , let's add a new user called Andrew
addser andrew
Adding user `andrew' ...
Adding new group `andrew' (1001) ...
Adding new user `andrew' (1004) with group `andrew' ...
Creating home directory `/home/andrew' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for andrew
Enter the new value, or press ENTER for the default
        Full Name []: andrew james
        Room Number []: 45
        Work Phone []: 555-456
        Home Phone []: 987-764
        Other []:
Is the information correct? [Y/n] Y

使用相同的命令和过程,我们可以添加更多用户,在这种情况下,'James','Alice'和'Paul'。

添加一个新组

添加新组,我们需要使用'groupadd'命令。

以下命令将添加新组"openSource"。

# groupadd opensource

要确认该组存在于'/etc/group'运行中:

# grep -i "opensource" /etc/group

将用户添加到组

现在,让我们将新创建的用户添加到组"OpenSource"中。

我们将使用usermod命令为此。

要将用户的"james"添加到"OpenSource"的"OpenSource"运行以下命令:

# usermod -aG opensource james

如何列出组成员

1)使用CAT/etc/group

正如我们之前所看到的那样,小组信息存储在"/etc/group"中。
显示此信息运行

# cat /etc/group

我们将获得系统定义的组列表和我们之前创建的组

# opensource:x:1005:james,alice,paul

其中

OpenSource是组名称X表示加密密码1005表示组ID(GID)James,Alice,Paul表示组中存在的用户)使用成员命令

我们可以使用"成员"命令列出组中的用户。

这个语法是

# members groupname

在这个例子中,我们将拥有

# members opensource

输出

james alice paul

3)使用Getent命令

我们还可以使用"getent"命令"列出组中的用户。

以下是语法:

# getent group groupname

例如

# getent group opensource

输出

opensource:x:1005:james,paul

4)使用Perl脚本

最后,我们可以列出Linux系统中的所有组,并使用Perl脚本显示这些组中的所有成员,如图所示。

首先,使用我们喜欢的文本编辑器创建脚本

# vim userlist.pl
Copy and Paste this script and Save
#!/usr/bin/perl -T
## Lists members of all groups, or optionally just the group
# specified on the command line.
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";
}
}

保存并退出。

给脚本执行权限

# chmod +x userlist.pl

最后,运行脚本

# ./userlist.pl

示例输出

Group opensource has the following members:
james
paul
Group paul has the following members:
paul
Group plugdev has the following members:
ubuntu
Group postfix has the following members:
postfix
Group proxy has the following members:
proxy
Group root has the following members:
root
Group sudo has the following members:
ubuntu
Group sys has the following members:
sys
Group syslog has the following members:
syslog
Group systemd-bus-proxy has the following members:
systemd-bus-proxy
Group systemd-network has the following members:
systemd-network
Group systemd-resolve has the following members:
systemd-resolve
Group systemd-timesync has the following members:
systemd-timesync
Group ubuntu has the following members:
ubuntu
Group uucp has the following members:
uucp
Group uuidd has the following members:
uuidd
Group video has the following members:
ubuntu
Group www-data has the following members:
www-data