Linux文件权限总结

Linux 文件权限

ugoa, rwx。
参考: Unix 环境高级编程,第三版

对于文件,rwx 权限的含义

  • 读取文件内容需要且仅需要 r 权限。
  • 执行文件需要且仅需要 rx 权限。(文件本身必须是普通文件)
    ps:普通文件是相对于设备文件等来说的。
  • 写入文件,需要且仅需要 w 权限。
# 1. 执行文件,仅有 x 没有 r 权限是不行的
# 1.1 用户有 rx权限,可以执行
$ ll hello
-r-x------ 1 chenx chenx 24 12月  4 10:26 hello
$ cat hello
#!/bin/bash

echo hello
$ ./hello
hello
# 1.2 现在只保留 x 权限,执行失败
$ chmod u-r hello; ll hello
---x------ 1 chenx chenx 24 12月  4 10:26 hello
$ ./hello
/bin/bash: ./hello: Permission denied

# 2. 写入文件,需要且仅需要 w 权限。
# 2.1 创建文件
$ echo foo > foo
$ ll foo
-rw-rw-r-- 1 chenx chenx 4 12月  4 10:31 foo
# 2.2 只保留 w 权限
$ chmod a=,u=w foo; ll foo
--w------- 1 chenx chenx 4 12月  4 10:31 foo
# 2.4 不能读取
$ cat foo
cat: foo: Permission denied
# 2.5 但可以写入
$ echo foo2 >> foo

对于目录,rwx 权限的含义

  • 列出目录内容需要且仅需要 r 权限
  • 当需要打开一个文件时,有规则:
    对文件绝对路径中的每一个目录都需要且仅需要具有执行权限,然后需要对文件本身有适当的权限(只读、读写)
    例如,打开 /usr/include/stdio.h 时需要对目录 /、/usr、/usr/include 具备x权限
    注:目录的读和执行权限含义是不同的。读权限允许我们读目录(即获得目录下的文件列表),
    当目录是我们访问的文件路径的一部分时,执行权限允许我们通过该目录(也就是搜索该目录,寻找一个特定的文件名)
  • 在目录中创建文件,需要且仅需要对目录的 wx 权限
  • 删除目录中的文件,需要且仅需要目录的 wx 权限,对文件本身不需要任何权限

进程访问文件

进程每次打开、创建、或删除一个文件时,内核就进行文件访问权限测试。
测试可能涉及文件的所有者(st_uid和 st_gid)、进程的有效id(有效用户id和有效组id)以及
进程的附属组id。两个所有者id是文件的性质,两个有效id和附属组id则是进程的性质。
访问权限测试具体如下:

  1. 如果进程有效用户id是0(超级用户)则允许访问
  2. 如果进程的有效用户id等于文件的所有者id(即进程拥有此文件),那么如果所有者适当的访问
    权限位被设置(ugo中的u),则允许访问,否则拒绝访问
  3. 如果进程的有小组id或者附属组id之一等于文件组id,那么如果组适当的权限(ugo中的g)被
    设置则允许访问,否则拒绝访问。
  4. 若其它用户适当的权限(ugo中的o)被设置则允许访问,否则拒绝访问。

上述步骤是依次执行的,只要某一步得出了结果(允许或拒绝)则不再进行下一步测试。
例如,如果进程拥有此文件,则按用户权限批准或拒绝访问,不再看组权限。余类似。

新文件和目录的所有权

新文件或目录的所有者为当前进程的有效用户id,
新文件或目录所属的组可以是1)进程的有效组id,或者 2)它所在的目录的组id。
POSIX默认按规则1)处理,在设置父目录的SGID之后,则按规则2)处理。

Linux 文件的特殊权限

sticky bit

如今扩展了粘着位的作用。仅对目录生效。如果目录设置了粘着位,则必须是对该目录具有写权限
且满足如下条件之一的用户才能删除或重命名该目录下的文件:

  • 拥有此文件
  • 拥有此目录
  • 是超级用户
    目录/tmp和/var/tmp是设置粘着位的典型候选者:任何用户都可以在这两个目录下创建文件,但
    用户不应能删除或重命名属于其他人的文件。

