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,543

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,936
  • 2
    wonderful! I thought it would be simple, didn't expect a single character command though ;) Unfortunately it doesn't solve my xcalib problem though - I'll ask it in another quesion. – drevicko Dec 20 '12 at 02:54
  • I added a xcalib question here in case you're interested. – drevicko Dec 20 '12 at 03:09
  • with KDE I'm getting username :0 ?xdm? /bin/sh /usr/bin/startkde as the first line, so ?xdm? gets shown instead of :0. Any way to avoid this? – int_ua Oct 13 '13 at 10:03
  • 1
    Nitpick: It seems this won't pick up X displays that aren't logged into, such as the X display used by a GDM login screen. These can be found a bit messily via searching processes, e.g. ps aux | grep X, but you separately need w or who to see the displays used by logged in users, it seems. – Gertlex Sep 03 '20 at 18:17
  • No need for sort. w -hs | awk '!u[$3]++{print $3}' :P – user3342816 Jul 23 '21 at 01:29
  • This answer is only true with physical monitors, not with dummy monitors. – adamency Apr 02 '24 at 00:08
2

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
  • 182
  • 1
  • 5
  • This does not work in every situation. ls -al /tmp/.X11-unix/ may list inactive displays, too. My user is listed at X0, X2 and X3 where X2 is the current one. X2 and X3 have the same timestamp, hence using something like ls -altr does not work either. (I need to detect the current display from cron.) – Tino May 03 '24 at 05:41
  • @Tino The question precisely and unambiguously asks for existing displays, not active displays nor "current" one. My message correctly answers the question asked. – adamency Nov 07 '24 at 15:37
  • This approach is definitely better than using w; but if one wants more there is also loginctl. I'm not sure about it working on Ubuntu though (works on a different distro that I have): https://unix.stackexchange.com/questions/203844/how-to-find-out-the-current-active-xserver-display-number – hkoosha Aug 10 '25 at 08:21
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