Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

From X(7) man pages

DISPLAY NAMES
       From the user's perspective, every X server has a display name of the form:

                                  hostname:displaynumber.screennumber
[...]

displaynumber
    [...]
    To avoid confusion, each display on a machine is assigned a display
    number (beginning at 0) when the X server for that display is started.

How can I find the display number I have been assigned?

Or list currently open displays and their users?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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.

share|improve this answer
    
Thanks, consulting DISPLAY is the answer I think. However, the DISPLAY variable may be outdated. This happened to me when reattaching to a screen session. It caused graphical things not to work. I solved it by detaching, echoing DISPLAY, reattaching, and setting DISPLAY to the new value. –  ricab Sep 19 '13 at 13:02

https://gitorious.org/termsql/pages/Home

termsql is a good tool to use for extracting information like that.

Finding out the DISPLAY number is one of the examples:

export DISPLAY=$(ps aux | termsql "select COL11 from tbl where COL10 like '%Xorg.bin%' limit 1")

(set DISPLAY environment variable to what display X is running on right now, assuming that the X binary is called "Xorg.bin")

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.