在本教程中,我們將了解如何在 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”組,請運行以下命令:
# usermod -aG opensource james
如何列出群組成員
1) 使用 cat /etc/group
正如我們之前看到的,組信息存儲在 /etc/group
. 要顯示此信息,請運行
# cat /etc/group
您將獲得系統定義的組和我們之前創建的組的列表
# opensource:x:1005:james,alice,paul
這裡,
開源 是個 團隊名字
X 代表 加密密碼
1005 代表 組 ID (GID)
詹姆斯、愛麗絲、保羅代表 用戶 存在於群中。
2) 使用成員命令
您可以使用 members
命令列出組中的用戶。
這個的語法是
# members groupname
在這 example, 我們會有
# members opensource
輸出
james alice paul
3) 使用 getent 命令
您還可以使用 getent
命令列出組中的用戶。
下面是語法:
# getent group groupname
為了 example
# 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";
}
}
Save 並退出。
賦予腳本執行權限
# 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
如上所示,我們已經能夠使用 shell 腳本輕鬆完成很多工作。
結論
在這個簡短的教程中,我們向您展示了基本命令和 perl 腳本,您可以使用它方便地顯示組和這些組的成員。 我們很高興您能抽出時間陪我們。 留在這裡獲取更多信息教程。
另請閱讀:
- 106 個 Linux 命令 – 帶示例的簡要概述