简体   繁体   English

PHP:以root用户身份运行Shell脚本

[英]PHP: Run Shell Script as root user

I'm trying to run a script as root user. 我正在尝试以root用户身份运行脚本。 here is my code 这是我的代码

echo shell_exec("sudo bash.sh 2>&1");

It's giving error 出错了

sudo: no tty present and no askpass program specified
www-data is not in the sudoers file.  This incident will be reported.

I've followed these methods as well but end up with no permission error 我也遵循了这些方法,但是最终no permission错误

There are a couple of issues you might encounter: 您可能会遇到几个问题:

  • The user that is running the php process must have sudo rights (check with visudoers command) 运行php进程的用户必须具有sudo权限(请使用visudoers命令检查)
  • There is no environment set, so the $PATH variable does not include the path to the sudo command 没有设置环境,因此$PATH变量不包含sudo命令的路径
  • sudo might require a password. sudo可能需要密码。 Either change the sudoers file, adding NOPASSWORD , which would be hugely unsafe. 更改sudoers文件,添加NOPASSWORD ,这将是非常不安全的。 Or you have to use pipes ( proc_open ), and pass the password through the stdin pipe 或者,您必须使用管道( proc_open ),并通过stdin管道传递密码

I've managed to find a way to do so, but after some help from people on this site: 我设法找到了一种方法,但是在此站点上的人提供了一些帮助之后:

load .profile with proc_open() 用proc_open()加载.profile
proc_open interaction proc_open互动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM