bash 从 php 脚本登录 /var/log 时权限被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45697270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:22:09  来源:igfitidea点击:

Permission denied when logging on /var/log from a php script

phpbashloggingcentos

提问by Fabrizio Bartolomucci

I found my crontab scripts do not work as expected because they cannot write on /var/log. I tried executing command:

我发现我的 crontab 脚本无法按预期工作,因为它们无法写入 /var/log。我尝试执行命令:

sudo /usr/bin/php /var/www/html/iPhone/inarrivo/php/rome/process.php >> /var/log/romeLoading.log 2>&1

须藤/usr/bin/php /var/www/html/iPhone/inarrivo/php/rome/process.php >> /var/log/romeLoading.log 2>&1

by hand and got:

手工并得到:

-bash: /var/log/romeLoading.log: Permission Denied

-bash: /var/log/romeLoading.log: 权限被拒绝

/var/log permissions are:

/var/log 权限是:

drwxr-xr-x. 13 root root 4096 15 ago 16.20 .

drwxr-xr-x。13 根 根 4096 15 前 16.20 .

If I conversely execute:

如果我反过来执行:

sudo touch /var/log/loadRome.log

须藤触摸/var/log/loadRome.log

I get no error whatsoever.

我没有任何错误。

What could be the issue?

可能是什么问题?

Please note Apache is not at stake: I am calling those scripts from the root crontab and from the shell with sudo as a test.

请注意 Apache 没有受到威胁:我正在使用 sudo 作为测试从根 crontab 和 shell 调用这些脚本。

采纳答案by DrRoach

It's a permissions issue as the log file belongs to rootuser and apache runs off www-data. Try chown www-data:www-data /var/log/loadRome.log.

这是一个权限问题,因为日志文件属于root用户并且 apache 运行了www-data。试试chown www-data:www-data /var/log/loadRome.log

回答by hanshenrik

best guess: the user running the shell doesn't have write access to /var/log/romeLoading.log, and the stdout redirect (>>) is redirected by the shell user, not the sudo user, thus the access denied on >> , but not on sudo touch. maybe try

最佳猜测:运行 shell 的用户没有对 的写访问权限/var/log/romeLoading.log,并且 stdout 重定向 ( >>) 是由 shell 用户重定向的,而不是 sudo 用户,因此访问在 >> 上被拒绝,但在 上被拒绝sudo touch。也许试试

sudo sh -c '/usr/bin/php /var/www/html/iPhone/inarrivo/php/rome/process.php >> /var/log/romeLoading.log 2>&1'

sudo sh -c '/usr/bin/php /var/www/html/iPhone/inarrivo/php/rome/process.php >> /var/log/romeLoading.log 2>&1'

that should run sh as root, and have the root-sh do the redirect with root permissions. untested though.

应该以 root 身份运行 sh,并让 root-sh 使用 root 权限进行重定向。虽然未经测试。

and next time you want to post permissions for debugging, post the namei -l path/to/fileoutput, it gives much more info than stating the single file itself when debugging permission issues, as the issue can be higher up than the file itself, like the folder its in, or the folder that the folder it's in, is in, etc~ and namei gives you, recursively, detailed permission information on all of them.

下次您想发布调试权限时,发布namei -l path/to/file输出,它提供的信息比在调试权限问题时说明单个文件本身要多得多,因为问题可能比文件本身更高,例如其所在的文件夹,或者它所在的文件夹,所在的文件夹等等~和namei递归地为您提供所有这些的详细权限信息。