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
How can we get list of non-system users on linux?
提问by user2436428
Considering that all users with id >= 1000
are 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 gid
is 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 awk
for this task:
您可以awk
用于此任务:
awk -F: ' >= 1000' /etc/passwd
This will split the /etc/passwd
file by colon, then if field 3 (userid) is greater than or equal to 1000, it will print the entire /etc/passwd
record.
这将/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/passwd
which 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 awk
to extract the data from /etc/passwd
. awk
is the most flexible of those, but how about a solution with sed
:
假设系统仅识别本地用户(即记录在 中的用户/etc/passwd
,而不是通过远程服务(如 LDAP、NIS 或 Winbind)进行身份验证的用户),您可以使用grep
、sed
、 或awk
从 中提取数据/etc/passwd
。 awk
是其中最灵活的,但解决方案如何sed
:
sed -n '/^\([^:]\+\):[^:]\+:[1-9][0-9]\{3\}/ { s/:.*//; p }' /etc/passwd
回答by Thomas Dickey
System users (should be) those listed in /etc/passwd
with 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/passwd
UID 小于 1000 的用户。实际数量只是一个约定。非系统用户不需要在那里列出。您可以使用getent和awk获取列表,忽略“nobody”(也是约定):
getent passwd |awk -F : ' >= 1000 && < 65534'
回答by Excalibur
回答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