simpleowen / simpleowen.github.io

https://simpleowen.github.io
1 stars 2 forks source link

Linux 中的文件权限 #1

Closed simpleowen closed 5 years ago

simpleowen commented 5 years ago

本文讲述, 文件权限在 Linux 中是如何查看, 修改的, 主要内容包括:

simpleowen commented 5 years ago

参考链接

simpleowen commented 5 years ago

一个在线的权限计算器

simpleowen commented 5 years ago

ls -l

root@debuguself:~# ll
total 52
drwx------  6 root root 4096 Aug 26 03:04 ./
drwxr-xr-x 23 root root 4096 Sep 11 06:19 ../
-rw-------  1 root root 2958 Aug 26 15:41 .bash_history
-rw-r--r--  1 root root 3106 Apr  9  2018 .bashrc
drwx------  2 root root 4096 May 26 01:46 .cache/
-rw-r--r--  1 root root    0 May 26 01:45 .cloud-locale-test.skip
drwx------  3 root root 4096 May 26 13:28 .gnupg/
drwxr-xr-x  3 root root 4096 Aug 26 03:04 .local/
-rw-r--r--  1 root root  148 Aug 17  2015 .profile
-rw-------  1 root root    7 May 26 01:49 .python_history
drwx------  2 root root 4096 Jun  5 14:48 .ssh/
-rw-------  1 root root 9922 Jun  6 02:21 .viminfo

Linux 中一切皆文件, 文件的权限对象有 3 类, 分别是

文件的权限分为 3 种, 分别是

ls -l 命令的输出

simpleowen commented 5 years ago

设置权限

chmod

有两种形式

八进制

rwx 分别代表数字 4, 2, 1

示例

符号

增加权限用 +, 撤回权限用 -, 赋值权限用 =

示例

simpleowen commented 5 years ago

umask

root@debuguself:~# umask
0022

使用 umask 命令可获得当前权限掩码值

新建一个测试文件

root@debuguself:~# touch a
root@debuguself:~# ls -l
-rw-r--r--  1 root root    0 Sep 14 06:26 a

可以看到user, group, other 的权限组为 rw-r--r--

修改 umask 值

root@debuguself:~# umask 0000

再次创建测试文件 b

root@debuguself:~# touch b
root@debuguself:~# ls -l
-rw-r--r--  1 root root    0 Sep 14 06:26 a
-rw-rw-rw-  1 root root    0 Sep 14 06:30 b

这次, b 的权限值比刚才 a 的权限值差异很明显, group 和 other 多了 w 权限

对比前后 umask 值

通常 umask 值使用 3 位八进制数字分别代表 user, group, other 的权限掩码, 与 chmod 的八进制形式类似.

所谓掩码即遮掩, 2 代表 w, 也就是遮掩 w 权限.

所以, 0022 表示在新建文件时的默认权限值上去除 group 和 other 的 w 权限.

simpleowen commented 5 years ago

umask 补充

在内核级别, Linux 使用默认模式