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 commandgetent命令

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/group
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:syslog
tty:x:5:
disk:x:6:
lp:x:7:
mail:x:8:
news:x:9:
...
Linux List All Group /etc/group File
Linux列出所有组/ etc / group文件

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

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


root@localhost:~# cat /etc/group | grep sudo
sudo:x:27:journaldev,test
root@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 group
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:syslog
tty:x:5:
...
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/group
root
daemon
bin
sys
adm
tty
...
Linux Print All Group Names
Linux打印所有组名

We can use cut command with the getent command too.

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


root@localhost:~# getent group | cut -d: -f1
root
daemon
bin
sys
adm
tty
disk
...
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 | sort
adm
audio
backup
bin
cdrom
crontab
daemon
...
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 ""
68
root@localhost:~# getent group | grep -c ""
68
root@localhost:~#
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 journaldev
journaldev : journaldev sudo test_users test_users_pwd
root@localhost:~# root@localhost:~# groups root
root : root
root@localhost:~#
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:~# groups
root
root@localhost:~# su - journaldev
journaldev@localhost:~$ groups
journaldev sudo test_users test_users_pwd
journaldev@localhost:~$
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 journaldev
uid=1002(journaldev) gid=1003(journaldev) groups=1003(journaldev),27(sudo),1004(test_users),1007(test_users_pwd)
root@localhost:~#
root@localhost:~# id root
uid=0(root) gid=0(root) groups=0(root)
root@localhost:~#
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 sudo
sudo:x:27:journaldev,test
root@localhost:~#
root@localhost:~# getent group sudo | cut -d: -f4
journaldev,test
root@localhost:~#
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)

  • getent man page入门手册页
  • cut command man page剪切命令手册页

翻译自: https://www.journaldev.com/39681/linux-list-all-groups

linux列出组

linux列出组_如何列出Linux中的所有组?相关推荐

  1. linuxl下创建mysql用户和组_实验四 Linux下用户和组的管理

    Linux命令行界面下的用户和组的管理 groupadd创建用户组: # groupadd [-g gid [-o]] [-r] [-f] group 参数: -g gid: group's ID 值 ...

  2. linux mysql怎么样_最强Linux和Mysql面试题套餐,让你的面试无懈可击!

    引言: 大家好,我是一菲,在软件测试当中linux 操作系统和Mysql数据库的内容是十分的知识同时也是十分重要的.所以一菲这两天通过查阅资料等其他方式为大家梳理了liunx和Mysql面试题大礼包, ...

  3. linux uniq命令_如何在Linux上使用uniq命令

    linux uniq命令 Fatmawati Achmad Zaenuri/ShutterstockFatmawati Achmad Zaenuri / Shutterstock The Linux ...

  4. linux gcc 示例_最好的Linux示例

    linux gcc 示例 Linux is a powerful operating system that powers most servers and most mobile devices. ...

  5. mysql和linux的题目_最强Linux和Mysql面试题套餐,让你的面试无懈可击!

    引言: 大家好,我是一菲,在软件测试当中linux 操作系统和Mysql数据库的内容是十分的知识同时也是十分重要的.所以一菲这两天通过查阅资料等其他方式为大家梳理了liunx和Mysql面试题大礼包, ...

  6. linux常用命令_干货:Linux常用命令全称及讲解

    从事IT行业的很多人都会使用Linux常用命令,但是知道这些常用命令全称的人并不多,让我们来看看这些常用命令对应的全称吧!小编精心整理了一下,毕竟常用命令比较多,如果没有你常用的还望海涵,可以评论区补 ...

  7. linux tee 重定向_快乐的linux命令行-重定向

    整理自<快乐的linux命令行一书>. linux系统版本: Ubuntu 17.04 本章,我们将介绍命令行最酷的特性,叫做I/O重定向,通过这个工具,可以重定向命令的输入输出,命令的输 ...

  8. linux cached释放_正点原子Linux第四十一章嵌入式Linux LED驱动开发实验

    1)资料下载:点击资料即可下载 2)对正点原子Linux感兴趣的同学可以加群讨论:935446741 3)关注正点原子公众号,获取最新资料更新 第四十一章嵌入式Linux LED驱动开发实验 上一章我 ...

  9. linux使用单核_如何使用Linux还原单核计算机

    linux使用单核 在上一篇文章中 ,我解释了如何翻新大约5至15年的旧双核计算机. 经过适当还原,这些机器可以托管功能全面的轻量级Linux发行版,如Mint / Xfce , Xubuntu或Lu ...

最新文章

  1. 电力笔记-30个行业专业词汇(Ⅰ期)
  2. 横版网页游戏【一骑当先】地图原画资源
  3. 腾讯接入支付宝 京东、苏宁易购在等是什么
  4. mac mysql语句_Mac 下MySQL使用group by 语句报错解决方法
  5. C#资源,自定义控件等
  6. HashMap 容量为什么总是为 2 的次幂?
  7. The eighteen day
  8. 为什么我电脑的所有浏览器都开不了网页
  9. 用Codeblocks 10.05调试程序
  10. python3.7安装opencv4.1_VS2019+python3.7+opencv4.1+tensorflow1.13配置详解
  11. 使用java,求100以内的质数(素数)
  12. 计算机管理员保密责任书,信息安全保密工作责任书
  13. 管理制度化 制度流程化 流程表单化 表单信息化-企业管理制度应该靠奖罚
  14. Golang的广东11选5出售协程调度机制与GOMAXPROCS性能调优
  15. 虚拟机安装麒麟操作系统网络设置
  16. html设置点击变换图标,【css】css实现点击Toggle功能/icon切换
  17. 漫说从打工者到企业家的蜕变过程所碰到的问题及解决方案
  18. 设置Windows默认登录方式为智能卡登录
  19. websocket实现语音通讯
  20. 解决spark运行时控制台打印冗杂的INFO(屏蔽掉无用的INFO)

热门文章

  1. C#用域账号登陆,访问网络路径
  2. 支持向量机原理(四)SMO算法原理
  3. Spark Streaming源码解读之Driver中ReceiverTracker架构设计以具体实现彻底研究
  4. Oracle 11g完全卸载(Windows)
  5. 怎样设置HTML上传控件,上传文件的大小
  6. [转载] python中callable_Python callable() 函数
  7. [转载] Python杂谈 | (6) numpy中array()和asarray()的区别
  8. 史上最细的FIFO最小深度计算,(大多数笔试题中都会涉及)
  9. centos7卸载旧jdk安装新jdk1.8
  10. Git正确的协作方式(很简单)