7

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

4 Answers4

8

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,557
4

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.

3

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