I have init script which should run as non root user. How can I make it? Ubuntu 12.04#!/bin/sh DEPLOY_BIN=/usr/share/deploy/binPID_FILE=/var/run/deploy.pid PATH=/bin:/usr/bin:/sbin:/usr/sbin:$DEPLOY_BIN DAEMON=$DEPLOY_BIN/reportingNAME=deploy DESC=Deploy if [ `id -u` -ne 0 ]; then        echo You need root privileges to run this script        exit 1 fi . /lib/lsb/init-functions if [ -r /etc/default/rcS ]; then        . /etc/default/rcS fi case $1 in  start)        log_daemon_msg Starting $DESC        pid=`pidofproc -p $PID_FILE deploy`        if [ -n $pid ] ; then                log_begin_msg Already running.                log_end_msg 0                exit 0        fi        # Start Daemon        start-stop-daemon --start --pidfile $PID_FILE --make-pidfile --background --exec $DAEMON        log_end_msg $?        ;;  stop)        log_daemon_msg Stopping $DESC        if [ -f $PID_FILE ]; then                start-stop-daemon --stop --pidfile $PID_FILE \                        --retry=TERM/20/KILL/5 >/dev/null                if [ $? -eq 1 ]; then                        log_progress_msg $DESC is not running but pid file exists, cleaning up                elif [ $? -eq 3 ]; then                        PID=`cat $PID_FILE`                        log_failure_msg Failed to stop $DESC (pid $PID)                        exit 1                fi                rm -f $PID_FILE        else                log_progress_msg (not running)        fi        log_end_msg 0        ;;  status)        status_of_proc -p $PID_FILE  deploy && exit 0 || exit $?    ;;  restart|force-reload)        if [ -f $PID_FILE ]; then                $0 stop                sleep 1        fi        $0 start        ;;  *)        log_success_msg Usage: $0 {start|stop|restart|force-reload|status}        exit 1        ;; esac exit 0
How can I make this deploy/init run as a non root user?
shell script;ubuntu
Solution 1: sudo rulesAdd to your sudoers file (using visudo) the following rule:<user_name> ALL=(root) NOPASSWD:<path/to/your/script>Replacing <user_name> to your non-root user login name and <path/to/your/script> with the absolute path to what you want to execute.The same operation may be done by placing that rule inside a file under /etc/sudoers.d. Note that such file must be owned by root:root and have the mode 0440 in order to work with sudo.The NOPASSWD flag is optional here, depending on whether you want your user to provide the password to authenticate the action or not. That is purely up to your decisions about who can do what and how easy.After that it's done, you may execute it with sudo <script> from your non-root user.NOTE: Check your user's sudo rules with sudo -lSolution 2: (PITA) Check all ownerships and permissionsIn order to allow a non-root user to run as itself the script, be sure that everything done by the script and the daemon that starts within is inside the allowed actions of the executing user. That means:The user must have proper permissions to any device used by the daemonThe user must have proper permissions to read/write/execute any folder inside a path that tries to accessThe user must have proper permissions to read/write any file that needs to be opened.So yes, it is possible, but I would definitely go for the sudo ruleEDIT: Of course, you will need to delete the line in the script where the UID is compared with 0 (root) done here[...]if [ `id -u` -ne 0 ]; then[...]