bash 我们如何获得Linux上的非系统用户列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33150365/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:44:46  来源:igfitidea点击:

How can we get list of non-system users on linux?

linuxbashshellunixubuntu

提问by user2436428

Considering that all users with id >= 1000are non-system users, how can we get list of these users in a single command?

考虑到所有用户id >= 1000都是非系统用户,我们如何在单个命令中获取这些用户的列表?

回答by Diogo Rocha

You need to get all users whose gidis greater than or equals 1000. Use this command for that:

您需要获取gid大于或等于 1000 的所有用户。为此使用此命令:

awk -F: '(>=1000)&&(!="nobody"){print }' /etc/passwd

If you want system users (gid<1000) it will be:

如果您想要系统用户 (gid<1000),它将是:

awk -F: '(<1000){print }' /etc/passwd

回答by JNevill

You can use awkfor this task:

您可以awk用于此任务:

awk -F: ' >= 1000' /etc/passwd

This will split the /etc/passwdfile by colon, then if field 3 (userid) is greater than or equal to 1000, it will print the entire /etc/passwdrecord.

这将/etc/passwd通过冒号拆分文件,然后如果字段 3(用户 ID)大于或等于 1000,它将打印整个/etc/passwd记录。

If you want to get only the username out of this list then:

如果您只想从此列表中获取用户名,则:

awk -F: ' >= 1000 {print }' /etc/passwd

Where $1 is the first field of etc/passwdwhich is the username.

其中 $1 是第一个字段etc/passwd是用户名。

回答by John Bollinger

Supposing that the system recognizes only local users (i.e. those recorded in /etc/passwd, as opposed to any authenticated via a remote service such as LDAP, NIS, or Winbind), you can use grep, sed, or awkto extract the data from /etc/passwd. awkis the most flexible of those, but how about a solution with sed:

假设系统仅识别本地用户(即记录在 中的用户/etc/passwd,而不是通过远程服务(如 LDAP、NIS 或 Winbind)进行身份验证的用户),您可以使用grepsed、 或awk从 中提取数据/etc/passwdawk是其中最灵活的,但解决方案如何sed

sed -n '/^\([^:]\+\):[^:]\+:[1-9][0-9]\{3\}/ { s/:.*//; p }' /etc/passwd

回答by Thomas Dickey

System users (should be) those listed in /etc/passwdwith UIDs less than 1000. The actual number is a convention only. Non-system users need not be listed there. You can get the list using getentand awkignoring "nobody"(also a convention):

系统用户(应该)是那些/etc/passwdUID 小于 1000 的用户。实际数量只是一个约定。非系统用户不需要在那里列出。您可以使用getentawk获取列表,忽略“nobody”(也是约定):

getent passwd |awk -F : ' >= 1000 &&  < 65534'

回答by Excalibur

Here's an answer for doing this on allyour machines, using Ansible and awk, building on JNevill'sanswer:

这是在所有机器上执行此操作的答案,使用 Ansible 和awk,以JNevill 的答案为基础:

ansible -i inventories/cd_staging all -m shell -a "awk -F: '$3 >= 1000 && $7 \!~ /nologin/ {print $1}' \/etc\/passwd |sort"

回答by Nick ODell

You'll want to ignore GIDs less than 1000, but also GIDs greater than 60000. Ubuntu/Debian reserve these for various system services.

您将要忽略小于 1000 的 GID,但也要忽略大于 60000 的 GID。Ubuntu/Debian 为各种系统服务保留这些。

awk -F: '(>=1000)&&(<60000)&&(!="nobody"){print }' /etc/passwd