Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
Is that the entire /etc/rc.local? You don't have a shebang line? –  terdon Mar 28 '13 at 17:18
    
I will put the complete fileˆˆ –  GarouDan Mar 28 '13 at 17:19
    
I think this might need to be asked on Ask Ubuntu... –  nerof61 Mar 28 '13 at 17:19
1  
No, the problem is that you're trying to run Firefox as root... –  grawity Mar 28 '13 at 17:27
2  
Why would a cron job need to open a graphical web browser as the root user? This is sounding more and more like an XY problem. What exactly are you trying to do here? –  terdon Mar 28 '13 at 17:31
show 6 more comments

2 Answers

up vote 2 down vote accepted

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.

share|improve this answer
    
Most display managers read (source) ~/.xprofile from their Xsession scripts – I know that GDM, KDM and LightDM do. But I have this feeling that OP is trying to run something on the login screen... –  grawity Mar 28 '13 at 17:29
    
But, IMHO, disabling access control is a much worse idea than giving access to root. Think about it: With xhost + you're giving access to root and to any other user. (And possibly even to anybody over the network, if Xorg was not launched with -nolisten tcp.) –  grawity Mar 28 '13 at 17:30
    
@grawity yeah, hat's why I added xhost - right after it :). –  terdon Mar 28 '13 at 17:32
    
@terdon, very interesting too. Oh, maybe I can try this. I just using this because when my cron tries to open the firefox it crashes. With sudo xhost SI:local user:root I had solve this. I will try to refactory and return. –  GarouDan Mar 28 '13 at 17:34
1  
@GarouDan as grawity said, it is not really different from what you are doing. The thing is we suspect that there is a better way of doing what you want to do but you have not explained what it is you are attempting so we cannot give you the right answer. See XY problem. –  terdon Mar 28 '13 at 17:37
show 3 more comments

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.

share|improve this answer
    
Interesting. Well I need this because my cron should run as root. So, can I think on a solution to perform this command as root after I have a X? –  GarouDan Mar 28 '13 at 17:23
    
gravity, I did another question, explaining what I'm really tying to do. Please take a look to me. I appreciate the help. Thx a lot –  GarouDan Mar 28 '13 at 17:56
add comment

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.