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.

up vote 27 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.

  • xauth info shows path to authority file – SummerBreeze Apr 7 '15 at 7:11
  • xhost + fixed my problem – danger89 Apr 24 at 11:31
  • Be aware that xhost + completely deactivates authentication and allows everyone to access all application on your screen... – michas Apr 24 at 14:14

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.

Background: On X Window, there is a client/server architecture. When you launch an application you request the X server authorisation to display it. By default once you open a session (you graphically login), you (your user) are obviously allowed to commmunicate with the server and display applications. Other users do not have this permission unless you specify it. xhost is a tool to manipulate the list of permissions. The si indicates that the rule is server side and it authorise the local user foo to display applications. X Window is very powerful in this regard and you can display remote applications locally by playing with the DISPLAY environment variable and xhost (but not limited to them). In older times, when people typed xhost + and implicitely allowed everyone to use their X session, it was possible to display application on their screen for pranks ;-) not so much nowadays as people are less and less using X Window client/server architecture (at least for what I observe in the past 10 yers).

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 these instructions, e.g. I still have a permission denied with audio but video is fine (tested on Fedora 22 with SELinux ON).

  • In my case foo was root, i.e. I had to run xhost si:localuser:root on Ubuntu 17.10. – Karl Richter Dec 26 '17 at 2:12
  • Where do you add the xhost si:localhost<user> command? If no users are logged on, then no one has an X server available to give permission to. – cbcoutinho Mar 12 at 20:44
  • @cbcoutinho The above use case is when someone is logged on and wants to run an XWindow application as someone else on the same host. If you explain your use case (what is the problem you would like to solve) then I can potentially help you. – Huygens Mar 12 at 21:50
  • @Huygens I have a workstation for simulating fluids that is also equipped with a GPU. I render the visualizations with ParaView, a program built on vtk, usually on the workstation itself. ParaView also provides a secure headless client/server rendering model through ssh, which I would like to take advantage of remote instead of using VNC. Without logging into the workstation and executing xhost, I can't utilize the GPU. That means that I can't reboot the machine remotely and still have access to the GPU. – cbcoutinho Mar 13 at 20:55
  • 1
    @cbcoutinho X Window is a client server architecture. When you connect via ssh, your local workstation becomes the client X Window. You could use the xhost with unsafe protocols like rlogin but not ssh. You need to instruct ssh to do it for you. Either use the -X or (better?) -Y flag on ssh, it will do the proper redirection. Of course you then need a local C Server. However with GPU and OpenGL, I am not sure where the rendering/compute happens perhaps it’s on the client side, not the server one. Could be tricky. – Huygens Mar 17 at 22:58

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 +

  • 1
    xhost + does work as a temporary measure before going in as sudo – Octopus Mar 9 '14 at 9:34
  • 1
    xhost + worked for me too, only that they forgot to mention we should run it from the authorizing user's terminal, not from a terminal under sudo su. – nyxee Jan 14 '17 at 8:28
  • 1
    perfect work like boom – Adiii Mar 26 at 16:59

Given that you're on Debian, the simple and supported solution is to arrange for sudo to copy your X11 authorization credentials. pam_xauth is included in the libpam-modules package for exactly this purpose; to use it, you just need to add

session  optional  pam_xauth.so

to your /etc/pam.d/sudo file. You may also choose to add it to su, too. For full information, consult the pam_xauth man page, of course.

What helped me:

  1. You can xauth generate :0 . trusted on the user side, which will generate a new MIT-MAGIC-COOKIE-1
  2. Check the newly created Key with xauth list' asuserandroot(they should be the same if yourxAuthority` variable is pointing to the same file.

  3. Voila, root will access any X-App from terminal, but only temporarily.

To make it permanent, see @Huygens' answer!

Alternative solution :

Cron running under root does not have access to display if the current x user is not root.

We just need to add root user to x, you can do it at login time with a startup script

xhost local:root

For test purpose we can just run than command under current user and relaunch the root cron job

use this command and it will work

sudo cp /home/user/.Xauthority .Xauthority
  • 1
    This will give cp: cannot stat ‘/home/user/.Xauthority’: No such file or directory on any of the 10+ Debian based machines I have access to. – Anthon Aug 31 '16 at 14:29
  • This gives a little context (e.g., you silently expect some current directory) and you don't mention side effects like troubles when doing so in multiple X11 instances. I think this way is a bit hacky. – v6ak Mar 12 at 20:44

Just run the xhost localhost command and everything will work fine.

  • 2
    Please explain in more details what this does, why this is needed, why it should solve the reported problem. That would make your answer more useful. – Filipe Brandenburger Apr 6 at 18:11

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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