开发者

Can use PHP to reboot linux server or restart dhcp?

开发者 https://www.devze.com 2023-01-22 18:40 出处:网络
i have try some command likesystem(reboot) exec(reboot) and system(\"/ect/init.d/networking restart\");

i have try some command like system(reboot) exec(reboot) and system("/ect/init.d/networking restart"); but not happen somet开发者_JAVA百科hing

how can i do?


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.


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.


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.


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.


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/


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

#Defaults requiretty
0

精彩评论

暂无评论...
验证码 换一张
取 消