PHP-CLI Sudo 执行
我正在运行一个 cli 脚本,需要 exec('sudo ...');称呼。我知道它在网络上不安全,但是在 cli 中如何做到呢?该脚本由名为“btcdbit”的用户执行,该用户位于 sudoers 文件中。
I am running a cli-script, that requires a exec('sudo ...'); call. I know it is not safe on the web, but how can it be done in cli? The script is executed by a user known as "btcdbit", who is in the sudoers file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据我的经验,设置 NOPASSWD 选项并不总是有效,即使有效也似乎不安全。在我看来,更好的方法(如果您能够使用它)将涉及使用 phpseclib 通过 SSH 执行 sudo。例如。
网站“sudo in php”详细说明
In my experience setting the NOPASSWD option doesn't always work and even if it does it seems unsafe. Seems to me that a better approach - if you're able to use it - would involve using phpseclib to do sudo through SSH. eg.
The website "sudo in php" elaborates
只要 btcdbit 位于您希望其能够运行的程序的 sudoers 中,您就应该能够使用任何 PHP 函数,例如
exec
或system
来运行它。确保在 sudoers 中使用 NOPASSWD 选项(请参阅 http://www.ducea.com/2006/06/18/linux-tips-password-usage-in-sudo-passwd-nopasswd/)如果您不这样做希望它能够向 btcdbit 询问密码。So long as btcdbit is in sudoers for the program that you want it to be able to run, you should be able to use any of the PHP functions like
exec
orsystem
to run it. Make sure that you use the NOPASSWD option in sudoers (see http://www.ducea.com/2006/06/18/linux-tips-password-usage-in-sudo-passwd-nopasswd/ for example) if you don't want it to get caught up asking btcdbit for a password.它应该像 exec('/usr/bin/sudo {script}') 一样简单。
It should be just as simple as exec('/usr/bin/sudo {script}').