Linux 运行进程。为什么显示 uid 号而不是用户名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7440839/
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
Running processes. Why displayed uid number instead username?
提问by Anton Shevtsov
ps -eaf
ps -eaf
..
..
kude 22593 12078 0 09:06 ? 00:00:02 smbd -D hasi 22929 12078 0 09:12 ? 00:00:00 someprog.pl root 22950 43 0 Sep08 ? 00:00:19 [nfsiod] root 24558 43 0 Sep09 ? 00:00:28 [pdflush] root 25320 1 0 00:00 ? 00:00:01 /usr/bin/atop -a -w /var/log/atop/atop_20110916 600 1466 25757 12078 0 10:12 ? 00:00:00 smbd -D root 26752 12078 0 10:32 ? 00:00:01 smbd -D
kude 22593 12078 0 09:06 ? 00:00:02 smbd -D hasi 22929 12078 0 09:12 ? 00:00:00 someprog.pl root 22950 43 0 Sep08 ? 00:00:19 [nfsiod] root 24558 43 0 Sep09 ? 00:00:28 [pdflush] root 25320 1 0 00:00 ? 00:00:01 /usr/bin/atop -a -w /var/log/atop/atop_20110916 600 1466 25757 12078 0 10:12 ? 00:00:00 smbd -D root 26752 12078 0 10:32 ? 00:00:01 smbd -D
..
..
id username2
用户名 2
uid=1466(username2) gid=513(DomainUsers) groups=513(DomainUsers)
uid=1466(username2) gid=513(DomainUsers) 组=513(DomainUsers)
All users in LDAP (/etc/nsswitch.conf is correct, all is correct - but only this user do not show)
LDAP 中的所有用户(/etc/nsswitch.conf 是正确的,都是正确的 - 但只有该用户不显示)
Why displayed uid number (1466) instead username?
为什么显示 uid 号(1466)而不是用户名?
采纳答案by Ray Toal
There is an 8-character limit for usernames to be listed in ps -ef
(POSIX) or ps aux
(BSD-derivatives). Explanation of options
在ps -ef
(POSIX) 或ps aux
(BSD-derivatives) 中列出的用户名有 8 个字符的限制。 选项说明
I've searched man pages on both Macs and Linux boxes and did not see that limit recorded there.
我在 Mac 和 Linux 机器上搜索了手册页,但没有看到那里记录了该限制。
The question does routinely appear on forums with the 8-character limit as the answer.
这个问题确实经常出现在论坛上,答案是 8 个字符的限制。
I didfinally stumble upon this pagein which the "bug" was reported against Debian but closed as being not a bug. They reference this pagewhich also makes a claim about how POSIX and UNIX standards require falling back to uids when names are too long. Also not from the actual POSIX standard.
我也终于绊倒在此页面中,被报道的“错误”针对Debian的,但收盘为不是一个错误。他们引用了这个页面,该页面还声明了当名称太长时 POSIX 和 UNIX 标准如何要求回退到 uids。也不是来自实际的 POSIX 标准。
I don't know if this is authoritative, but it does explain the behavior you are seeing with a 9-character username. :)
我不知道这是否具有权威性,但它确实解释了您使用 9 个字符的用户名所看到的行为。:)
Maybe someone else can post an answer to a more authoritative link?
也许其他人可以发布更权威链接的答案?
回答by another.anon.coward
As Ray Toal mentions it is restricted to 8-character limit. This is not a bug but part of the standard again as mentioned by Ray. If you check the source code of ps
(part of procps
package) one of the comments says
正如 Ray Toal 所提到的,它仅限于 8 个字符的限制。正如Ray所提到的,这不是一个错误,而是标准的一部分。如果您检查ps
(procps
包的一部分)的源代码,其中一条评论说
The Open Group Base Specifications Issue 6 (IEEE Std 1003.1, 2004 Edition)
requires that user and group names print as decimal numbers if there is
not enough room in the column, so tough luck if you don't like it.
The UNIX and POSIX way to change column width is to rename it:
ps -o pid,user=CumbersomeUserNames -o comm
The easy way is to directly specify the desired width:
ps -o pid,user:19,comm
If you check the linkin the section STDOUTit says that the fields (user, ruser, group, rgroup)will printed if it can be obtained and the field width permits, or a decimal representation otherwise.
The reason why user & group name field widths are restricted to 8 could be for legacy supportbut that is only a guess.
如果检查链接的部分STDOUT它说,字段(用户,RUSER,组,R-基团,如果它能够获得否则字段宽度允许,或十进制表示)将打印。
用户和组名称字段宽度限制为 8 的原因可能是为了支持旧版,但这只是猜测。
回答by moosaka
ps -eo user:$(cut -d: -f1 /etc/passwd | wc -L),pid,ppid,c,stime,tname,time,cmd
ps -eo user:$(cut -d: -f1 /etc/passwd | wc -L),pid,ppid,c,stime,tname,time,cmd
The -o
option is used to specify a user defined format for the output of the ps
command.
该-o
选项用于为ps
命令的输出指定用户定义的格式。
The user defined format specified says to output the user, pid, ppid, stime, tname, time and cmd fields.
指定的用户定义格式表示输出用户、pid、ppid、stime、tname、time 和 cmd 字段。
cut -d: -f1 /etc/passwd | wc -L
determines the number of characters in the longest login name in the password file. Therefore user:$(cut -d: -f1 /etc/passwd | wc -L)
tells the ps command to output the user field using the maximum length of the longest login name.
cut -d: -f1 /etc/passwd | wc -L
确定密码文件中最长登录名的字符数。因此user:$(cut -d: -f1 /etc/passwd | wc -L)
告诉 ps 命令使用最长登录名的最大长度输出用户字段。
回答by Matija Nalis
For even more dynamic length output that moosaka's answer (for example, if you have few very long usernames, but which are very rarely used and you don't want screen wasted most of the time), you can use:
对于更动态的长度输出,moosaka 的答案(例如,如果您的用户名很少,但很少使用,并且您不希望大部分时间浪费屏幕),您可以使用:
ps -eo user:$(ps axho uid | sort -u | xargs getent passwd | cut -f1 -d: | wc -L),pid,ppid,c,stime,tname,time,cmd
ps -eo 用户:$(ps axho uid | sort -u | xargs getent passwd | cut -f1 -d: | wc -L),pid,ppid,c,stime,tname,time,cmd
It will make the lenght of the username column just as long as longest username of currently running process. (Note that it is not bulletproof however, and if new process with longer username starts in the split second while command is running, you might still get a number displayed. But for 99.99% of the time it's much nicer output)
它将使用户名列的长度与当前正在运行的进程的最长用户名一样长。(但是请注意,它不是防弹的,如果在命令运行的瞬间启动了具有更长用户名的新进程,您可能仍然会显示一个数字。但在 99.99% 的时间里,它的输出要好得多)
Explanation: $(ps axho uid [...] | wc -L)
calculates maximum username length of CURRENTLY running process, and then we execute normal ps with that length of username
说明:$(ps axho uid [...] | wc -L)
计算当前运行进程的最大用户名长度,然后我们用那个长度的用户名执行正常的ps
Alternatively, if you want ps to look like usual for short usernames (<=8 chars), and do not mind for few long usernames in output to be misaligned with headers, you can do something like:
或者,如果您希望 ps 看起来像通常的短用户名(<=8 个字符),并且不介意输出中的几个长用户名与标题不对齐,您可以执行以下操作:
ps ax -o user:40,pid,ppid,c,stime,tname,time,cmd | perl -pe 'if (/^(\S+)/ and length $1 > 8) {s/^(\S+)\s+/$1 /} else { s/^(.{9})\s+/$1/}'
ps ax -o 用户:40,pid,ppid,c,stime,tname,time,cmd | perl -pe 'if (/^(\S+)/ 并且长度 $1 > 8) {s/^(\S+)\s+/$1 /} else { s/^(.{9})\s+/$1/} '
what that does is make output username column very long (-o user:40
), and then postprocesses the output so long usernames (length $1 > 8
) have just one space between them and next column, and short usernames (else {
) are trimmed back to default (up to 8 chars username, and the rest up to 9th character are spaces)
这样做是使输出用户名列非常长 ( -o user:40
),然后对输出进行后处理,以便长用户名 ( length $1 > 8
) 与下一列之间只有一个空格,并将短用户名 ( else {
) 修剪回默认值(最多 8 个字符的用户名,其余最多第 9 个字符是空格)