19

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?

26

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= | \
    while read pid; do
        cat /proc/$pid/environ 2>/dev/null | tr '\0' '\n' | grep '^DISPLAY=:'
    done | grep -o ':[0-9]*' | sort -u

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

  • Note: If you wanna use this on one line, you need to add a ; before the done. – not2qubit Jun 7 '16 at 8:48
  • @user1147688 thanks, but a newline character works just as well, at least in the shells that I use (Bash and Dash). – Amir Jun 7 '16 at 12:22
  • Brillant ! Thanks. I stole this for unix.stackexchange.com/a/429162/12574 with some enhancements ;) – Gilles Quenot Mar 9 '18 at 5:16
11

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.

  • 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
  • Linux exposes the environment of each process under /proc/$pid/environ, as in cat /proc/$$/environ | tr '\0' '\n' | grep '^DISPLAY='. See my answer for another usage example. – Amir Sep 20 '15 at 9:51
2

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
1

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

termsql is a good tool 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)

1

A small variation on Amir's answer.

ps e will output each command's environment, space separated so it can be passed directly to sed to filter out DISPLAY, giving a simpler command than reading /proc/PID/environ directly. (To read the environments of other users you still need root permissions, though.)

This code will output the names of all logged-in users with X sessions, and their DISPLAY numbers:

#!/bin/bash
declare -A disps usrs
usrs=()
disps=()

for i in $(users);do
    [[ $i = root ]] && continue # skip root
    usrs[$i]=1
done # unique names

for u in "${!usrs[@]}"; do
    for i in $(sudo ps e -u "$u" | sed -rn 's/.* DISPLAY=(:[0-9]*).*/\1/p');do
        disps[$i]=$u
    done
done

for d in "${!disps[@]}";do
    echo "User: ${disps[$d]}, Display: $d"
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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