I have a user brftv on my linux system and I have www-data that runs the nginx.
from the terminal I can let my brftv user run
sudo /sbin/reboot
and it works fine since I added the following to my /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 my php file runs the following code, nothing happens
exec('nohup sudo -u brftv /sbin/reboot');
I added the www-data line to the etc/sudoers above in case it was necessary when running the above exec() (even though I run it as -u brftv, but I'm no linux expert, just thought better be safe just in case).
The php file that runs this exec() is owned by www-data, and chmod is 777, all should thus have privilege to execute from it.
I have tried running the php-file both through browser (would be run by user www-data I assume) and from terminal $ php myFile.php
.
------------------- UPDATE -----------------
I did this
sudo chmod u s /sbin/reboot
Which allows all users on my system to run the reboot cmd without password. It works, but I rather not leave it THAT open, so the other solution with /etc/sudoers would be better, if someone would have a hint at what my problem is...
I followed this tut http://linux.byexamples.com/archives/315/how-to-shutdown-and-reboot-without-sudo-password/ and the second example is pretty much what I got above that didn't work for me..
Allow Reboots From PHP:
vim /etc/sudoers
%www-data ALL=NOPASSWD: /sbin/reboot
" and write changes.chown www-data:www-data filename.php
(apache user)Enjoy :)
(tested on ubuntu server 14.40 LTS with php5-7, should also work on any debian based distro.)
The only way I got this to work on my system was to "hack" it by changing chmod on /sbin/reboot like this guy did
http://linux.byexamples.com/archives/315/how-to-shutdown-and-reboot-without-sudo-password/
sudo chmod u+s /sbin/reboot
I realize this might not be optimal in many cases, but this mediaPlayer is very much locked down so there is no accessing a terminal for anyone else anyways.
Giving reboot permission to
www user
is a bad idea.Create a cron and do system reboot from the cron rather than from PHP script. The Cron will run every minute and check for reboot flag. If it is set the it will do the reboot.1)write a flag to a file from your php program so that the cron can decide whether to do reboot or not.
2) Create a bash script to read that file and do reboot if the PHP script tells it to do so.
execute this in shell
chmod +x mycron.sh
3) Configure the script in crontab
crontab -e
and paste this4) The user who set the cron should have sudo permission. Add him to sudoers.
Why did you put
-u brftv
in there? That makes your PHP script try to run thereboot
as your user, which won't work. Only root can reboot the system. Remove it.Try this code
then from php
I would use a very small C program to grant access to only the PHP group (probably
www-data
in your case?), use the suid bit on the executable, and exec the reboot commandphpreboot.c :
Compile it
Move phpreboot where you want to run it (has to be accessible by PHP!)
As root (or via sudo) ensure owner is root and group is set to www-data, and change rights to have suid bit (in this order)
The result,
ls -l phpreboot
should be something like (note the s in rws)Change the PHP script to execute phpreboot instead
Only one tiny executable will have the suid to run the reboot program, and only the PHP group will be able to execute it (and root of course).
id groupID
gives the group name.