3

With Centos 6.6, when I run ps command, the first column is User ID and not user name.

root@cluster:mahmood# ps aux | grep Xvnc
506      11881  0.6  0.1 168580 63164 ?        S    Jun03  24:56 /usr/bin/Xvnc :6 -desktop cluster.hpc.org:6 (haghsheno) -auth /home/mahmood/.Xauthority -geometry 1000x900 -rfbwait 30000 -rfbauth /home/mahmood/.vnc/passwd -rfbport 5906 -fp catalogue:/etc/X11/fontpath.d -pn

Why such thing happens? and how can I change that to user name?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
mahmood
  • 1,211
  • 2
    Is answerd hier why https://unix.stackexchange.com/questions/363910/dbus-launch-and-dbus-daemon-whats-happining/365837#365837 –  Jun 06 '17 at 14:01
  • 1
    Does it show the numeric UID for processes of all users, or just some? Do getent passwd 506 and getent passwd $username work? (I think getent should lookup by number on CentOS) – ilkkachu Jun 06 '17 at 14:19
  • It shows for some users. As pointed by Bahamut, it seems that the user name length is important! – mahmood Jun 06 '17 at 14:21
  • or maybe you have a numeric username: https://unix.stackexchange.com/questions/287077/why-cant-linux-usernames-begin-with-numbers/287079#287079 – Jeff Schaller Jun 06 '17 at 15:09

2 Answers2

3

Unfortunately the default ps does not display a username if the username is longer than 8 chars. If your system is running sssd with LDAP you can use "getent passwd userid" to find the user if they are in the LDAP database or the password file. My username "tvb" almost always shows up. However the username "flonglastname" would not in most cases. Other commands like "w" will show the first 8 chars "flonglas" and truncate the remaining. In the example by @KevinO above, "haldaemon" is 9 chars so that's why it doesn't appear in ps as a username, but a userid instead.

% getent passwd haldaemon
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin

Also, the answer at blog.dbi-services.com helps with a way to specify the field length of the username field even in BSD format ps output. Simplifying their suggestion a little gives:

env PS_FORMAT='user:12,pid,%cpu,%mem,vsz,rss,tty,stat,start,time,command' ps ax | grep haldaemon
haldaemon     2032  0.0  0.0  53100  2020 ?        Ssl    Aug 12 00:01:10 hald

Which provides the desired output.

1

If the id is not inside passwd (grep 506 /etc/passwd) there is no username to display.

In this case add an corresponding entry inside /etc/passwd.

EDIT As mentioned by Jeff Schaller and KevinO:

This answer isn't quite complete! It's better to locate these lines from /etc/passwd, whose third field ($3, separated by colon -F:) contains exactly the value of 506. See first comment from Jeff. My simple example above would show lines containing username yx506, id 123506, and so on.

But anyway: if the userid is missing inside /etc/passwd, this answer could be a solution.

I'll try to be more concrete in future ...

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
ChristophS
  • 565
  • 5
  • 13
  • or awk -F: '$3 == 506' /etc/passwd or getent passwd | awk -F: '$3 == 506' – Jeff Schaller Jun 06 '17 at 15:08
  • Yes, my example could result in more than just one line. Wanted to keep it simple ... ;) Thanks anyway! – ChristophS Jun 06 '17 at 15:12
  • This answer isn't quite complete, as an entry may be in /etc/passwd but still not display the textual user name. As an example, I have from ps -ef a line of :68 1964 1 0 May17 ? 00:00:30 hald (so it is showing the numeric id; most other lines have "root" or "apache" showing), but grep 68 /etc/passwd gives haldaemon:x:68:68:HAL daemon:/:/sbin/nologin. – KevinO Jun 06 '17 at 15:16
  • userid 1506, username tax506, gid 5061, etc etc etc – Jeff Schaller Jun 06 '17 at 15:19
  • @Jeff/Kevin: I hope the edit is acceptable for you ... – ChristophS Jun 06 '17 at 15:33
  • The user doesn't have to be listed in /etc/passwd, it could be in some other user database such as LDAP. – Gilles 'SO- stop being evil' Jun 07 '17 at 21:34
  • By default it is fetched from passwd. There is no hint from OP about custom configuration, so default is to be assumed. – ChristophS Jun 08 '17 at 10:31