6

I have installed /usr/local/stata/xstata-mp as root, permissions 755.

When I try to run this program from a non-root account via the terminal, I get:

user@host ~ % /usr/local/stata/xstata-mp
(xstata-mp:8030): Gtk-WARNING **: 10:09:24.384: cannot open display: 0

"That's odd," I think, I just know I set DISPLAY=0 in my .zshrc, "Well, maybe I need to explicitly do so when running this command?" So I try:

user@host ~ % DISPLAY=0 /usr/local/stata/stata-mp
(xstata-mp:8201): Gtk-WARNING **: 10:13:28.638: cannot open display: 0

Edit: per @steeldriver 's comment I have also tried DISPLAY=:0

I have a gander around, and find Why don't gksu/gksudo or launching a graphical application with sudo work with Wayland? which seems promising (although I am not using Wayland, I am on Ubuntu 18.10 and Wayland remains installed). I try the below command as user@host, and as su in an administrative account:

user@host ~ % xhost -si:localuser:root
xhost:  unable to open display "0"

I get desperate, and try:

user@host ~ % DISPLAY=0 xhost -si:localuser:root
xhost:  unable to open display "0"

I try other display numbers all to no avail. I think "I am a newb, maybe localhost really means [LOCAL USER] and try with the permutations mentioned above.

user@host ~ % xhost -si:user:root
xhost:  unable to open display "0"

It turns out I am a newb, but that didn't help. :)

How can I make the xhost magic happen so that Gtk doesn't throw a fit and I can just launch my application (as user@host)?

EDIT 3-22-2019:

pgrep -a Xorg
3907 /usr/lib/xorg/Xorg vt1 -displayfd 3 -auth /run/user/123/gdm/Xauthority -background none -noreset -keeptty -verbose 3
7370 /usr/lib/xorg/Xorg vt2 -displayfd 3 -auth /run/user/1000/gdm/Xauthority -background none -noreset -keeptty -verbose 3
Lexible
  • 1,355
  • 4
  • 16
  • 31
  • The DISPLAY variable would usually have the form :0 rather than plain 0 - also you could check that the X server is running on display :0 (by pgrep -a Xorg for example) – steeldriver Jan 11 '19 at 19:28
  • @steeldriver Please see the output from pgrep -a Xorg in my most recent edit – Lexible Mar 22 '19 at 21:06

4 Answers4

6

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.

Lexible
  • 1,355
  • 4
  • 16
  • 31
  • 2
    This was also my experience! Do you know why DISPLAY was suddenly incorrect? I am experiencing the issue after upgrading Ubuntu to 22.04, and lots of things stopped working (Google Chrome can't run from the terminal but can be launched indirectly via shortcuts in the applications menu). Minecraft has the same issue. Running MC with DISPLAY changed to :1 fixes it for me. Any ideas on a more permanent fix that isn't hacky? – saxbophone Nov 20 '22 at 21:07
  • Can also report after upgrade to Ubuntu 22.04 , I have issues with this. 'who' shows me as on ":1" , for whatever reason. – David Mar 25 '24 at 06:23
3

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.

  • somehow I feel there must also be a tweak like this for non-root users since on focal I really don't get the x11 authentication to work while it was working very well on bionic. – woodz Feb 19 '21 at 17:26
  • I added session optional pam_xauth.so line in the file /etc/pam.d/sudo file and tested with sudo xeyes. It worked. Only this handle worked for me. Thanks. – vega Jul 12 '21 at 11:40
2

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.

1

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.

CEF
  • 11