Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restarting apache without being superuser

Is there any possibility to perform graceful restart and check apache config syntax without being root or having root privileges?

I have already tried to set suid bit to the script which performs restart. Here is the script itself:

#!/bin/bash

FILENAME=$1
DATE=`date +%Y%m%d`
DIRECTORY='bckp/'
if [ ! -d "$DIRECTORY" ]; then
    echo "Backup directory doesn't exist. Creating one."
    mkdir $DIRECTORY
fi

if [ -z "$FILENAME" -a ! -f "$FILENAME" ]; then
    FILENAME="webdav.conf"
    echo "No file specified. Backing up webdav.conf"
fi

REV=0
BACKUP="$FILENAME.$DATE.$REV"

while [ -f $BACKUP ]; do
    let REV+=1
    BACKUP="$DIRECTORY$FILENAME.$DATE.$REV"
done

cp $FILENAME $BACKUP

echo $OUTPUT
OUTPUT=$(apache2ctl configtest 2>&1)
if [ "$OUTPUT" == "Syntax OK" ]; then
    echo "Syntax OK"
    echo "Performing restart"
    apachectl -k graceful 2>&1
fi

exit $?

here is the ls -l for this file:

-rwsrwxr-x 1 root user  645 2012-04-26 18:05 graceful-restart

When i try to run this script i get the following output:

No file specified. Backing up webdav.conf
ulimit: 88: error setting limit (Operation not permitted) Syntax OK

I'm interested if it is possible to perform what i've described.

like image 605
roman Avatar asked Jan 18 '26 21:01

roman


1 Answers

Linux will not honor the suid bit on a shell script. Read this for more information.

A common solution for this is the sudo command. With an entry like this in /etc/sudoers:

yourname ALL = NOPASSWD:/path/to/graceful-restart

You could run:

sudo /path/to/graceful-restart

And this would run with root privileges without prompting you for a password. See the sudoers man page for more information on the syntax of the sudoers file.

like image 186
larsks Avatar answered Jan 21 '26 12:01

larsks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!