Command to run as root on boot isn't working

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

Posted 2013-03-28T17:14:56.437

Reputation: 477

Is that the entire /etc/rc.local? You don't have a shebang line? – terdon – 2013-03-28T17:18:20.370

I will put the complete fileˆˆ – GarouDan – 2013-03-28T17:19:32.727

I think this might need to be asked on Ask Ubuntu...

– horIzoN – 2013-03-28T17:19:54.783

Why do you need to add root to the access list? – user1686 – 2013-03-28T17:19:55.567

@nerof61 Linux questions are welcome here. Especially if grawity has noticed them :). – terdon – 2013-03-28T17:20:13.173

I know, just thinking he would get direct answers from pro's at Ask Ubuntu. – horIzoN – 2013-03-28T17:21:02.617

Well, looks like superuser question to me, but I will wait some time after migrate it to AskUbuntu. – GarouDan – 2013-03-28T17:24:02.513

@grawity, I need to run a cron as root and this cron (after other things) should open firefox. The problem is withou this firefox crash when trying to open. – GarouDan – 2013-03-28T17:25:06.690

1No, the problem is that you're trying to run Firefox as root... – user1686 – 2013-03-28T17:27:28.093

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 – 2013-03-28T17:31:37.807

@terdon, I think is a good time to write another question, saying what I'm really trying to doˆˆ – GarouDan – 2013-03-28T17:45:37.300

Answers

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

Posted 2013-03-28T17:14:56.437

Reputation: 45 216

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... – user1686 – 2013-03-28T17:29:04.227

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.) – user1686 – 2013-03-28T17:30:43.280

@grawity yeah, hat's why I added xhost - right after it :). – terdon – 2013-03-28T17:32:06.493

@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 – 2013-03-28T17:34:34.187

@terdon: I noticed, but it's still not any better than xhost +SI:localuser:root; sudo gedit; xhost -SI:localuser:root... – user1686 – 2013-03-28T17:34:56.093

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 – 2013-03-28T17:37:57.123

I tried put host + and host - on the script runned by my cron, but didn't worked. I will open a question saying what I'm trying to do... – GarouDan – 2013-03-28T17:46:33.853

terdon, please see the question that I did linking to this one I appreciate the help. Thx a lot. – GarouDan – 2013-03-28T17:55:55.863

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.

user1686

Posted 2013-03-28T17:14:56.437

Reputation: 283 655

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 – 2013-03-28T17:23:00.350

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 – 2013-03-28T17:56:49.087