4

I couldn't get any program requiring x11 (e.g. gedit, nautilus) to work with sudo. Using gksudo would just fail silently, while on terminal I received the following error:

sudo nautilus

** (nautilus:4160): WARNING **: Command line `dbus-launch --autolaunch=cced40b4aa1d252bc89d3a6a0000000a --binary-syntax --close-stderr' exited with non-zero exit status 1: Autolaunch error: X11 initialization failed.\n
Could not parse arguments: Cannot open display: 

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.

Any ideas on how to properly solve this issue?

2
  • 1
    @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?
    – terdon
    Dec 4, 2012 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!
    – manuhalo
    Dec 4, 2012 at 17:51

3 Answers 3

3

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...

5
  • 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.
    – manuhalo
    Dec 4, 2012 at 17:34
  • @manuhalo Just an idea, did you make sure that DISPLAY was added to the env_keep variable in /etc/sudoers?
    – BenjiWiebe
    Dec 4, 2012 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 :)
    – manuhalo
    Dec 4, 2012 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... ;)
    – BenjiWiebe
    Dec 4, 2012 at 17:48
  • @BenjiWiebe, I just upvoted another answer of yours (which deserved it anyway) to make up for it :).
    – terdon
    Dec 4, 2012 at 18:06
1

When running applications that required sudo I received the following error:

sudo gadmin-samba 
debug1: client_input_channel_open: ctype x11 rchan 3 win 65536 max 16384
debug1: client_request_x11: request from 127.0.0.1 37270
debug1: channel 1: new [x11]
debug1: confirm x11
X11 connection rejected because of wrong authentication.
debug1: channel 1: free: x11, nchannels 2

(gadmin-samba:2166): Gtk-WARNING **: cannot open display: hostname:10.0


To get applications to run as root or via sudo I had to follow these instructions here

  • login via

    ssh username@machine -X
    
  • than when logged in type

    xauth list $DISPLAY
    
  • The output will look like this

    hostname:10  MIT-MAGIC-COOKIE-1  47c3222525f807fc9e8b5fb56e7fee2a
    
  • Copy the whole line.

  • Than do

    sudo xauth add hostname:10  MIT-MAGIC-COOKIE-1  47c3222525f807fc9e8b5fb56e7fee2a
    
  • or login as root su root or sudo -i and then type

    xauth add hostname:10  MIT-MAGIC-COOKIE-1  47c3222525f807fc9e8b5fb56e7fee2a
    

Prequisite

  • ssh
  • xauth

  • The ssh config file on the server must contain the following lines:

X11Forwarding yes
X11DisplayOffset 10
X11UseLocalhost no
  • SSH on the client side has to be run with capital -X and for troubleshooting -v

    ssh -X -v username@machine
    

Type echo $DISPLAY to check if x11 forwarding is working.

0

I use gksu instead, it takes care of secure xauth forwarding

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .