Pergunta

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

..

id username2

uid=1466(username2) gid=513(DomainUsers) groups=513(DomainUsers)

All users in LDAP (/etc/nsswitch.conf is correct, all is correct - but only this user do not show)

Why displayed uid number (1466) instead username?

Foi útil?

Solução

There is an 8-character limit for usernames to be listed in ps -ef (POSIX) or ps aux (BSD-derivatives). Explanation of options

I've searched man pages on both Macs and Linux boxes and did not see that limit recorded there.

The question does routinely appear on forums with the 8-character limit as the answer.

I did finally stumble upon this page in which the "bug" was reported against Debian but closed as being not a bug. They reference this page which 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.

I don't know if this is authoritative, but it does explain the behavior you are seeing with a 9-character username. :)

Maybe someone else can post an answer to a more authoritative link?

Outras dicas

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

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 link in the section STDOUT it 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 support but that is only a guess.

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.

The user defined format specified says to output the user, pid, ppid, stime, tname, time and cmd fields.

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.

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:

ps -eo user:$(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)

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

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 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/}'

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)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top