With the help of a friend I managed to find a workaround: if I run xhost +
as an unprivileged user and then export DISPLAY=:0 as root after a sudo su, it finally works (without the xhost step I'd get an undefined protocol error). However I don't understand why I have this problem in the first place, and it is a laborious workaround - I thought of adding an `export DISPLAY=:0" in root's .bashrc but that's only called if I'm suing, not if I just sudo.

@BenjiWiebe, the thing is that none of this should be necessary in an Ubuntu install. It should work out of the box. Manuhalo, have you changed anything else that might be relevant? Is this a fresh install? Did it used to work?
–
terdonDec 4 '12 at 17:43

@terdon it is the machine I use at work, not really a fresh install. I don't remember messing up with /etc/sudoers but then again I might have just forgotten about it. Anyhow, problem solved, thanks a lot!
–
manuhaloDec 4 '12 at 17:51

1 Answer
1

In general, please remember to include your OS (I imagine you are on Linux) and distribution.

By default, the X server does not allow other users (e.g. root in your case) to connect to an existing X instance. This can be overridden with the xhost + command, after which root can connect to your existing X session.

Exporting DISPLAY should not be necessary, are you sure that it was? You should be ok by simply running xhost + and then sudo gedit or whatever. If that is the case (and you don't mind the slight security risk) you can add xhost + to your ~/.bashrc.

As a horrible workaround, you could run sudo specifying the $DISPLAY variable:

sudo DISPLAY=:0 gedit

To automate, you could add these lines to your ~/.bashrc:

xhost +
alias sudo='sudo DISPLAY=:0'

This is a horrible hack though, and I would like to know how you managed to get yourself in a situation where it is necessary :). As I said in my comment above, this really should work out of the box on Ubuntu (and most other modern desktop Linux systems). I last had to to fiddle with xhost and DISPLAY when I was working with RedHat about 10 years ago...

thanks for your answer, I'm on Ubuntu 12.04 64bit. The DISPLAY export was indeed necessary, as from the error above (and from running echo $DISPLAY) we noticed that that env variable wasn't set for some reason. Adding xhost + in my .bashrc will remove the need for that command, but the problem is actually the DISPLAY export, which needs to be done as root. Ideally I'd like to avoid su-ing into root and just use sudo.
–
manuhaloDec 4 '12 at 17:34

@manuhalo Just an idea, did you make sure that DISPLAY was added to the env_keep variable in /etc/sudoers?
–
BenjiWiebeDec 4 '12 at 17:37

@BenjiWiebe you are right, that was obviously the right thing to do. Turns out my env_keep only had "http_proxy" for some reason. I'd accept your answer if it wasn't a comment :)
–
manuhaloDec 4 '12 at 17:46

@manuhalo Oops. Next time, when I get a brainwave, I will bloat it with unnecessary words and post it as an answer so I will get rep points... and then get downvoted... ;)
–
BenjiWiebeDec 4 '12 at 17:48

@BenjiWiebe, I just upvoted another answer of yours (which deserved it anyway) to make up for it :).
–
terdonDec 4 '12 at 18:06