4

I have a problem. I want to restart ubuntu with php code but I can not. I have tried all of codes from the internet like

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

and

<?php
shell_exec("shutdown -r");
exec("shutdown -r");
system("shutdown -r");
?>

But all of them do nothing. Please help me. I need this code.

2 Answers 2

2

If the PHP code is being executed by the webserver, it is running under the user "www-data" in ubuntu.

Probably www-data has no right to shutdown, which is a sane default.

You can give www-data sudo rights to shutdown without providing a password and call reboot using sudo.

Take a good look at man sudoers and be sure to understand the security implications before editing the sudo config: this will effectively give any PHP script the right to shutdown the server (untested).

# /etc/sudoers (edit using the visudo command)

Cmnd_Alias     SHUTDOWN = /usr/sbin/shutdown
Cmnd_Alias     HALT = /usr/sbin/halt
Cmnd_Alias     REBOOT = /usr/sbin/reboot

Host_Alias     LOCALHOST = 127.0.0.1

www-data       LOCALHOST = NOPASSWD: SHUTDOWN, HALT, REBOOT

In PHP you can use:

shell_exec("/usr/bin/sudo /usr/sbin/reboot");
Sign up to request clarification or add additional context in comments.

Comments

0

I don't recommend to giving root access to the PHP user

Check here: https://stackoverflow.com/a/5226760/2708670

2 Comments

i saw your link . but i do not understand exactly .
I know about this is dangerous but I need this code for exercise

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.