In the X session, you can always consult the DISPLAY
environment variable to get you current display number (echo $DISPLAY
).
Finding out others' display numbers is tougher. I don't think there is any standard way of getting the information other than asking all the users which display they are connected to, so any attempt to gain this information requires some sort of hackery and will not work for all cases.
There are some options given elsewhere in SE but they won't work on Debian Wheezy, for instance. However, listing the displays currently active is easy, as shown in the post I linked above:
ps axu | grep "X "
will show you the X processes that are currently running, something like the following:
root 2354 0.0 0.5 23380 18292 tty8 Ss+ Sep15 2:36 /usr/bin/X :1 vt8 -br -nolisten tcp -auth /var/run/xauth/A:1-7xlyuc
root 7901 0.0 0.3 17044 12072 tty7 Ss+ Sep14 5:58 /usr/bin/X :0 vt7 -br -nolisten tcp -auth /var/run/xauth/A:0-wckh0a
The first parameter after /usr/bin/X
is the display number prefixed by colon, and the next one shows you the virtual terminal assigned to the session.
You might get you answer from looking at the process list, though:
ps axu |grep -3 "X "
root 1990 0.0 0.0 0 0 ? S Sep14 0:00 [kjournald]
root 1991 0.0 0.0 0 0 ? S Sep14 0:00 [kjournald]
root 1992 0.0 0.0 0 0 ? S Sep14 0:00 [kjournald]
root 2354 0.0 0.5 23380 18292 tty8 Ss+ Sep15 2:36 /usr/bin/X :1 vt8 -br -nolisten tcp -auth /var/run/xauth/A:1-7xlyuc
root 2359 0.0 0.0 5264 2228 ? S Sep15 0:00 -:1
user_1 2378 0.0 0.0 4920 1348 ? Ss Sep15 0:00 /bin/sh /usr/bin/startkde
user_1 2443 0.0 0.0 3868 348 ? Ss Sep15 0:00 /usr/bin/ssh-agent env TMPDIR=/var/tmp /usr/bin/dbus-launch --exit-with-session /usr/bin/openbox-kde-session
--
root 7500 0.0 0.0 6460 1080 ? Ss Sep14 0:00 /usr/sbin/sshd
ntp 7841 0.0 0.0 5352 1760 ? Ss Sep14 0:17 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 108:111
root 7882 0.0 0.0 3964 716 ? Ss Sep14 0:00 /usr/bin/kdm -config /var/run/kdm/kdmrc
root 7901 0.0 0.3 17044 12072 tty7 Ss+ Sep14 5:59 /usr/bin/X :0 vt7 -br -nolisten tcp -auth /var/run/xauth/A:0-wckh0a
root 7906 0.0 0.0 5260 2224 ? S Sep14 0:00 -:0
user_2 7925 0.0 0.0 12924 1560 ? Ssl Sep14 0:00 /usr/bin/lxsession -s LXDE -e LXDE
user_2 7990 0.0 0.0 3868 348 ? Ss Sep14 0:00 /usr/bin/ssh-agent env TMPDIR=/var/tmp /usr/bin/dbus-launch --exit-with-session /usr/bin/startlxde
Look at the lines after /usr/bin/X
- as the X session starts, a window manager is typically started as the user the session belongs to, and there you get the username. In my case user_1 is using KDE and user_2 is using LXDE. However, this solution relies purely on the fact that the kernel happens to be assigning new process IDs in ascending sequence, which might not be the case always.