博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux列出组_如何列出Linux中的所有组?
阅读量:2529 次
发布时间:2019-05-11

本文共 4665 字,大约阅读时间需要 15 分钟。

linux列出组

Linux groups are a collection of users. They are meant to easily provide privileges to a group of users. In this tutorial, we will look at various ways to list all groups in Linux.

Linux组是用户的集合。 它们旨在轻松地向一组用户提供特权。 在本教程中,我们将研究列出Linux中所有组的各种方法。

列出Linux中所有组的2种方法 (2 Ways to List All Groups in Linux)

  1. /etc/group file

    / etc / group文件
  2. getent command

    getent命令

1. / etc / group文件 (1. /etc/group file)

The /etc/group file contains all the local groups. So, we can open this file and look at all the groups.

/ etc / group文件包含所有本地组。 因此,我们可以打开该文件并查看所有组。

root@localhost:~# cat /etc/grouproot:x:0:daemon:x:1:bin:x:2:sys:x:3:adm:x:4:syslogtty:x:5:disk:x:6:lp:x:7:mail:x:8:news:x:9:...
Linux List All Group Etc Group File
Linux List All Group /etc/group File
Linux列出所有组/ etc / group文件

If you are looking for a specific group, then use the to filter it out.

如果要查找特定的组,请使用将其过滤掉。

root@localhost:~# cat /etc/group | grep sudosudo:x:27:journaldev,testroot@localhost:~#

2. getent命令 (2. getent command)

Linux getent command fetch entries from databases supported by the Name Service Switch libraries. We can use it to get all the groups information from the group database.

Linux getent命令从名称服务交换机库支持的数据库中获取条目。 我们可以使用它从组数据库中获取所有组信息。

root@localhost:~# getent grouproot:x:0:daemon:x:1:bin:x:2:sys:x:3:adm:x:4:syslogtty:x:5:...
Linux Getent Group Command
Linux Getent Group Command
Linux Getent组命令

Let’s look at some more examples of listing all the groups in Linux.

让我们看看列出Linux中所有组的更多示例。

Linux列出所有组名 (Linux List All Group Names)

We can use cut command to print only the group names. This is useful when we are looking for a specific group name presence in a shell script.

我们可以使用cut命令仅打印组名。 当我们在shell脚本中寻找特定的组名存在时,这很有用。

root@localhost:~# cut -d: -f1 /etc/grouprootdaemonbinsysadmtty...
Linux Print All Group Names
Linux Print All Group Names
Linux打印所有组名

We can use cut command with the getent command too.

我们也可以将cut命令与getent命令一起使用。

root@localhost:~# getent group | cut -d: -f1rootdaemonbinsysadmttydisk...
Linux Print All Group Names Getent Cut Command
Linux Print All Group Names getent and cut Command
Linux打印所有组名getent和cut命令

The cut command is splitting every line using the colon (:) delimiter. Then the first field, which is the group name, is selected using the -f1 option.

cut命令使用冒号(:)分隔符分割每一行。 然后,使用-f1选项选择第一个字段,即组名。

以字母顺序列出所有组名 (Listing All Group Names in Alphabetical Order)

The above commands output can be passed to the sort command to print the output in natural sorting order.

上面的命令输出可以传递给sort命令,以自然排序顺序输出输出。

root@localhost:~# getent group | cut -d: -f1 | sortadmaudiobackupbincdromcrontabdaemon...
Linux All Group Names Sort
Linux All Group Names Sort
Linux所有组名排序

所有Linux组的数量 (Count of All the Linux Groups)

If you are interested in the count of the linux groups, use the following commands.

如果您对linux组的数量感兴趣,请使用以下命令。

root@localhost:~# cat /etc/group | grep -c ""68root@localhost:~# getent group | grep -c ""68root@localhost:~#
Linux Count Of All Groups
Linux Count Of All Groups
所有组的Linux数量

列出用户的所有组 (List All Groups of a User)

We can use the groups command to get all the groups of a user.

我们可以使用groups命令来获取用户的所有组。

root@localhost:~# groups journaldevjournaldev : journaldev sudo test_users test_users_pwdroot@localhost:~# root@localhost:~# groups rootroot : rootroot@localhost:~#
Linux List User Groups
Linux List User Groups
Linux列表用户组

当前用户的列表组 (List Groups of the Current User)

If you run the groups command without any user input, it will print the groups of the current user.

如果您在没有任何用户输入的情况下运行groups命令,它将打印当前用户的组。

root@localhost:~# groupsrootroot@localhost:~# su - journaldevjournaldev@localhost:~$ groupsjournaldev sudo test_users test_users_pwdjournaldev@localhost:~$
Linux Current User Groups
Linux Current User Groups
Linux当前用户组

列出用户组以及组ID (List User Groups Along with Group ID)

We can use id command to print the user information. This command lists all the groups along with their group id.

我们可以使用id命令来打印用户信息。 此命令列出所有组及其组ID。

root@localhost:~# id journaldevuid=1002(journaldev) gid=1003(journaldev) groups=1003(journaldev),27(sudo),1004(test_users),1007(test_users_pwd)root@localhost:~# root@localhost:~# id rootuid=0(root) gid=0(root) groups=0(root)root@localhost:~#
Linux User Groups With Group Id
Linux User Groups With Group Id
具有组ID的Linux用户组

列出组中的所有用户 (List All Users of a Group)

We can use the getent command or the /etc/groups file to get all the users that belongs to a group.

我们可以使用getent命令或/ etc / groups文件来获取属于一个组的所有用户。

root@localhost:~# getent group sudosudo:x:27:journaldev,testroot@localhost:~# root@localhost:~# getent group sudo | cut -d: -f4journaldev,testroot@localhost:~#
Linux All Users Of A Group
Linux All Users Of A Group
Linux组中的所有用户

结论 (Conclusion)

The getent command and /etc/group file can be used to get all the Linux groups details. We can use them alongside cut and sort command to present the output in a better way.

getent命令和/ etc / group文件可用于获取所有Linux组详细信息。 我们可以将它们与cut和sort命令一起使用,以更好地呈现输出。

参考资料 (References)

翻译自:

linux列出组

转载地址:http://uqozd.baihongyu.com/

你可能感兴趣的文章
vim删除#开头的行和正则表达式笔记
查看>>
python3 提成计算
查看>>
VBA赋值给指定单元格
查看>>
抽象类和接口总结回顾
查看>>
【语言处理与Python】5.3使用Python字典映射词及其属性
查看>>
设备信息
查看>>
Android Volley框架的使用(三)
查看>>
[错误总结] 控件frame设置无效
查看>>
Redis Java API
查看>>
oracle 查询表的定义语句
查看>>
Android 笔记之 Android 系统架构
查看>>
状压dp终极篇(状态转移的思想)
查看>>
AtCoder Grand Contest 031 B - Reversi
查看>>
完整成功配置wamp server小记
查看>>
build.gradle添加Oracle jdbc6 链接
查看>>
影响系统性能的20个瓶颈--转自开源中国
查看>>
根据ISBN获取豆瓣API提供的图书信息
查看>>
【转】Python中*args和**kwargs的区别
查看>>
git命令简单使用
查看>>
CODEFORCES 125E MST Company 巧用Kruskal算法
查看>>