How to get the display number I was assigned by X

If you know of one process ID running in the user’s session, and you have permission to access that process’ information, you can grep its environment for the DISPLAY variable:

cat /proc/$pid/environ | tr '\0' '\n' | grep ^DISPLAY=

As an example, to list all the main displays used by your UID on the current computer, use:

ps -u $(id -u) -o pid= \
    | xargs -I PID -r cat /proc/PID/environ 2> /dev/null \
    | tr '\0' '\n' \
    | grep ^DISPLAY=: \
    | sort -u

If you are only logged in to one X session, the above should output only one line with something like DISPLAY=:0.


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.


This answer has a one-liner, based on who & grep

https://askubuntu.com/a/230838/152287

who can be abbreviated as w

you can read more at the link including a one-liner to get the value.

It works. Here is raspberrypi (rasbian) output, where the display value is in the FROM column

tim@raspberrypi:~ $ w
 19:19:30 up  8:15,  2 users,  load average: 0.12, 0.11, 0.09
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
pi       tty7     :0               11:04    8:15m  1:15   0.89s /usr/bin/lxsess
tim      pts/0    192.168.0.29     19:17    0.00s  1.00s  0.09s w