xhost: unable to open display "0"

For some reason (quite probably having to do with me being an aforementioned newb), although echo $DISPLAY returns :0, if I type who I see that my display is actually :1 (?!), so that if I invoke DISPLAY=:1 /usr/local/stata/stata-mp the program launches without error.

To reiterate my solution of unable to open display #:

  1. type who and note the actual display number (including a colon if present)
  2. for the desired command (e.g., xhost, or /usr/local/stata/stata-mp), type DISPLAY=[INSERT NUMBER YOU NOTED HERE] [x COMMAND HERE]

I will gladly upvote and accept another answer which can help me understand this seemingly discrepant behavior.


Try xhost +si:localuser:root

This tells the xserver you are using (whether on display :0 or :1, or wherever) to accept connections from local users called 'root' - that is, programs with SUID to root, or you did sudo in front of.


None of the xauth or xhost schemes allowed root to open the display in Debian 10 (Buster) or Ubuntu 20.04 from a terminal running root via "su" or an application launched using "sudo".

Somebody (I don't remember who) posted (I don't remember where) to add the following line to /etc/pam.d/su and /etc/pam.d/sudo:

session optional pam_xauth.so

It would be helpful if that were (a) easier to find, or (b) that way in distributions.


Runnig a X window under sudo (or as root) typically fails with an unable to open display ... I have not tried to modify the

/etc/pam.d/su and /etc/pam.d/sudo

file, but another small workaround fixes the display (as root) ala this script:

#!/bin/bash

X=`xauth list $DISPLAY`
sudo  -- bash -c "xauth add $X && $@"

Save it as, say xsudo.sh and call it like a normal sudo command ala

> xsudo.sh gparted

It just adds your per-user xauth cookie to the root-accounts cookies, and hence makes the display work on localhost.