--- title: "Linux 文件系统中的权限" date: 2021-09-29T22:01:00+08:00 tags: [ linux, permission, file system ] categories: [ tech ] weight: 50 show_comments: true draft: false --- ## 常规权限 ### 读权限(r) - 对文件来说, 就是文件的读取权限, 如 `cat` . - 对目录来说, 就是查询目录下文件结构的权限, 如 `ls` . ### 写权限(w) - 对文件来说, 就是文件的写入权限, 如重定向输出到这个文件. - 对目录来说, 就是修改此目录下的文件结构的权限, 包括创建文件/删除文件/移动文件. ### 执行权限(x) - 对文件来说, 就是文件的执行权限. - 对于目录来说, 就是进入此目录的权限, 并控制访问此目录中的其他文件或目录的权限, **若无此权限, 则目录的读写权限也会受到影响**. 下面为一些各种情况下进行操作的示例. 可以发现没有 x 权限时, r 权限只能列出目录的文件名称, 不能得知其权限和修改时间等信息; w 权限无法在目录中创建文件; 无法对目录中的文件的内容进行读取或修改. 仅有 x 权限时, 不能读取目录内文件的名称, 不能在目录中创建或删除文件, 但是可以获取文件的权限信息, 可以修改目录中其他文件的内容. ``` $ ls -l dr-------- 2 leafee98 leafee 4096 Sep 29 20:57 dir $ ls -l dir ls: cannot access 'dir/a': Permission denied -????????? ? ? ? ? ? a ...... ...... $ ls -l d-w------- 2 leafee98 leafee 4096 Sep 29 20:57 dir $ touch dir/b touch: cannot touch 'dir/b': Permission denied ...... ...... $ ls -l drw------- 2 leafee98 leafee 4096 Sep 29 20:57 dir $ echo "nya~" > dir/a zsh: permission denied: dir/a $ cat dir/a cat: dir/a: Permission denied ...... ...... $ ls -l d--x------ 2 leafee98 leafee 4096 Sep 29 20:57 dir $ ls -l dir ls: cannot open directory 'dir': Permission denied $ echo "miao~" > dir/a ; cat dir/a miao~ $ rm dir/a rm: cannot remove 'dir/a': Permission denied $ touch dir/z touch: cannot touch 'dir/z': Permission denied $ ls -l dir/a -rw-r--r-- 1 leafee98 leafee 6 Sep 29 21:18 dir/a ``` ## 特殊权限 ### 用户特殊权限(SUID user+s) - 标记到文件(file)时, 此文件在运行时, 会以文件属主的身份运行. - 标记到目录(directory)时, 不会有其他的作用. 如命令 `sudo` 就是属主为 root 的添加 SUID 标记的可执行文件. ``` $ ls -l /usr/bin/sudo -rwsr-xr-x 1 root root 223536 Sep 22 14:19 /usr/bin/sudo ``` ### 组特殊权限(SGID group+s) - 标记到文件时, 文件在运行时会以文件属组的身份运行. - 标记到目录, 所有在此目录下创建的文件, 其属组均会变为该目录的属组. (移动文件至此目录下属组不会改变) ``` $ sudo chmod g+s dir ; ls -l drwsrwsrwx 2 mail daemon 4096 Sep 29 21:40 dir $ touch file ; mv file dir/ $ touch dir/file2 $ ls -l dir -rw-r--r-- 1 leafee98 leafee 0 Sep 29 21:35 file -rw-r--r-- 1 leafee98 daemon 0 Sep 29 21:35 file2 ``` ### 沾滞位(STICKY other+t) - 标记到文件时, 不会产生任何效果. - 标记到目录时, 对于此目录下的所有文件, 将仅有这些文件的属主分别对其有删除权限. ``` $ sudo chmod o+t dir ; ls -l drwxrwxrwt 2 mail daemon 4096 Sep 29 21:38 dir $ touch file1 file2 ; sudo chown mail file2 ; ls -l -rw-r--r-- 1 leafee98 daemon 0 Sep 29 21:38 file1 -rw-r--r-- 1 mail daemon 0 Sep 29 21:38 file2 $ rm file1 file2 ; ls -l rm: cannot remove 'file2': Operation not permitted -rw-r--r-- 1 mail leafee 0 Sep 29 21:38 file2 ``` ## 在命令中使用 ### chmod 中的标记 常规权限的 r 为 4, w 为 2, x 为 1. 且分别对应属主(或称为用户 user), 属组(group), 其他(other). ``` $ chmod o+w file $ chmod g-x file $ chmod 755 file ``` 特殊权限的 SUID 为 4, SGID 为 2, STICKY 为 1, 且数字表示权限时不与常规权限同时计算. ``` $ chmod u+s file $ chmod g-s file $ chmod o+t file $ chmod 6755 file ``` ### ls 命令中的标记 特殊权限在 ls 命令的输出中会占据常规权限 x 的位置, 如果原来文件或目录已经具有 x 权限, 则新添加的 SUID, SGID, STICKY 会以小写字母 s 或 t 占在相应位置. 如果原来文件或目录不具有 x 权限, 则出现在相应位置的字母会是大写形式. ``` $ sudo chmod 7777 dir ; ls -l drwsrwsrwt 2 mail daemon 4096 Sep 29 21:40 dir $ sudo chmod 7666 dir ; ls -l drwSrwSrwT 2 mail daemon 4096 Sep 29 21:40 dir ``` ## 参考 1. https://www.redhat.com/sysadmin/suid-sgid-sticky-bit 2. https://superuser.com/questions/471844/why-is-setuid-ignored-on-directories 4. https://unix.stackexchange.com/questions/21251/execute-vs-read-bit-how-do-directory-permissions-in-linux-work/21252#21252