Run PHP shell_exec() like root user

a 夏天 提交于 2020-01-03 02:54:11

问题


I building one PHP application where I create command line functionality for Linux debian Jessie. All works fne but I need to be able use some commands like root user.

Is there a way to use shell_exec() or similar command to access like root user via PHP?

Idea of this command line is to people who have access to that server can handle with it over internet from any place or device.

Here is image of console:


回答1:


Executing commands as root via PHP will leave yourself wide open to all sorts of malicious hackery.

Have a look at the "sudo" documentation.

You should be able to set up all the commands you need as "sudo"able scripts. It is much better to write specific scripts with limited functions than to expose the underlying priviledged command.

As in:

exec ('sudo getCurrentUser.sh')

First, you need to add the user that PHP is using to run (most of the time it is www-data) to the sudo group if it is not already assigned.

Then, in your php file:

use sudo -S, so you can pass the password via echo

$exec = "echo your_passwd | /usr/bin/sudo -S your command";
exec($exec,$out,$rcode);

if you have trouble with the paths - use

"bash -lc 'echo your_passwd | /usr/bin/sudo -S your command'"

so you get a new bash that acts like a login shell and has the paths set



来源:https://stackoverflow.com/questions/43095751/run-php-shell-exec-like-root-user

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!