duanmeng1862
2017-08-12 12:46
浏览 196
已采纳

从php运行shell脚本作为指定用户

I am trying to run a shell script from a PHP script.

PHP code :

<? php
     $sss = escapeshellarg('virtualbox');
     $result = shell_exec("/home/hani/Desktop/launchscript.sh '$sss' 2>&1 ");
     echo "<pre>$result</pre>";
     echo "<br />";
     echo (shell_exec('whoami'));
?>

my shell script :

#!/bin/bash


sss=$1
echo 'the sudo password' |sudo -S service $1 restart

After I run the PHP code in a web server (Xampp), I got this output :

[sudo] password for daemon: Sorry, try again.
[sudo] password for daemon: 
sudo: 1 incorrect password attempt

daemon

Although, I haven't set any password for the daemon user. And after I checked the current user running the PHP code I found it is daemon.
After many researches here and in the net, I found that daemon can't run sudo commands.
I also found that I can fix this by editing the sudoers file and giving permissions to the daemon user to run sudo commands. However this is not a secured solution.
so my question is : How to run that script via the PHP code but not as a daemon?
PS : I tried this in order to change the current user running the PHP file :

$result = shell_exec(" sudo -u hani /home/hani/Desktop/launchscript.sh '$sss' 2>&1 ");  

But I got this output in the browser :

sudo: no tty present and no askpass program specified

and the user remains daemon.

I am using Xampp in Ubuntu 16.04
Another information, I run this command in the terminal to know the owner of the 'httpd' service :

ps -ef | egrep '(httpd)' | grep -v `whoami` | grep -v root | head -n1 | awk '{print $1}'

the output is : daemon

图片转代码服务由CSDN问答提供 功能建议

我正在尝试从PHP脚本运行shell脚本。

PHP代码:

 &lt;?  php 
 $ sss = escapeshellarg('virtualbox'); 
 $ result = shell_exec(“/ home / hani / Desktop / launchscript.sh'$ sss'2&gt;&amp; 1”); 
 echo“&lt; pre&gt;  ; $ result&lt; / pre&gt;“; 
 echo”&lt; br /&gt;“; 
 echo(shell_exec('whoami')); 
?&gt; 
   
  
 

我的shell脚本:

 #!/ bin / bash 
 
 
sss = $ 1 
echo'sudo password'| sudo -S service $ 1  restart 
   
 
 

在Web服务器(Xampp)中运行PHP代码后,我得到了这个输出:

 守护进程的[sudo]密码:对不起,请再试一次。
守护进程的密码:
sudo:1密码尝试不正确
 
 
守护进程
   
 
 

虽然,我没有为守护程序用户设置任何密码。 在我检查运行PHP代码的当前用户后,我发现它是守护程序
\ 经过这里和网上的许多研究,我发现守护程序无法运行 sudo 命令。
我还发现我可以通过编辑sudoers来解决这个问题。 档案 nd授予守护程序用户运行sudo命令的权限。 然而,这不是一个安全的解决方案。
我的问题是:如何通过PHP代码运行该脚本但不作为守护程序?
PS :我试过这个以便更改运行PHP文件的当前用户:

  $ result = shell_exec(“sudo -u hani / home / hani / Desktop / launchscript  .sh'$ sss'2&gt;&amp; 1“);  
   
 
 

但是我在浏览器中输出了这个输出:

  sudo:no tty present并且没有指定askpass程序 
   
 
 

用户仍然是守护进程。

我在Ubuntu 16.04中使用Xampp
另一个信息,我在终端运行此命令以了解'httpd'服务的所有者: \ n

  ps -ef |  egrep'(httpd)'|  grep -v`whoami` |  grep -v root | 头-n1 |  awk'{print $ 1}'
   
 
 

输出为:守护程序

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douqin0676 2017-08-12 15:12
    已采纳

    I think i found a solution ( but still not sure about the security issues).
    It only needs to change the default user (owner) and group of the httpd service. This can be done by editing the httpd.conf located in /opt/lampp/etc (if you are using Xampp). The default user, as I mentioned in the question, is daemon. However it has not permissions to run sudo commands, so it only needs to change that user by another one who has the permissions to run sudo commands (obviously the root user or your deafult user in Ubuntu).

    点赞 打赏 评论

相关推荐 更多相似问题