shell脚本来获得在Linux上定义的用户列表? [英] Shell script to get list of defined users on Linux?

查看:109
本文介绍了shell脚本来获得在Linux上定义的用户列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把这个在一起,但它吮吸:(!例如幻数在那里,文本解析.. BOO)

I put this together, but it sucks: (e.g. magic numbers in there, text parsing.. boo!)

awk -F: '{if($3 >= 1000 && $3 < 2**16-2) print $1}' /etc/passwd

什么是做到这一点的正确方法?

What's the proper way to do this?

推荐答案

某些UNIX系统中不使用 / etc / passwd文件,或者未指定用户有。您应该使用的 getent passwd文件,而不是读 / etc / passwd文件

Some unix systems don't use /etc/passwd, or have users that are not specified there. You should use getent passwd instead of reading /etc/passwd.

我的系统也有被禁用,用户可以LO与 /斌/假设置为自己的login命令或 / usr / sbin目录不再登录/ NOLOGIN 。你可能要排除他们。

My system also has users that are disabled and can lo longer login with their login command set to /bin/false or /usr/sbin/nologin. You probably want to exclude them as well.

下面是我,包括arheops awk命令和安斯加尔的code,以获得最小和最大什么作品 login.defs

Here is what works for me including arheops awk command and ansgar's code to get the min and max from login.defs:

getent passwd | \
grep -vE '(nologin|false)$' | \
awk -F: -v min=`awk '/^UID_MIN/ {print $2}' /etc/login.defs` \
-v max=`awk '/^UID_MAX/ {print $2}' /etc/login.defs` \
'{if(($3 >= min)&&($3 <= max)) print $1}' | \
sort -u

这篇关于shell脚本来获得在Linux上定义的用户列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