Linux中的每一个文件(包括目录)都有r(读)、w(写)、x(执行)三种权限,对于文件和目录,这三种权限的意义不一样,具体区别如下表:
|
文件 |
目录 |
r |
读取文件内容 |
只能读取目录中文件的文件名 |
w |
修改文件内容 |
修改目录中的文件(添加,删除文件等) |
x |
执行文件(针对可执行文件) |
进入目录 |
文件和目录的所有者可能是用户(user)、组(group)或者其他人(others),Linux针对不同的所有者可以设置不同的权限,故每一个文件或者目录都有三组权限,分别为用户权限,组权限和其他人权限。
[stone@localhost ~]$ ll
drwxrwxr-x 2 stone stone 4096 Feb 16 14:55 file1
如上,其中红色一组表示用户权限,蓝色一组表示组权限,黑色一组表示其他人权限。
权限可以采用三组八进制来表示,每一组内的字母对应的数值如下:
权限 |
数值 |
r |
4 |
w |
2 |
x |
1 |
-(表示无权限) |
0 |
故字母表示的权限就可以换算成数字表示的权限了,如rwxrwxr-x,就可以换算成775(4+2+1 4+2+1 4+1)。
改变文件和目录的权限chmod(change mode)
[stone@localhost ~]$ chmod 400 file1/
[stone@localhost ~]$ ll
dr-------- 2 stone stone 4096 Feb 16 14:55 file1
文件的权限比较好理解,目录的权限比较难理解,下面只针对目录权限举例:
[stone@localhost ~]$ mkdir file1
[stone@localhost ~]$ cd file1
[stone@localhost file1]$ touch f1 f2
[stone@localhost ~]$ tree file1/
file1/
|-- f1
`-- f2
0 directories, 2 files
[stone@localhost ~]$ ll -a file1/
total 8
drwxr-xr-x 2 stone stone 4096 Feb 16 15:57 .
drwx------ 4 stone stone 4096 Feb 16 14:55 ..
-rw-rw-r-- 1 stone stone 0 Feb 16 15:57 f1
-rw-rw-r-- 1 stone stone 0 Feb 16 14:55 f2
#新建一个目录file1,在file1下新建两个文件f1,f2。
[stone@localhost ~]$ chmod 400 file1/
[stone@localhost ~]$ ll -d file1/
dr-------- 2 stone stone 4096 Feb 16 15:57 file1/
[stone@localhost ~]$ ll file1/
total 0
?--------- ? ? ? ? ? f1
?--------- ? ? ? ? ? f2
[stone@localhost ~]$ rm file1/f1
rm: cannot remove `file1/f1': Permission denied
[stone@localhost ~]$ cd file1/
-bash: cd: file1/: Permission denied
#仅有r权限,只能查看目录内的文件,不能删除目录内的文件,不可以进入目录。
[stone@localhost ~]$ chmod 200 file1/
[stone@localhost ~]$ ll -d file1/
d-w------- 2 stone stone 4096 Feb 16 16:14 file1/
[stone@localhost ~]$ ll file1/
ls: file1/: Permission denied
[stone@localhost ~]$ rm file1/f2
rm: cannot remove `file1/f2': Permission denied
[stone@localhost ~]$ cd file1/
-bash: cd: file1/: Permission denied
#只有w权限,不能查看目录内的文件,不能删除目录内的文件,不可以进入目录。
[stone@localhost ~]$ chmod 100 file1
[stone@localhost ~]$ ll -d file1/
d--x------ 2 stone stone 4096 Feb 16 15:57 file1/
[stone@localhost ~]$ ll file1/
ls: file1/: Permission denied
[stone@localhost ~]$ rm file1/f1
rm: cannot remove `file1/f1': Permission denied
[stone@localhost ~]$ cd file1/
[stone@localhost file1]$
#仅有x权限,不能查看目录内的文件,不能删除目录内的文件,但可以进入目录。
[stone@localhost ~]$ chmod 300 file1/
[stone@localhost ~]$ ll -d file1/
d-wx------ 2 stone stone 4096 Feb 16 15:57 file1/
[stone@localhost ~]$ ll file1/
ls: file1/: Permission denied
[stone@localhost ~]$ rm file1/f1
[stone@localhost ~]$ cd file1/
[stone@localhost file1]$
#有w、x权限,不能查看目录内的文件,可以删除目录内的文件,可以进入目录。
[stone@localhost ~]$ chmod 500 file1/
[stone@localhost ~]$ ll -d file1/
dr-x------ 2 stone stone 4096 Feb 16 16:14 file1/
[stone@localhost ~]$ ll file1/
total 0
-rw-rw-r-- 1 stone stone 0 Feb 16 14:55 f2
[stone@localhost ~]$ rm file1/f2
rm: cannot remove `file1/f2': Permission denied
[stone@localhost ~]$ cd file1/
[stone@localhost file1]$
#有r、x权限,可以查看目录内的文件,不能删除目录内的文件,可以进入目录。
[stone@localhost ~]$ chmod 600 file1/
[stone@localhost ~]$ ll -d file1/
drw------- 2 stone stone 4096 Feb 16 16:14 file1/
[stone@localhost ~]$ ll file1/
total 0
?--------- ? ? ? ? ? f2
[stone@localhost ~]$ rm file1/f2
rm: cannot remove `file1/f2': Permission denied
[stone@localhost ~]$ cd file1/
-bash: cd: file1/: Permission denied
#有r、w权限,可以查看目录内的文件,不能删除目录内的文件,不可以进入目录。
综上,对于目录,w一定要配合x使用,单独使用没有意义。