Automate execution of shell script owned by non-root user at booting

Hi all, This is really going to be useful. many times we need to execute some commands or shell scripts as non-root user at the time of booting. say for example you need to mount samba share to be mounted as non-root user at time of system boot. Many people do argue that why you need to mount share as non-root user ... A good answer for that argument is, not all applications are running as root user on server. For security reasons it is a good practice to have different non-root users as owner of different applications.

Follow these steps to execute a single command as non-root user at time of bootinig in unix/linux.

  1. edit your rc.local script as this is the script which will be execute immediately after booting. usually it is available under /etc/ directory. type following line of command to mount a external samba share as non-root user.

    su - {userid} -c {COMMAND}

    If you think you have more arguments in your command line or you have more then 1 command to be executed then put them alltogether in one shell script and then use

    su - {userid} -s {shell-script}

    e.g. you need to mount two mounts on single linux servers and those two mounts are on different servers then your shell-script called myscript will become something like. also it is good idea to keep that script in users home directory or in a directory which is accessible by that non-root user other wise it will not work.


      #!/bin/bash
      /usr/bin/smbmount //{first server name OR IP address}/{share name} {first local path to mount} -o username,password rw
      /usr/bin/smbmount //{second server name OR IP address}/{share name} {second local path to mount} -o username,password rw
      exit;

    And for this case your entry in /etc/rc.local will be

    su - {non-root-user} -s {/home/non-root-user/myscript}

Make sure that non root user have valid shell available other wise this will not work

Back to top