理解Linux文件权限
Permission Groups(权限组)
根据权限组划分:每个文件和目录都有3种使用者(用户)
- ower(所有者) - 所有者的权限仅适用于文件和目录的所有者,不会影响其他用户的操作;
- group(所属组) - 所属组的权限仅适用于已分配的文件和目录,不会影响其他用户的操作;
- all users(所有用户) - 所有用户的权限适用于所有用户;
Permission Types(权限种类)
每个文件或目录都有3中基本的权限:
- read(读) - 此权限授予您打开和读取文件的权限。对目录的读取权限使您能够列出其内容;
- write(写) - 对目录的权限赋予您添加、删除和重命名目录中存储的文件的权限。考虑一个场景,您必须对文件具有写入权限,但对存储文件的目录没有写入权限。您将能够修改文件内容。但您将无法重命名、移动或从目录中删除该文件;
- execute(执行) - 在Windows中,可执行程序通常有一个扩展名“.exe”,您可以轻松运行该扩展名。在Unix/Linux中,除非设置了执行权限,否则无法运行程序。如果未设置执行权限,您仍可以查看/修改程序代码(前提是设置了读写权限),但不能运行它;
Viewing the Permissions(查看权限)
在终端上使用指令ls -l 或 ll
均可查看当前目录下的文件或目录的权限
如果要查看当前所处目录的权限,可以使用指令ls -l -d 或 ll -d
[hzh@VM-8-3-centos lesson5]$ ls
test.c
// 当前目录下只有test.c一个文件
// ls-l 或 ll 查看当前目录下文件或目录的权限
[hzh@VM-8-3-centos lesson5]$ ls -l
total 0
-rw-rw-r-- 1 hzh hzh 0 Nov 14 13:10 test.c
[hzh@VM-8-3-centos lesson5]$ ll
total 0
-rw-rw-r-- 1 hzh hzh 0 Nov 14 13:10 test.c
// ls -l -d 或 ll -d查看当前目录的权限
[hzh@VM-8-3-centos lesson5]$ ll -d
drwxrwxr-x 2 hzh hzh 4096 Nov 14 13:10 .
[hzh@VM-8-3-centos lesson5]$ ls -l -d
drwxrwxr-x 2 hzh hzh 4096 Nov 14 13:10 .
命令行中的权限显示为:_rwxrwxr-x 2 owner:group
- 用户的权限
- 第一个用下划线标志的字符代表的是一种特殊的权限,该权限可以变化;
- 接下来的3个字符集(rwx),代表的是owner(拥有者)的权限;可读可写可执行
- 第二个的3个字符集(rwx),代表的是group(所属组)的权限;可读可写可执行
- 第三个的3个字符集(r-x),代表的是All Users(所有用户的权限);可读不可写可执行
- 接下来的一组数字显示的是该文件或目录的硬链接数;
- 最后一部分显示的是owner和group,格式为owner:group;
Modifying the Permissions(更改权限)
在命令行中,可以使用指令chmod
来更改文件的权限;chmod
意为change mode
;
有两种更改文件或目录方法:
1. Explicitly Defining Permissions(显式定义权限)
权限组用下列字符来代替:
- u - Owner
- g - Group
- o - Other
- a - All users
赋值运算符包括:+(plus) 和 - (minus),赋值运算符的使用是用来告诉系统是否需要添加或者是去除特定的权限;
权限种类用下列字符代替:
- r - Read
- w - Write
- x - Excute
实例
[hzh@VM-8-3-centos lesson5]$ ls
test.c
[hzh@VM-8-3-centos lesson5]$ ll
total 0
-rw-rw-r-- 1 hzh hzh 0 Nov 14 13:10 test.c
[hzh@VM-8-3-centos lesson5]$ chmod a-rw test.c
[hzh@VM-8-3-centos lesson5]$ ll
total 0
---------- 1 hzh hzh 0 Nov 14 13:10 test.c
[hzh@VM-8-3-centos lesson5]$ chmod a+rw test.c
[hzh@VM-8-3-centos lesson5]$ ll
total 0
-rw-rw-rw- 1 hzh hzh 0 Nov 14 13:10 test.c
由上述实例可见,显式定义权限的语法形式为:chmod Permission Groups assignment operator(+/-) filename
;
2. Using Binary References to Set permissions(使用二进制引用来设置文件权限)
首先需要理解的是:利用二进制引用来设置文件权限,输入是通过输入3个整数来完成的!
举个例子:
chmod 640 filename
,这段代码的意思是:owner(所有者)的权限是可读可写,group(所属组)的权限是可读,all other users(其他人)对文件没有任何权限;
第一个数字代表的是Owner(拥有者)的权限,第二个数字代表的是Group(所属组)的权限,最后一个数字代表的是All other users(其他人)的权限;
一个权限组最多只需要3个二进制数即可表示,对应位置如果是0表示没有对应权限,如果是1表示拥有对应的权限;
下面的表格中的数字代表的不同的权限种类:
Number | Permission Type | Symbol |
---|---|---|
0 (000) | No Permission | — |
1 (001) | Excute | –x |
2 (010) | Write | -w- |
3 (011) | Write + Excute | -wx |
4 (100) | Read | r– |
5 (101) | Read + Excute | r-x |
6 (110) | Read + Write | rw- |
7 (111) | Read + Write + Excute | rwx |
3. Owners and Groups(拥有者和所属组)
这里涉及的是如何更改文件的Owner(拥有者)和Group(所属组)
使用chown和chgrp指令即可:
语法:
chown Owner(:Group) filename
chgrp Group filename
如果是普通用户使用上述指令前要在其前面加上sudo进行提权:
举个例子:刚开始的时候文件test.c的拥有者和所属组都是hzh
[hzh@VM-8-3-centos lesson5]$ ll
total 0
-rw-rw-rw- 1 hzh hzh 0 Nov 14 13:10 test.c
// 将Owner和Group全部改成root
[hzh@VM-8-3-centos lesson5]$ sudo chown root:root test.c
[sudo] password for hzh:
[hzh@VM-8-3-centos lesson5]$ ll
total 0
-rw-rw-rw- 1 root root 0 Nov 14 13:10 test.c
// 只将Owner改成hzh
[hzh@VM-8-3-centos lesson5]$ sudo chown hzh test.c
[hzh@VM-8-3-centos lesson5]$ ll
total 0
-rw-rw-rw- 1 hzh root 0 Nov 14 13:10 test.c
// [hzh@VM-8-3-centos lesson5]$ sudo chgrp hzh test.c
[hzh@VM-8-3-centos lesson5]$ ll
total 0
-rw-rw-rw- 1 hzh hzh 0 Nov 14 13:10 test.c
Linux文件类型
[hzh@VM-8-3-centos lesson5]$ ll
total 0
-rw-rw-rw- 1 hzh hzh 0 Nov 14 13:10 test.c
-rw-rw-rw-,该段字符中的第一个代表的是Linux中的文件类型:
这里仅列出比较重要的文件类型
字符 | 代表的意义 |
---|---|
- | 普通文件(源代码,库文件,可执行程序,压缩包…) |
d | 目录文件 |