SUID

对目录:未使用。
对普通文件文件:执行时设置有效用户id为文件的所有者id,而不是默认的当前用户id。

SGID

对目录:在目录中创建的新文件的id继承目录的组id,而不是默认的进程的有效组id。
对普通文件文件:如设置,则执行文件时设置有效组id为文件本身的组id,而不是当前进程的有效组id。

chmod

参考:man chmod

名字

chmod - change file mode bits

语法

chmod [OPTION]... MODE[,MODE]... FILE...
chmod [OPTION]... OCTAL-MODE FILE...
chmod [OPTION]... --reference=RFILE FILE...

本手册是 GNU 版的 chmod 命令的文档。chmod 用于设置文件的模式位,模式可以是符号形式(
指定模式变化),也可以是数字形式(指定新模式)。

This manual page documents the GNU version of chmod. chmod changes the file mode bits of each given file according to mode, which can be either a symbolic representation of changes to make, or an octal number representing the bit pattern for the new mode bits.

符号模式的语法是[ugoa...][[+-=][perms...]...],其中 perms 是零个或多个rwxXst
的字符或者ugo中的单个字符。
Q. u=g

The format of a symbolic mode is [ugoa…][[±=][perms…]…], where perms is either zero or more letters from the set rwxXst, or a single letter from the set ugo. Multiple symbolic modes can be given, separated by commas.

ugoa 的含义:

  • u 代表文件所有者
  • g 代表和文件所有者同一群组的其它用户
  • o 代表不是同一组的其它用户
  • a 代表所有用户。
    当省略ugoa时,使用缺省值 a,但在umask中设置的位不受影响。
    Q. 什么叫“但在umask中设置的位不受影响”。

Ans: 就是类似 +w 之类的模式,仍然受umask屏蔽的影响。

[chenx@lcs2 test2]$ umask
0002
[chenx@lcs2 test2]$ chmod +w hello
[chenx@lcs2 test2]$ ll hello
-rw-rw-r-- 1 chenx chenx 0 12月  4 09:26 hello
[chenx@lcs2 test2]$ chmod +wx hello
[chenx@lcs2 test2]$ ll
total 0
-rwxrwxr-x 1 chenx chenx 0 12月  4 09:26 hello

A combination of the letters ugoa controls which users’ access to the
file will be changed: the user who owns it (u), other users in the
file’s group (g), other users not in the file’s group (o), or all users
(a). If none of these are given, the effect is as if a were given, but
bits that are set in the umask are not affected.

运算符 +-= 的含义

  • + 表示设置后面指定的标志位
  • - 表示移除后面指定的标志位
  • = 表示把标志位置为后面指定的样子(指定的被设置,未指定的被移除,但目录的suid和sgid不受影响)。

The operator + causes the selected file mode bits to be added to the
existing file mode bits of each file; - causes them to be removed; and
= causes them to be added and causes unmentioned bits to be removed
except that a directory’s unmentioned set user and group ID bits are
not affected.

字符 rwxXst 的含义:

  • r 读权限
  • w 写
  • x 对于文件是执行权限,对于目录是搜索权限
  • X xecute/search only if the file is a directory or already has execute permission for some user
  • s set user or group ID on execution
  • t restricted deletion flag or sticky bit
    另外还可以指定单个的 ugo 作为作为权限说明(等效于替换为对应的权限,Q。Xst受影响吗?)

Q. 特殊权限位 Xst 的含义?

The letters rwxXst select file mode bits for the affected users: read
®, write (w), execute (or search for directories) (x), execute/search
only if the file is a directory or already has execute permission for
some user (X), set user or group ID on execution (s), restricted dele‐
tion flag or sticky bit (t). Instead of one or more of these letters,
you can specify exactly one of the letters ugo: the permissions granted
to the user who owns the file (u), the permissions granted to other
users who are members of the file’s group (g), and the permissions
granted to users that are in neither of the two preceding categories
(o).


