1

I need to run the following command sudo xhost SI:localuser:root every time I start a machine of mine. So I'd like automate this process. I'm using Ubuntu 12.04 LTS for this.

I tried put this command on my file /etc/rc.local but it isn't working, because when I run xhost should appear:

SI:localuser:<myUser>
SI:localuser:root

The file /etc/rc.local is the following:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

xhost SI:localuser:root

exit 0

Am I missing something?

*PS.: as suggest on the comments** The thing I'm tying to do is described on this question here. Can I do this on a better way?

GarouDan
  • 605

2 Answers2

2

It's not going to work, because:

  1. rc.local runs before the X11 server is started.

  2. rc.local does not know where the X11 server is. In other words, it does not know the right value for $DISPLAY.

    Always keep in mind that there can be multiple X11 servers – Ubuntu supports "fast user switching", so you might have one Xorg instance for yourself, and a second one for the login screen.

  3. rc.local does not have access to any of the currently running X11 servers.

    Think about it: If you need to give access to root using xhost, it means root doesn't have access yet. And rc.local is running as root.

All three points also apply to cronjobs, to udev rules, and to most other things.

grawity
  • 501,077
2

As @grawity very correctly points out, this will not work. If you really want to do this (which seems like a pretty bad idea), create a ~/.xinitrc file with these lines:

#!/usr/bin/env bash
xhost SI:localuser:root

The xhost command does not need to be run as root since you are the owner of the X session. Now, xinitrc is pretty old and I am not sure that it will be read by modern Desktop Environments. You could probably achieve the same thing by tweaking your GNOME session properties.

As a general rule enabling root access to your X server is really not a good idea. Any time you need root to run a graphical program, you can disable access control:

xhost + 
sudo gedit
xhost -

I'm pretty sure you don't need to do this on most modern systems though. What exactly are you trying to do? Programs invoked with sudo should have access to your X server anyway.

terdon
  • 54,564