sudo for X Programs

By default (at least on my machine), it wasn’t possible to open X applications using sudo. For example sudoing xclock resulted in the following error:

$ sudo xclock
No protocol specified
Error: Can't open display: :0.0

The same error appeared even when I executed xclock after running sudo su.

Apperantly, by default the X server doesn’t allow anyone else besides you to open windows on the display. While it’s generally a good thing, it’s annoying if you’re trying to preform some GUI stuff as root. You can use the xauth utility to grant permissions to other users. For example:

$ xhost local:root
non-network local connections being added to access control list

Allows root user connecting from the local machine (like sudo) to access the X display for your user. You can see the current access list by simply typing xhost.

3 thoughts on “sudo for X Programs”

  1. Hello Guy! I have been following your blog for a while.

    Quite liked the source-code posting style. Which plugin do you use for the same?

    I found Github-Gist cool too! One can just push to Github without worrying about updating the blog post.

  2. Hi,

    I’m using wp-syntax. Github-Gist looks very nice, I didn’t hear about it before. There are two things which trouble me with it:
    1. The embedding code is javascript. This means that if I search my blog for some code, it won’t find it in the snippets.
    2. What happens if somebody else pushes changes to my gist? Will it update my post too?

  3. You can get X applications to run as root if you copy DISPLAY and XAUTHORITY into the environment.
    You can make sudo pass them in by something like this:
    Defaults:%wheel env_keep += “DISPLAY XAUTHORITY http_proxy https_proxy ftp_proxy”
    (if you use the “wheel” group to permit users to sudo)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.