简体   繁体   English

PHP:shell_exec是一个shell脚本,而shell_exec是一个Linux上的程序(权限)

[英]PHP: shell_exec a shell script vs. shell_exec a program on linux (permissions)

I have a shell script, which when executed, makes some folders and copies files into them. 我有一个Shell脚本,该脚本在执行时会创建一些文件夹并将文件复制到其中。 I also have a node.js "program", which does exactly what the shell script does. 我也有一个node.js“程序”,它确实完成了shell脚本的工作。

When I call the shell script using PHP's shell_exec , I get a permission error, which basically says that the shell script does not have privileges to make folders. 当我使用PHP的shell_exec调用Shell脚本时,出现权限错误,该错误基本上是说Shell脚本没有创建文件夹的权限。 I did some googling and turns out I'd have to setup passwordless sudo and who knows what else trickery to fix this. 我做了一些谷歌搜索,结果发现我必须设置无密码的sudo,谁知道还可以解决这个问题。

However, I made an exact same thing with node and when I run the node "program" by calling it with shell_exec just like before and it seems that this node "program" has the rights to do whatever. 但是,我对节点做了完全相同的事情,当我像以前一样通过shell_exec调用它来运行节点“程序”时,似乎该节点“程序”有权执行任何操作。

What is the reason behind this? 这背后的原因是什么? Why do "programs" have privileges when ran, but shell scripts do not? 为什么“程序”在运行时具有特权,但Shell脚本却没有? Is this a legitimate way to do, for example, file system operations (calling something made with node.js from PHP)? 这是合法的方法吗,例如文件系统操作(从PHP调用使用node.js进行的操作)? Because it is annoying to fiddle with the permission if I try to do the same operations with PHP. 因为如果我尝试使用PHP进行相同的操作,那么摆弄权限是很烦人的。

Php was run by apache or something similar and is run under user www-data or http or something like that. Php由apache或类似的程序运行,并在用户www-data或http或类似的程序下运行。 Your node server probably runs under piwwoli user. 您的节点服务器可能在piwwoli用户下运行。 piwwoli can create a directory under /home/piwwoli, but www-data can't. piwwoli可以在/ home / piwwoli下创建目录,但是www-data不能。

Both programs and shellscripts have privileges when they run, the question is who is running them. 程序和shellscript都在运行时具有特权,问题是谁在运行它们。

You can create a directory that is writeable both by www-data and piwwoli: 您可以创建一个可同时由www-data和piwwoli写入的目录:

mkdir /path/to/dir
chmod o+w,g+w /path/to/dir
chown piwwoli.www-data /path/to/dir # maybe you'll need sudo for this to work

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

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