[chenx@lcs2 test2]$ ll
total 0
-rwxrwxr-x 1 chenx chenx 0 12月  4 09:26 hello
# 清除所有权限
[chenx@lcs2 test2]$ chmod a= hello
[chenx@lcs2 test2]$ ll
total 0
---------- 1 chenx chenx 0 12月  4 09:26 hello
chmod a= hello
[chenx@lcs2 test2]$ chmod u+rw hello
[chenx@lcs2 test2]$ ll hello
-rw------- 1 chenx chenx 0 12月  4 09:26 hello
# 复制 u 的权限给 g
[chenx@lcs2 test2]$ chmod g=u hello
[chenx@lcs2 test2]$ ll hello
-rw-rw---- 1 chenx chenx 0 12月  4 09:26 hello

数值表示的模式,由 1 ~ 4 个八进制数字构成,指定的数字不足四位时前头补零。
ps:每个八进制数字刚好三个二进制位,从左至右每个二进制位正好对应rwx(或者sXt)(000~111)

  • 第一个数字的三个二进制位分别对应 SUID, SGID, Sticky
  • 第二个数字对应 u 的 rwx,
  • 第三个数字对应 g 的 rwx,
  • 第四个数字对应 o 的 rwx。

A numeric mode is from one to four octal digits (0-7), derived by
adding up the bits with values 4, 2, and 1. Omitted digits are assumed
to be leading zeros. The first digit selects the set user ID (4) and
set group ID (2) and restricted deletion or sticky (1) attributes. The
second digit selects permissions for the user who owns the file: read
(4), write (2), and execute (1); the third selects permissions for
other users in the file’s group, with the same values; and the fourth
for other users not in the file’s group, with the same values.

chmod 不会修改符号链接的权限位,系统调用 chmod 也无法修改符号链接的权限位,因为符号
链接的权限位从不会被用到。
对于在命令行中列出对符号链接,chmod命令会把权限变动应用到链接文件实际指向的文件(对于多层符号链接也是如此),
对于递归处理目录时遇到的符号链接,chmod命令会忽略它。

chmod never changes the permissions of symbolic links; the chmod system
call cannot change their permissions. This is not a problem since the
permissions of symbolic links are never used. However, for each sym‐
bolic link listed on the command line, chmod changes the permissions of
the pointed-to file. In contrast, chmod ignores symbolic links encoun‐
tered during recursive directory traversals.

SUID和SGID权限位

SETUID AND SETGID BITS

chmod clears the set-group-ID bit of a regular file if the file’s group
ID does not match the user’s effective group ID or one of the user’s
supplementary group IDs, unless the user has appropriate privileges.
Additional restrictions may cause the set-user-ID and set-group-ID bits
of MODE or RFILE to be ignored. This behavior depends on the policy
and functionality of the underlying chmod system call. When in doubt,
check the underlying system behavior.

chmod preserves a directory’s set-user-ID and set-group-ID bits unless
you explicitly specify otherwise. You can set or clear the bits with
symbolic modes like u+s and g-s, and you can set (but not clear) the
bits with a numeric mode.

sticky 位

RESTRICTED DELETION FLAG OR STICKY BIT

The restricted deletion flag or sticky bit is a single bit, whose
interpretation depends on the file type. For directories, it prevents
unprivileged users from removing or renaming a file in the directory
unless they own the file or the directory; this is called the
restricted deletion flag for the directory, and is commonly found on
world-writable directories like /tmp. For regular files on some older
systems, the bit saves the program’s text image on the swap device so
it will load more quickly when run; this is called the sticky bit.

我补充一些例子

[chenx@lcs2 test2]$ ll hello
-rw-rw--w- 1 chenx chenx 24 12月  4 10:26 hello
# 1. 这个示例表明,多个符号表示的权限,是依次应用的
# 1.1
[chenx@lcs2 test2]$ chmod a=,u=x hello
[chenx@lcs2 test2]$ ll hello
---x------ 1 chenx chenx 24 12月  4 10:26 hello
# 1.2
[chenx@lcs2 test2]$ chmod u=x,a= hello
[chenx@lcs2 test2]$ ll hello
---------- 1 chenx chenx 24 12月  4 10:26 hello
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值