根据权限组划分:每个文件和目录都有3种使用者(用户)
每个文件或目录都有3中基本的权限:
在终端上使用指令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
在命令行中,可以使用指令chmod
来更改文件的权限;chmod
意为change mode
;
有两种更改文件或目录方法:
权限组用下列字符来代替:
赋值运算符包括:+(plus) 和 - (minus),赋值运算符的使用是用来告诉系统是否需要添加或者是去除特定的权限;
权限种类用下列字符代替:
实例
[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
;
首先需要理解的是:利用二进制引用来设置文件权限,输入是通过输入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 |
这里涉及的是如何更改文件的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
[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 | 目录文件 |