How to run from PHP a bash script under root user (with all permissions) and not nobody user - php default user?
thats my output af
I would add a specific rule to allow this script to be called by nobody
user, using sudo
.
Under Linux you normally do this using sudo
. Try to be as specific as possible, so not to give the script too many permissions.
For examples on how to use sudo
: http://aplawrence.com/Basics/sudo.html
You can make a program which is set-uid root. This causes the program to always run as root. This doesn't work with shell scripts, so you have to use a program which calls your script.
I recently published a project that allows PHP to obtain and interact with a real Bash shell (as user: apache/www-data or root if needed). Get it here: https://github.com/merlinthemagic/MTS
After downloading you would simply use the following code:
$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1 = $shell->exeCmd('/full/path/to/script.sh');
You can use sudo:
exec("sudo /your/script");
You should allow executing your script without password prompt. Run sudo visudo
in console and add the following string to the end:
nobody ALL = NOPASSWD: /your/script
You must set up file mode properly to ensure that no one can modify this script and put dangerous contents into it (in root console):
chown root:root /your/script
chmod 755 /your/script