how to do a linux reboot from php file -
i have user brftv on linux system , have www-data runs nginx.
from terminal can let brftv user run
sudo /sbin/reboot
and works fine since added following /etc/sudoers file's "#user privilege specification" section:
brftv all=nopasswd: /sbin/halt, /sbin/reboot, /sbin/poweroff www-data all=nopasswd: /sbin/halt, /sbin/reboot, /sbin/poweroff
but when php file runs following code, nothing happens
exec('nohup sudo -u brftv /sbin/reboot');
i added www-data line etc/sudoers above in case necessary when running above exec() (even though run -u brftv, i'm no linux expert, thought better safe in case).
the php file runs exec() owned www-data, , chmod 777, should have privilege execute it.
i have tried running php-file both through browser (would run user www-data assume) , terminal $ php myfile.php
.
------------------- update -----------------
i did this
sudo chmod u s /sbin/reboot
which allows users on system run reboot cmd without password. works, rather not leave open, other solution /etc/sudoers better, if have hint @ problem is...
i followed tut http://linux.byexamples.com/archives/315/how-to-shutdown-and-reboot-without-sudo-password/ , second example pretty got above didn't work me..
i use small c program grant access php group (probably www-data
in case?), use suid bit on executable, , exec reboot command
phpreboot.c :
#include <stdlib.h> #include <sys/types.h> #include <unistd.h> int main() { setuid(0); // uid 0, root char *command = "/sbin/reboot"; execl(command, command, null); return 0; // avoid warning (since never returns) }
compile it
gcc -wall phpreboot.c -o phpreboot
move phpreboot want run (has accessible php!)
mv phpreboot /home/private/
as root (or via sudo) ensure owner root , group set www-data, , change rights have suid bit (in order)
chown root:www-data phpreboot chmod 4750 phpreboot
the result, ls -l phpreboot
should (note s in rws)
-rwsr-x--- 1 root www-data 8565 jun 12 11:42 phpreboot*
change php script execute phpreboot instead
exec ("/home/private/phpreboot"); // change path!
only 1 tiny executable have suid run reboot program, , php group able execute (and root of course).
- about setuid , suid bit
- php get running group id on linux doing
id groupid
gives group name.
Comments
Post a Comment