34

How do I get a list of current X display names?

Apart from being a useful thing to know, I want this so that (hopefully!) I can use xcalib -invert -alter as suggested in this question to invert the second of two screens on my computer.

drevicko
  • 4,523

3 Answers3

44
w

Yeah, that simple. That's an expanded version of who which shows who is logged in, and where they're connected from. That includes graphical sessions and that will show you all the current X displays, amongst other delicious data.

Here's what I see:

oli@bert:~$ w
 01:07:38 up 5 days, 58 min,  4 users,  load average: 0.40, 0.37, 0.41
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
oli      tty7     :0               Sat00    5days  4:22m  0.94s gnome-session --session=gnome-fallback
oli      pts/4    :0               Sat00   47:09m  0.77s  0.77s /bin/bash
oli      pts/6    :0               Wed02    0.00s  0.12s  0.00s w

You can file that down with various flags (try -hs) and then you can awk/grep away at that if you need to automate. Consider piping the resulting list through sort -u to get unique display strings. Something like this:

oli@bert:~$ w -hs | awk '{print $3}' | sort -u
:0
Oli
  • 299,380
1

The answer by @Oli is very poor in terms of robustness. This is absolutely not the purpose of the w command which is meant to list logged in users on the system, and happens to try retrieving the display for each user session.

But this doesn't work when the monitor is virtual, and can fail in other situations.

Example on a system with virtual display:

 02:07:22 up  1:01,  1 user,  load average: 1.83, 1.72, 1.21
USER     TTY       LOGIN@   IDLE   JCPU   PCPU  WHAT
user               01:13    1:00m  0.00s  0.02s lightdm

Universal Solution

The only absolutely reliable solution for every setup is to look at the unix socket used by the X server for each display, i.e. listing the directory /tmp/.X11-unix/

Ex:

root@host # ls /tmp/.X11-unix/
total 0
srwxrwxrwx 1 root root 0 Apr  2 01:06 X0

X0 is a Unix socket that Xorg uses to communicate raw stream of bytes with the X clients (your graphical programs).

This means that a single display called :0 is connected to the machine. (If the file was X3 this would mean that the display is called :3)

So that's how you can know for sure what displays the display server is using.

adamency
  • 162
0

The FROM column just shows - and IPs for me. As an alternative, all child processes of the gnome session that gdm starts has the DISPLAY environment variable set, which can be queried. Not the most robust, but maybe useful to quickly check:

for p in $(pgrep -f gnome-session-binary); do sudo cat /proc/$p/environ | tr '\0' '\n' | grep DISPLAY; done

To print usernames too:

for p in $(pgrep -f gnome-session-binary); do echo $(ps -o user= -p $p) $(sudo cat /proc/$p/environ | tr '\0' '\n' | grep DISPLAY); done

For just the current user (no sudo needed):

for p in $(pgrep -u $USER -f gnome-session-binary); do cat /proc/$p/environ | tr '\0' '\n' | grep DISPLAY; done

It looks like gnome-session-binary forks a child process, so this produces duplicate lines.

Other desktop environments probably have equivalent shell names to query.

jozxyqk
  • 1,181
  • 1
  • 15
  • 28