linux中文件的写但不读权限

Han*_*nna 6 linux filesystems permissions

是否可以在 Linux 中对文件具有写但不具有读权限?执行但没有读写权限怎么办?

Ben*_*ebe 8

[benji@laptop ~]$ ./hello
Hello world!
[benji@laptop ~]$ chmod 000 hello #no permissions, start out with a clean slate
[benji@laptop ~]$ chmod +x hello #make it executable
[benji@laptop ~]$ cat hello #try to read it, but can't
cat: hello: Permission denied
[benji@laptop ~]$ ./hello #try to run it, it works!
Hello world!
[benji@laptop ~]$

[benji@laptop ~]$ cat hello.sh
#!/usr/bin/bash
echo 'Hello world!'
[benji@laptop ~]$ chmod 000 hello.sh #no permissions, start out with a clean slate
[benji@laptop ~]$ chmod +x hello.sh #make it executable
[benji@laptop ~]$ cat hello.sh #try to read it; FAIL
cat: hello.sh: Permission denied
[benji@laptop ~]$ ./hello.sh #try to run it, but the shell interpreter (bash) cannot read it; FAIL
/usr/bin/bash: ./hello.sh: Permission denied
[benji@laptop ~]$ 
Run Code Online (Sandbox Code Playgroud)

仅当文件不是 shell 脚本时才可能具有仅执行权限;shell 脚本需要被 shell 解释器读取(因此需要读取权限)。否则,对于二进制可执行文件,不需要读权限;只是执行权限。

[benji@laptop ~]$ echo hello >file
[benji@laptop ~]$ chmod 000 file #no permissions, start out with a clean slate
[benji@laptop ~]$ chmod +w file #write-only permissions
[benji@laptop ~]$ cat file #cannot read it
cat: file: Permission denied
[benji@laptop ~]$ echo hello again >file #but *can* write it
Run Code Online (Sandbox Code Playgroud)

所以是的,可以对文件具有写入权限但没有读取权限。


Aar*_*ler 8

是的,尽管没有读取权限的执行权限对非二进制文件毫无意义(因为您需要读取脚本才能执行它)。在一个目录上,执行而不读取意味着您可以遍历该目录,但不能列出它或对其内容执行任何其他操作。考虑以下路径:

/home/user/foo/bar
Run Code Online (Sandbox Code Playgroud)

如果目录foo具有模式 0711(所有者的完全权限,仅对组和世界执行),而目录bar具有模式 0755(所有者的完全权限,对其他人具有读取和执行权限),则user可能cd /home/user/foo/bar和发现以外的帐户的bar行为与任何普通目录; 但是,whilecd /home/user/foo可以工作,ls并且该目录中的任何其他命令都将因权限不足而失败(即,您无法读取该目录以列出其内容)。

对文件没有读权限的写权限正如它所暗示的那样:您可以写入文件,但不能读回您所写的内容。在多个帐户下的进程写入单个日志文件的情况下,这可能很有用,但属于一个用户的进程必须不能从属于另一个用户的进程读取(可能是敏感的)日志条目。