dongxuanyi3406 2018-10-12 18:12
浏览 111
已采纳

使用apache通过php执行shell脚本时,当前用户和env用户不匹配

I'm running Apache on a Linux server. There are a collection of shell scripts that need to get executed by a PHP script. Apache/PHP on the server is only needed for this one purpose. The shell scripts must be executed by user "X". I've set the User and Group properties in httpd.conf to be for user "X" and its group.

When I call the PHP script, I've included an echo of whoami. It shows user "X". I've also included an output to log file in the shell script that PHP executes that echos whoami and env variables. When the shell script runs, it reports that the user is "X", but the env variables output shows the user to be ROOT. How is this possible? This is ultimately a problem as the shell script won't run properly if it doesn't have access to the env variables for user "X".

[edit] Further clarification. The issue exists at the PHP script level too, so to simplify things, I can rephrase the issue that when I have Apache configured in httpd.conf to run as user "X", and if I trigger a PHP script that echos "whoami" and "env", then I see that "whoami" is user "X" but the "env" details are for user "root". the PHP script would look something like this...

<?php
echo "<br>". exec('whoami');
exec('env', $envo);
echo "<br>". print_r($envo);
?>
  • 写回答

2条回答 默认 最新

  • duangan6731 2018-10-13 01:20
    关注

    whoami prints the effective user id.

    When you start Apache, I guess you do it using the root user, or with some sudo rule. This is required to let Apache bind ports below 1024 (so the default 80 HTTP or 443 HTTPS qualify).

    In your Apache config, you put User X (and probably Group X). This tells Apache that once the first process is started (via root), all other children processes will run under the X:X user and group.

    So in your PHP code, when you run whoami it returns the effective ID of the process PHP runs in. This is X, since the process is a child of the first process.

    But when you run env it runs the environment under which the child process was started. This environment was inherited from the first process, which is started by root. Hence you see the env of root.

    Your fix is ok.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