了解 Linux 中文件的权限

4 linux

我想知道对文件的读/写访问权限,所以我这样做了

ls -l Denem.sh
Run Code Online (Sandbox Code Playgroud)

这个命令显示

-rwxr-xr-x  1 pavan employee 672 DEC 20  2000 pavan.sh
Run Code Online (Sandbox Code Playgroud)

有人能告诉我 -rwxr-xr-x 表示什么吗??
从 Wikipedia.com 我发现 -rwxr-xr-x 表示

-rwxr-xr-x
用于其用户类具有完全权限且其组和其他类仅具有读取和执行权限的常规文件。

现在我的问题是我们应该考虑中间的事情(-xr-)吗??

n0p*_*0pe 19

这个字符串:

-rwxr-xr-x
Run Code Online (Sandbox Code Playgroud)

分为四个部分:

-    indicates what kind of file it is
rwx  (first 3) owner permissions
r-x  (second 3) group permissions
r-x  (last 3) other permissions
Run Code Online (Sandbox Code Playgroud)

总之,这个字符串应该提供(一目了然)文件最重要的方面。即它是什么以及谁可以用它做什么。


第一节

字符串中的第一个字符是为文件类型保留的。任何常规的旧文件-在这个位置都会有一个。其他包括:

d directory
p pipe/fifo
l link
c character device file
b block device file
s local domain socket
Run Code Online (Sandbox Code Playgroud)

所以一个管道可能看起来像这样:

prwx------ root root filename
Run Code Online (Sandbox Code Playgroud)

第二 - 第三 - 第四节

接下来的 9 位描述了每个人对该文件的权限。权限分为三种:

r read (opening the file for reading, can't save changes)
w write (change the contents of file)
x execute (run the file, like a script or binary)
Run Code Online (Sandbox Code Playgroud)

这些权限可以应用于三个组:

owner whoever owns the file (as seen by the output of ls -l)
group whoever is part of the group owner of this file
others anyone who doesn't fall in either of the two above categories
Run Code Online (Sandbox Code Playgroud)

例如:

-rwxr-xr-x  1 pavan employee 672 DEC 20  2000 pavan.sh
pavan is the owner
employee is the group owner name
anyone else falls into "others"
Run Code Online (Sandbox Code Playgroud)

参考上面的例子,如果我们想要pavan完全控制文件,让employee组中的任何人读取或执行文件并阻止所有权限others

-rwxr-x---
Run Code Online (Sandbox Code Playgroud)

数字

权限有时用数字表示的原因是使用 9 位的八进制表示通常更容易(我仍然更喜欢直接使用rwx)。

要了解数字的含义,您需要构建一个表(如果您曾经使用过二进制,这将有所帮助):

#  r  w  x
0  0  0  0
1  0  0  1
2  0  1  0
3  0  1  1
4  1  0  0
5  1  0  1
6  1  1  0
7  1  1  1
Run Code Online (Sandbox Code Playgroud)

对于每组三位,请参考此图表。例如,如果我决定让文件的所有者完全控制(r、w 和 x),则只为该组读取,也只为其他人读取:

rwx owner corresponds to 7 in the table
r-- group corresponds to 4 in the table
r-- other corresponds to 4 in the table
Therefore my file has permissions 744
Run Code Online (Sandbox Code Playgroud)


Tom*_*ych 7

像这样划分:

- rwx r-x r-x
Run Code Online (Sandbox Code Playgroud)

它是所有者、组和其他人。所以所有者( pavan) 可以读、写和执行;组内的人employee可以读和执行,但不能写;其他任何人也可以读取和执行,但不能写入。

-前面是d一个目录,l一个符号链接,等等man ls