Linux 是否有列出所有 Unix 组名称的命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14059916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a command to list all Unix group names?
提问by cavila
I know there is the /etc/group
file that lists all users groups.
我知道有一个/etc/group
文件列出了所有用户组。
I would like to know if there is a simple command to list all user group names in spite of parsing the world readable /etc/group
file. I am willing to create an administrator web page that lists Linux accounts' group names.
我想知道是否有一个简单的命令来列出所有用户组名称,尽管解析了世界可读/etc/group
文件。我愿意创建一个列出 Linux 帐户组名称的管理员网页。
采纳答案by Arpit
To list all local groups which have users assigned to them, use this command:
要列出分配有用户的所有本地组,请使用以下命令:
cut -d: -f1 /etc/group | sort
For more info- > Unix groups, Cut command, sort command
回答by dasup
If you want all groupsknown to the system, I would recommend using getent group
instead of parsing /etc/group
:
如果您希望系统知道所有组,我建议使用getent group
而不是解析/etc/group
:
getent group
The reason is that on networked systems, groups may not only read from /etc/group
file, but also obtained through LDAP or Yellow Pages (the list of known groups comes from the local group file plus groups received via LDAP or YP in these cases).
原因是在网络系统上,组不仅可以从/etc/group
文件中读取,还可以通过 LDAP 或黄页获取(在这些情况下,已知组列表来自本地组文件加上通过 LDAP 或 YP 接收的组)。
If you want just the group names you can use:
如果你只想要组名,你可以使用:
getent group | cut -d: -f1
回答by kenorb
On Linux, macOS and Unix to display the groups to which you belong, use:
在 Linux、macOS 和 Unix 上,要显示您所属的组,请使用:
id -Gn
which is equivalent to groups
utility which has been obsoleted on Unix (as per Unix manual).
这相当于groups
在 Unix 上已经过时的实用程序(根据Unix 手册)。
On macOS and Unix, the command id -p
is suggested for normal interactive.
在 macOS 和 Unix 上,id -p
建议使用该命令进行正常交互。
Explanation of the parameters:
参数说明:
-G
,--groups
- print all group IDs
-n
,--name
- print a name instead of a number, for-ugG
-p
- Make the output human-readable.
-G
,--groups
- 打印所有组 ID
-n
,--name
- 打印名称而不是数字,用于-ugG
-p
- 使输出可读。