Linux chown:不允许操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13225453/
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-08-06 17:41:44  来源:igfitidea点击:

chown: Operation not permitted

phplinuxshell

提问by mahen3d

I have problem where i need to set the file owner permission to different user in the system via a php script

我有问题,我需要通过 php 脚本为系统中的不同用户设置文件所有者权限

so i do this via this following command where 1002 is the user id of the system.

所以我通过以下命令执行此操作,其中 1002 是系统的用户 ID。

file_put_contents($filename, $content);
system("chown 1002 " . $filename . "");

however i get this error in productions server only (test server it works fine)

但是我只在生产服务器中得到这个错误(测试服务器工作正常)

chown: changing ownership of `/var/spool/asterisk/06h12m7.call':
Operation not permitted

采纳答案by Bruno Vieira

Since you tagged this question as LinuxI'm assuming that you use Apacheserver. In production servers the Apacheprocess, which owns all php processes, are usually executed by the apache useror other user that is not the root user.

由于您在Linux我假设您使用server.xml 时标记了这个问题Apache。在生产服务器的Apache过程中,该公司拥有的所有PHP进程,通常由执行apache user不属于或其他用户root user

Bearing that in mind, what you are trying to do is using the chownfunction, (which will be executed as apache user) to change the owner of a file that you don't own. (Yes, you can only change owners to the files you own).

记住这一点,您要做的是使用该chown函数(将以 apache 用户身份执行)来更改您不拥有的文件的所有者。(是的,您只能更改您拥有的文件的所有者)。

You see, quoting the php manual, the chownfunction attemptsto change the owner:

你看,引用 php 手册,chown函数试图改变所有者:

Attempts to change the owner of the file filename to user user. Only the superuser may change the owner of a file.

尝试将文件文件名的所有者更改为用户 user。只有超级用户可以更改文件的所有者。

In production servers usually you are running as in user directory mode, which means you are bound to the files that are in your home directory, something like /home/yourusername/public_htmland as such, files inside the /vardirectory are simply out of your reach (They are usually owned by root) and that's why you can't chown.

在生产服务器中,您通常以用户目录模式运行,这意味着您被绑定到主目录中的文件,/home/yourusername/public_html例如,/var目录中的文件根本无法访问(它们通常由root),这就是你不能chown的原因。

I hope it helped. Cheers!

我希望它有所帮助。干杯!

回答by mattc

You are only allowed to change the owner of a file if you are the root user (or running with root privileges).

如果您是 root 用户(或以 root 权限运行),则只能更改文件的所有者。

Attempts to change the owner of the file filename to user user. Only the superuser may change the owner of a file. http://www.php.net/manual/en/function.chown.php

尝试将文件文件名的所有者更改为用户 user。只有超级用户可以更改文件的所有者。 http://www.php.net/manual/en/function.chown.php

Most likely on your test server the apache process running the php script already owns the file, or you are running it with root privileges, and this isn't the case on your production server. I would definitely not recommend running in a production environment with root privileges.

很可能在您的测试服务器上,运行 php 脚本的 apache 进程已经拥有该文件,或者您正在以 root 权限运行它,而在您的生产服务器上情况并非如此。我绝对不建议在具有 root 权限的生产环境中运行。

Some work arounds that may help depending on the situation, you can change the "group" setting of the file using the chgrpfunction. This is permitted by anyone with write privileges on the file. You can also make the file writeable to anyone using the chmodfunction, although this can be dangerous. Links to both functions are listed below:

一些可能会根据情况有所帮助的变通方法,您可以使用该chgrp功能更改文件的“组”设置。对文件具有写权限的任何人都允许这样做。您还可以使文件对使用该chmod函数的任何人都可写,尽管这可能很危险。下面列出了这两个函数的链接: