Much like /etc/profile and ~/.profile but ran by root instead of the user is doing login. /etc/rc.local runs after boot but I need running the script before login. The user is not a sudoer one.


  • There may be a way to do this, but it wouldn't be like /etc/profile or ~/.profile because running code in those scripts is done by the user's shell and is subject to control of the user, whereas if you don't want the user to have the power to run programs as root themselves, then this will have to be done by some running service external to the user's processes. ...It might help if you could tell us the purpose of the script you want to run. Nov 24, 2011 at 20:02
  • I need to perform one specific (--bind) mount for one specific user. I have tried fstab with owner and user options, but it does not work. /etc/rc.local is one option but it always performs the mount whether the user logs in or not. I have currently granted sudoer privileges for 'mount' to the user, but this is a workaround and don't like granting any privileges to standard users. I think I will use rc.local instead. Thanks! Nov 24, 2011 at 20:21
  • You could create a root script with only that particular mount command, and give to the user the privilege to run only that script (configuring /etc/sudoers). A command with given options can be set directly in sudoers.
    – enzotib
    Nov 25, 2011 at 8:01
  • I have already tried that way. The thing is that when the time comes for running 'mount' inside the script, again 'sudo' is mandatory, because the script is ran by 'user' and not by 'root', so at the end of the day 'mount' must be granted too for 'user' and instead of just one only sudoers rule now I have two. This is my experience, so please, bear in mind tha I'm not an expert and maybe I have done something wrong. Thanks! Nov 25, 2011 at 10:40

3 Answers 3


Just to go back to the sudoers method, I think you were almost there before you gave up. Looking at your latest comments, I'd just like to address something that will probably fix things for you:

If you run a script as root, you don't need to call sudo from within it.

I have a script like this:

#! /bin/bash

echo $USER

If I run sudo ./myscript I see root returned for both those. The session that script is running in is a root shell.

In short that means that everything you do in your script already has root permissions. You don't need to call sudo (not that it should hurt - root usually has sudo permissions).

So write your script, chown it to root and chmod it to 700 (so only root can run, read, or edit it) and then just allow your user[s] to run it through sudoers. That should work.

If it's not working, it's likely a bigger issue with the script, not the permissions framework. I'd suggest giving a user full sudo access (adding to admin group is the easiest way) and then running your script.


Step 1. create a script with the bind command using any editor. For example:

sudo emacs bind_user_directories.sh



#NOTE: this file would be placed in /usr/local/sbin/ folder as bind_user_directories.sh
#alternatively it could be placed in /etc/init.d/ ... (I guess)

# Provides:          bind_user_directories
# Required-Start:    
# Required-Stop:     
# Should-Start:      $named
# Default-Start:     0 2 3 4 5 6 (I guess...)
# Default-Stop:      1
# Short-Description: mount --bind for a user
# Description:       runs mount --bind command for certain pre-defined directories for a specific user

# What is this?

# See how we were called.
case "$1" in

        log_progress_msg "bind directories for user..."
        sudo mount --bind /source/path /target/path
        log_progress_msg "done: bind directories for user"

        log_progress_msg "umount --bind directories for user..."
        sudo umount /target/path
        log_progress_msg "done: unbind directories for user"

        $0 stop
        sleep 1
        $0 start

        #log_success_msg "Usage: bind_user_directories {start|stop|restart}"
        log_success_msg "Usage: service bind_user_directories <start|stop|restart>"
        exit 1

exit 0

Step 2. save bind_user_directories.sh and make it executable:

chmod a+x bind_user_directories.sh

Step 3. link it to a suitable location such as /usr/local/sbin:

sudo ln -s bind_user_directories.sh /usr/local/sbin/bind_user_directories.sh

Step 4. create the upstart script:

sudo emacs /etc/init/bind_user_directories.conf


description "runs mount --bind command for certain pre-defined directories for a specific user"

start on filesystem and net-device-up IFACE!=lo

stop on runlevel [!023456]
console output
respawn limit 10 5

exec /usr/local/sbin/bind_user_directories.sh start

If this works for you, please let me know. You can check system log for messages after logging in. (I didn't test it yet and I have never implemented anything like this before.) If you improve to the solution, please share your final solution here. Thanks.


To run a scrip as root before the login

  1. You could start the scrip with the Upstart.

  2. You could add the script call to the existing kdm/gdm/etc startup scripts.

Examples: Topic: FAQ: Autostart / Autorun, Periodic run.

  • But what Antonio Sánchez is looking for is how to make a script run as root at the time a particular user logs in. Nov 24, 2011 at 20:00
  • The script must run independently the session is graphic or standard shell. I have updated the title to remark this. I have read something about Upstart, but it looks complicated for the (apparently) simple task it is. Anyway and up to now Upstart seems to be the only way to do it. If you know Upstart, would it be difficult to learn enough as to get this job done? Thanks! Nov 24, 2011 at 20:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .