Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I installed debian onto my machine last night. Now, I don't understand why I can't run GUI apps from a terminal when running as root.

For example:

sudo -i
glxgears

Generates the following output:

No protocol specified
Error: couldn't open display :0

But when I first open the terminal I can run glxgears from the user account. Its only after I do sudo -i that the problem crops up. This happens for any GUI app that I try to run. I think its probably related to X11, but I'm not sure.

share|improve this question
    
stackoverflow.com/a/20612084 this worked perfectly for me. –  user88494 Oct 20 '14 at 9:42

3 Answers 3

up vote 11 down vote accepted

Accessing the X server requires two things:

  • The $DISPLAY variable pointing to the correct display (usually :0)
  • Proper authentication information

The authentication information can be explicitly specified via $XAUTHORITY, and defaults to ~/.Xauthority otherwise.

If $DISPLAY and $XAUTHORITY is set for your user, sudo will set them for the new shell, too, and everything should work fine.

If they are not set, they will probably default to the wrong values and you cannot start and X applications.

In Debian $XAUTHORITY is usually not set explicitly. Just add

export XAUTHORITY=~/.Xauthority

to your .bashrc or explicitly say XAUTHORITY=~/.Xauthority sudo ... and everything should work.

You can also use xauth list to check whether proper authentication information are available.

share|improve this answer
    
xauth info shows path to authority file –  SummerBreeze Apr 7 at 7:11

Maybe this can help someone else. I had the same question as you but for a normal user. Let's say I want to start firefox using the user account foo. I'm logged in as bar:

[bar@localhost ~]$ sudo -u foo -H firefox

Sadly that command failed with the same error as in the question (i.e. no protocol specified & cannot open display)

My solution was to simply add the user foo to the list of authorised access to the X server.

xhost si:localuser:foo

And that was it, I was then able to launch Firefox (and other X application) using sudo and the user foo.

I hope this helps others.

PS: I did this in order to launch Firefox in a kind of "jail" (to avoid a vulnerability like for pdf.js in the future). But I quickly found out that calling Firefox via sudo won't allow it to access audio nor the video hardware. But there is one guy which explain clearly how to activate video hardware acceleration and audio when calling Firefox via sudo. YMMV with this instuctions, I still have a permission denied with audio, but video is fine. (tested on Fedora 22 with SELinux ON)

share|improve this answer

You can either

Specify the display to be used on the command line, by adding -display :0.0

or

Set up the environment variable in root's login script (one of .bashrc, .profile, .bash_profile ...).

export DISPLAY=:0.0

You can check whether it's set,

$ env |grep DISPLAY
DISPLAY=:0.0

To open up your display for all users from all hosts as your normal user you can do this with :

xhost +

share|improve this answer
    
xhost + does work as a temporary measure before going in as sudo –  Octopus Mar 9 '14 at 9:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.