Can I use PHP to reboot a Linux server or reboot dhcp?

I tried some command, for example system(reboot) exec(reboot) , system("/ect/init.d/networking restart"); but nothing happens

How can i do this?

+3
source share
6 answers

You will need to add the apache user to sudoers using NOPASSWD and provide only access to ie restart, and then start the system ("sudo reboot");

For sudoers file:

www-data reboot = NOPASSWD: /sbin/reboot

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

+3
source

apache CGI PHP, "". PHP , apache, .

+4

, . ssh / /.

, , apache (www) sudo.

+2

, PHP . root, PHP , , .

, sudo , -.

Another sudo alternative for some systems is dbus. With the correct dbus privileges, you can send instructions to restart, for example.

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

It works from the command line, and I believe dbus bindings are available for php.

+1
source

Try the following:

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

See here for more details:

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

0
source

on RHEL, I had to comment out a line that requires tty in the sudoers configuration file:

#Defaults requiretty
0
source

Source: https://habr.com/ru/post/1772102/


All Articles