“必须在目录上设置什么权限才能允许将数据附加到该目录中的文件,但不能删除该文件?”
根据我的理解,您需要“w”写入权限才能将数据附加到文件,但这也使您能够删除该文件,但问题是要求应设置的权限才能附加数据到文件但不删除它。
答案1
将数据附加到文件需要文件本身的写权限。删除文件需要对包含该文件的目录具有写权限。
例如,我有一个名为 testdir 的目录,我已删除了该目录的写入权限:
[haxiel@testvm1 ~]$ ls -ld testdir/
dr-xr-xr-x. 2 haxiel haxiel 26 Nov 23 10:09 testdir/
在该目录中,我创建了一个名为 testfile.txt 的文件(这是在删除该目录的写权限之前完成的)。
[haxiel@testvm1 testdir]$ ls -l testfile.txt
-rw-rw-r--. 1 haxiel haxiel 12 Nov 23 10:11 testfile.txt
现在,我可以将数据附加到文件中,因为我有写入权限:
[haxiel@testvm1 testdir]$ echo "Line1" >> testfile.txt
[haxiel@testvm1 testdir]$ echo "Line2" >> testfile.txt
[haxiel@testvm1 testdir]$ cat testfile.txt
Line1
Line2
但我无法删除该文件,因为我对其父目录没有写权限。
[haxiel@testvm1 testdir]$ rm testfile.txt
rm: cannot remove ‘testfile.txt’: Permission denied
您可以查看此问题以获取有关目录权限的更多详细信息:执行与读取位。 Linux 中的目录权限如何工作?
答案2
目录与文件权限无关。如果该文件也可以写入,则也可以删除。您可以尝试 ACL,如下所示:如何授予文件读写但不删除的权限,但这很容易实现。
以下是文件权限的说明:
(rwx------) This area is for owner.
(---rwx---) This area is for group owner.
(------rwx) This area is for others.
(-rwx------) The preceding - indicates a directory.
Value | Meaning
|
==========================================================================================================================================================================================================
|
777 (rwxrwxrwx) | No restrictions on permissions. Anybody may do anything. Generally not a desirable setting.
755 (rwxr-xr-x) | The file's owner may read, write, and execute the file. All others may read and execute the file. This setting is common for programs that are used by all users.
700 (rwx------) | The file's owner may read, write, and execute the file. Nobody else has any rights. This setting is useful for programs that only the owner may use and must be kept private from others.
666 (rw-rw-rw-) | All users may read and write the file.
644 (rw-r--r--) | The owner may read and write a file, while all others may only read the file. A common setting for data files that everybody may read, but only the owner may change.
600 (rw-------) | The owner may read and write a file. All others have no rights. A common setting for data files that the owner wants to keep private.