fifty arashi
fifty arashi

Reputation: 541

Can use PHP to reboot linux server or restart dhcp?

i have try some command like system(reboot) exec(reboot) and system("/ect/init.d/networking restart"); but not happen something

how can i do?

Upvotes: 3

Views: 5673

Answers (6)

Ehecatl
Ehecatl

Reputation: 122

on RHEL i had to comment the line which requires tty on sudoers config file:

#Defaults requiretty

Upvotes: 0

Probably your PHP interpreter isn't running as a user with suitable permissions to do this. Normally these need to be done as root, which is a bad idea for PHP in general since one small security hole could see the whole machine being compromised.

If you really want to do this I'd suggest looking at using sudo to grant limited extra access for the user the webserver runs as.

The other alternative to sudo on some systems is dbus. With correct dbus privileges you can send an instruction to restart e.g.

dbus-send --system --print-reply --dest="org.freedesktop.Hal" /org/freedesktop/Hal/devices/computer org.freedesktop.Hal.Device.SystemPowerManagement.Shutdown

Works from the command line and I believe there are dbus bindings available for php.

Upvotes: 1

Ole Melhus
Ole Melhus

Reputation: 1939

You'll have to add your apache user to sudoers with NOPASSWD and only give access to the i.e. reboot and then run system("sudo reboot");

For the sudoersfile:

www-data reboot = NOPASSWD: /sbin/reboot

This will give apache access to reboot your server, but rememer that all users on the system then will be able to reboot.

Upvotes: 4

Alex Rashkov
Alex Rashkov

Reputation: 10015

Try this:

<?php
shell_exec("/usr/sbin/reboot");
exec("/usr/sbin/reboot");
system("/usr/sbin/reboot");
?>

For more details look here:

http://www.linuxquestions.org/questions/linux-newbie-8/shutdown-and-reboot-linux-system-via-php-script-713379/

Upvotes: 0

dvhh
dvhh

Reputation: 4750

as everyboy stated, it is quite dangerous. Better use some ssh session to reboot/restart your server/services.

In the case you still want to do it give your apache running user (www) a sudo right.

Upvotes: 2

bogeymin
bogeymin

Reputation: 685

If you're trying to do this through apache and the CGI version of PHP -- then the answers is "no". PHP will execute as the same user as apache and giving this user access to your system would be extremely dangerous.

Upvotes: 4

Related Questions