简体   繁体   English

如何从PHP以root特权运行shell脚本?

[英]How to run a shell script with root privileges from PHP?

I'm trying to use vpopmail's vadddomain to add new virtual domains. 我正在尝试使用vpopmail的vadddomain添加新的虚拟域。 Since apache user has no access to vpopmail's binaries I cannot exec('../bin/vadddomain...') directly from PHP. 由于apache用户无法访问vpopmail的二进制文件,因此我无法直接从PHP exec('../bin/vadddomain...')

I made a shell script ( adddomain.sh ) as below: 我制作了一个shell脚本( adddomain.sh ),如下所示:

#!/bin/bash
 cd /home/lxadmin/mail/bin
 ./vadddomain $1 postmaster_password

And appended this line to /etc/sudoers using visudo (forget about security for now): 并使用visudo将此行附加到/etc/sudoersvisudo忘记安全性):

ALL ALL=NOPASSWD: /home/lxadmin/mail/bin/adddomain.sh

I have something like this in my PHP script: 我的PHP脚本中有以下内容:

exec('sudo /home/lxadmin/mail/bin/adddomain.sh example.com', $output);

But this is not working at all (domain example.com will not be added to vpopmail). 但这根本不起作用(域example.com将不会添加到vpopmail)。 But when I run this command through SSH and with root user, it works. 但是,当我通过SSH和root用户运行此命令时,它可以工作。

Can anyone tell what is my problem here? 谁能告诉我我的问题是什么?

This should be possible by adding the webmaster -user tot the /etc/sudoers file and simply accept that user to use the command you are willing to use. 这应该可以通过将webmaster -user添加到/etc/sudoers文件并仅接受该用户使用您愿意使用的命令来实现。 I do not recommend to use the sudo command directly in your php since you probably have to store the password for this somewhere, which off course you do not want! 我不建议直接在php中使用sudo命令,因为您可能必须将密码存储在某个地方,这当然是您不希望的! You don't want to post your password by accident somewhere or another developer which you don't want to have root privileges to your code. 您不想在不希望对代码具有root特权的地方或其他开发人员中意外地发布密码。

Another big downside of simply using root commands is that WHEN you have a security breach in your code, that I can run sudo commands through for example a form. 仅使用根命令的另一个大缺点是,当您的代码中存在安全漏洞时,我可以通过例如表单来运行sudo命令。 Be very careful with the privileges. 请特别注意特权。

Ps. PS。 I'm new to SSH and setting up users privileges as well, so please correct me if I'm wrong. 我是SSH的新手,并且还设置了用户特权,因此如果我输入错误,请更正我。 But this is what I know. 但这就是我所知道的。

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

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