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

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.

| improve this answer | |
  • 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. – user2930077 Jan 30 '15 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 '15 at 13:04
  • Thanks, you have helped me a lot. The command 'sudo' has to be called before a script. – user2930077 Feb 3 '15 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.