How to run from PHP a bash script under root user

前端 未结 5 1476
天涯浪人
天涯浪人 2020-12-09 18:02

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

相关标签:
5条回答
  • 2020-12-09 18:26

    I would add a specific rule to allow this script to be called by nobody user, using sudo.

    0 讨论(0)
  • 2020-12-09 18:28

    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

    0 讨论(0)
  • 2020-12-09 18:36

    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.

    0 讨论(0)
  • 2020-12-09 18:41

    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');
    
    0 讨论(0)
  • 2020-12-09 18:43

    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
    
    0 讨论(0)
提交回复
热议问题