在 Linux 中,文件权限、属性和所有权控制系统进程和用户对文件的访问级别。这确保只有授权的用户和进程才能访问特定的文件和目录。
Linux 文件权限#
基本的 Linux 权限模型的工作原理是将每个系统文件与所有者和组相关联,并为三类不同的用户分配权限访问权限:
可以使用以下命令更改文件所有权chown
and chgrp命令。
三种文件权限类型适用于每一类用户:
这个概念允许您控制哪些用户可以读取文件、写入文件或执行文件。
要查看文件权限,请使用ls命令:
ls -l file_name
-rw-r--r-- 12 linuxize users 12.0K Apr 28 10:10 file_name
|[-][-][-]- [------] [---]
| | | | | | |
| | | | | | +-----------> 7. Group
| | | | | +-------------------> 6. Owner
| | | | +--------------------------> 5. Alternate Access Method
| | | +----------------------------> 4. Others Permissions
| | +-------------------------------> 3. Group Permissions
| +----------------------------------> 2. Owner Permissions
+------------------------------------> 1. File Type
第一个字符表示文件类型。它可以是一个常规文件(-
), 目录 (d
), a 符号链接
(l
),或其他特殊类型的文件。接下来的九个字符代表文件权限,三个三元组,每个三元组。第一个三元组显示所有者权限,第二个三元组显示组权限,最后一个三元组显示其他人权限。
在上面的例子中(rw-r--r--
)表示文件所有者拥有读写权限(rw-
),该组和其他人只有读取权限(r--
).
根据文件类型的不同,文件权限具有不同的含义。
三个权限三元组中的每一个都可以由以下字符构成,并具有不同的效果,具体取决于它们是设置为文件还是目录:
权限对文件的影响
Permission |
Character |
Meaning on File |
Read |
- |
The file is not readable. You cannot view the file contents. |
|
r |
The file is readable. |
Write |
- |
The file cannot be changed or modified. |
|
w |
The file can be changed or modified. |
Execute |
- |
The file cannot be executed. |
|
x |
The file can be executed. |
|
s |
If found in the user triplet, it sets the setuid bit. If found in the group triplet, it sets the setgid bit. It also means that x flag is set. When the setuid or setgid flags are set on an executable file, the file is executed with the file’s owner and/or group privileges. |
|
S |
Same as s , but the x flag is not set. This flag is rarely used on files. |
|
t |
If found in the others triplet, it sets the sticky bit. It also means that x flag is set. This flag is useless on files. |
|
T |
Same as, t but the x flag is not set. This flag is useless on files. |
权限对目录(文件夹)的影响
目录是可以包含其他文件和目录的特殊类型的文件。
Permission |
Character |
Meaning on Directory |
Read |
- |
The directory’s contents cannot be shown. |
|
r |
The directory’s contents can be shown. (e.g., You can list files inside the directory with ls
.) |
Write |
- |
The directory’s contents cannot be altered. |
|
w |
The directory’s contents can be altered. (e.g., You can create new files
, delete files
..etc.) |
Execute |
- |
The directory cannot be changed to. |
|
x |
The directory can be navigated using cd
. |
|
s |
If found in the user triplet, it sets the setuid bit. If found in the group triplet it sets the setgid bit. It also means that x flag is set. When the setgid flag is set on a directory, the new files created within it inherits the directory group ID (GID) instead of the primary group ID of the user who created the file.
setuid has no effect on directories. |
|
S |
Same as s , but the x flag is not set. This flag is useless on directories. |
|
t |
If found in the others triplet, it sets the sticky bit. It also means that x flag is set. When the sticky bit is set on a directory, only the file’s owner, the directory’s owner, or the administrative user can delete or rename the files within the directory. |
|
T |
Same as t , but the x flag is not set. This flag is useless on directories. |
更改文件权限#
可以使用以下命令更改文件权限chmod
命令。只有 root、文件所有者或具有 sudo 权限的用户才能更改文件的权限。使用时要格外小心chmod
,尤其是在递归更改权限时。该命令可以接受一个或多个以空格分隔的文件和/或目录作为参数。
可以使用符号模式、数字模式或参考文件来指定权限。
符号(文本)方法#
的语法chmod
使用符号模式时的命令具有以下格式:
chmod [OPTIONS] [ugoa…][-+=]perms…[,…] FILE...
第一组标志([ugoa…]
)、用户标志,定义文件权限发生更改的用户类别。
-
u
- 文件所有者。
-
g
- 作为组成员的用户。
-
o
- 所有其他用户。
-
a
- 所有用户,相同ugo
.
当用户的标志被省略时,它默认为a
.
第二组标志([-+=]
),操作标志,定义是否要删除、添加或设置权限:
-
-
- 删除指定的权限。
-
+
- 添加指定权限。
-
=
- 将当前权限更改为指定权限。如果之后没有给予权限=
符号,指定用户类的所有权限都将被删除。
权限(perms...
) 使用零个或一个或多个以下字母显式设置:r
, w
, x
, X
, s
, and t
。使用集合中的单个字母u
, g
, and o
将权限从一个用户的类复制到另一用户的类时。
当为多个用户类别设置权限时([,…]
),使用逗号(不带空格)分隔符号模式。
以下是一些如何使用的示例chmod
符号模式下的命令:
-
授予组成员执行该文件的权限,但不能读取和写入该文件:
chmod g=x filename
-
删除所有用户的写权限:
chmod a-w filename
-
递归删除其他用户的执行权限:
chmod -R o-x dirname
-
删除除文件所有者之外的所有用户的读、写和执行权限:
chmod og-rwx filename
同样的事情也可以通过使用以下形式来完成:
chmod og= filename
-
向文件所有者授予读取、写入和执行权限,向文件所属组授予读取权限,向所有其他用户授予无权限:
chmod u=rwx,g=r,o= filename
数值法#
的语法chmod
使用符号模式时的命令具有以下格式:
chmod [OPTIONS] NUMBER FILE...
使用数字模式时,您可以同时设置所有三个用户类别(所有者、组和所有其他)的权限。
权限编号可以是 3 位或 4 位数字。当使用3位数字时,第一个数字代表文件所有者的权限,第二个数字代表文件所属的组,最后一个数字代表所有其他用户。
每个写入、读取和执行权限都具有以下数值:
-
r
(读)= 4
-
w
(写)= 2
-
x
(执行)= 1
- 无权限 = 0
特定用户类别的权限数由该组的权限值的总和表示。
要找出数字模式下文件的权限,只需计算所有用户类别的总数即可。例如,要向文件所有者授予读、写和执行权限,向文件组授予读和执行权限,向所有其他用户授予只读权限,您可以执行以下操作:
- 所有者:rwx=4+2+1=7
- 组:r-x=4+0+1=5
- 其他:r-x=4+0+0=4
使用上面的方法,我们得出数字754
,它代表所需的权限。
要设置setuid
, setgid
, and sticky bit
标志,使用四位数字。
当使用 4 位数字时,第一位数字的含义如下:
- setuid=4
- setgid=2
- sticky=1
- 没有变化 = 0
接下来的三位数字与使用 3 位数字时的含义相同。
如果第一位为0则可以省略,模式可以用3位数字表示。数字模式0755
是相同的755
.
要计算数值模式,还可以使用另一种方法(二进制方法),但稍微复杂一些。对于大多数用户来说,了解如何使用 4、2 和 1 计算数字模式就足够了。
您可以使用数字符号检查文件的权限stat命令:
以下是一些如何使用的示例chmod
数字模式下的命令:
-
为文件所有者授予读写权限,并为组成员和所有其他用户授予只读权限:
chmod 644 dirname
-
授予文件所有者读取、写入和执行权限,授予组成员读取和执行权限,授予所有其他用户无权限:
chmod 750 dirname
-
授予给定目录的读、写和执行权限以及粘滞位:
chmod 1777 dirname
-
递归地为文件所有者设置读、写和执行权限,而对给定目录的所有其他用户没有权限:
chmod -R 700 dirname
结论#
在 Linux 中,使用文件权限、属性和所有权来限制对文件的访问。要更改文件的权限,请使用chmod
命令。
如果您有任何问题或反馈,请随时发表评论。