0

I have a web server under apache2 and php5. I want to shutdown a computer using web server but to shutdown the computer a current user should be 'root' user because command for the turning off of PC 'shuwdown -h now' should be executed under root user.
The php user is called 'www-data'.

I have such files

file: shutdown.sh

#!/bin/bash
echo $(shutdown -h now);

file: shutdown.php

If this script will be called the script 'shutdown.sh' should be executed.
<?php
echo "Shutdown";
echo exec ( 'shutdown.sh' );
?>

The question:how to execute file 'shutdown.sh' as root user without password. Or maybe are another ways exist?

1 Answer 1

1

You can give www-data permission to execute the shutdown command. Try this:

As root, use the command 'visudo' to edit your /etc/sudoers file. The last lines should look like this:

# Cmnd alias specification

# User privilege specification

root ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges

%admin ALL=(ALL) ALL

So, in the end of the file, insert:

www-data ALL = NOPASSWD: /sbin/shutdown

Then exit the editor and save it (CTRL+x).

The user www-data should be able to call shutdown now, without sudo password.

3
  • Thanks for answer but it didn't help me. I added a line to file /etc/sudoerc artem ALL = NOPASSWD: /sbin/shutdown, artem is my account, then I tried to execute command in terminal '/sbin/shutdown -h now' and it requires password from me. Jan 30, 2015 at 23:07
  • Oh, you need to call it like 'sudo /sbin/shutdown ...', sudo is still needed, but it will see that you are authorized to do that without a password. Also, remember to restart sudo service 'service sudo restart' before that.
    – diegov
    Jan 31, 2015 at 13:04
  • Thanks, you have helped me a lot. The command 'sudo' has to be called before a script. Feb 3, 2015 at 18:45

You must log in to answer this question.

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