Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to excute some command line script in centos using, for example "shutdown -h now",...
I have username and password for root privilege but when I use browsers to run php file, it not work.
Here is my PHP code:
PHP
<?php
   shell_exec('/sbin/shutdown -r now');
?>


I also find some guide, about like this:
php - Use shell_exec() to restart server? - Stack Overflow[^]
but I think why use this while I have root username and pass? And I not found /etc/sudoer directory.
Please help me. Thanks

What I have tried:

Execute Centos command line srcipt using PHP, such as, reboot machine,...
Posted
Updated 28-Feb-17 0:15am

1 solution

Quote:
but I think why use this while I have root username and pass?
You have the password but it has to be entered when required. How do want to do that when the web server executes a script?

Quote:
And I not found /etc/sudoer directory
It is not a directory but a file. And you should use visudo to edit that file instead of using an editor.


The web server will not run the script as root. In most cases the PHP script is run as the user that runs the server (e.g. wwwrun or apache).

When using shell_exec to execute commands that are not allowed for the user running the script, you have to execute that command as another user (root in your case).

This can be done using sudo (43.1.4.3.2. The sudo Command[^]).

But sudo will prompt for a password which will not work when called from a script started by the web server. The solution is to allow specific users executing spefic commands using the /etc/sudoers file (use visudo to edit that file).

An example entry might look like
wwwrun ALL=(root) NOPASSWD: /sbin/shutdown


But a better solution would be creating a dedicated script. This will ensure that only this script can be executed as root because it is a bad idea to give the web server account root privileges for various commands.

Move to a folder where you want to store the script (e.g. /usr/local/bin) and create the script (named reboot.sh here) using your favorite editor as root:
Bash
#!/bin/bash
/sbin/shutdown -r now

Make the script executable:
sudo chmod u+x reboot.sh

Now edit the sudoers file and allow the script to be executed:
sudo visudo

Add this line at the bottom (assuming the PHP script is executed as user wwwrun)
wwwrun ALL=(root) NOPASSWD: /usr/local/bin/reboot.sh

Then call this script using sudo
PHP
shell_exec('sudo /usr/local/bin/reboot.sh');
 
Share this answer
 
Comments
CPallini 28-Feb-17 6:28am    
5.
Member 10390715 1-Mar-17 4:50am    
But How can I find PHP script user?
Jochen Arndt 1-Mar-17 5:09am    
When executing a script the user name is provided in the $USER variable.

So you can put a statement into the script to write that to a file:

echo "$USER" > /tmp/www_script_user.txt

or executed that:

shell_exec('echo "$USER" > /tmp/www_script_user.txt')

Then read the file content (here from the shell):

cat /tmp/www_script_user.txt

Or check the documentation of your web server on which user is used when executing scripts. In most cases this should be the same user that runs the server. To get that user execute (here for the Apache server)

ps -aux | grep apache

The first column will show the user name (usually wwwrun or www-data).
Member 10390715 2-Mar-17 2:53am    
Hi, I use this:
echo "$USER" > /tmp/www_script_user.txt and result is root
Jochen Arndt 2-Mar-17 3:03am    
When your script is executed as root, there is no need for all this and you should be able to execute the shutdown command.

But a web server should never be executed as root.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900