chmod

chmod 功能说明:chmod命令是用来改变文件或目录权限的命令,但是只有文件的属主和超级用户root才能够执行这个命令。

参数选项:
-R 递归处理指定目录及其子目录下的所有文件。

权限对应表:
权限位: r
全称: read
含义: 可读权限
对应数字:4

权限位: w
全称: write
含义: 可写权限
对应数字:2

权限位: x
全称: execute
含义: 可执行权限
对应数字: 1

权限位: -
含义: 没有执行权限
对应数字: 0


一些特殊权限位:t、T、s、S、X、x

用户类型:
文件所属用户:u(Owner/User)
文件所属用户组:g(Group)
其他用户:o(Other)
所有:a(ALL),等效于u、g、o的总和

操作字符
+:加入
-:减去
=:设置


字母和数字权限转换说明:
rwxr-x-wx
421401021

其中:
421 => 7
401 => 5
021 => 3
 
 
权限字母和操作符表达式
[root@testdb62 tmp]# touch file.log
[root@testdb62 tmp]# ll file.log
-rw-r--r-- 1 root root 0 Dec 15 16:57 file.log
[root@testdb62 tmp]# chmod a= file.log
[root@testdb62 tmp]# ll file.log
---------- 1 root root 0 Dec 15 16:57 file.log
[root@testdb62 tmp]# chmod u+x file.log
[root@testdb62 tmp]# ll file.log
---x------ 1 root root 0 Dec 15 16:57 file.log
[root@testdb62 tmp]# chmod g+w file.log 
[root@testdb62 tmp]# ll file.log
---x-w---- 1 root root 0 Dec 15 16:57 file.log
[root@testdb62 tmp]# chmod o+r file.log 
[root@testdb62 tmp]# ll file.log
---x-w-r-- 1 root root 0 Dec 15 16:57 file.log
[root@testdb62 tmp]# chmod ug+r,o-r file.log
[root@testdb62 tmp]# ll file.log
-r-xrw---- 1 root root 0 Dec 15 16:57 file.log
[root@testdb62 tmp]# chmod u=rwx,g=rx,o=x file.log
[root@testdb62 tmp]# ll file.log
-rwxr-x--x 1 root root 0 Dec 15 16:57 file.log



文件的数字权限授权案例
[root@testdb62 tmp]# chmod 000 file.log
[root@testdb62 tmp]# ll file.log
---------- 1 root root 0 Dec 15 16:57 file.log
[root@testdb62 tmp]# chmod 755 file.log 
[root@testdb62 tmp]# ll file.log
-rwxr-xr-x 1 root root 0 Dec 15 16:57 file.log


普通文件的读、写、执行权限说明
可读r:表示具有读取/阅读文件内容的权限。

可写w:表示有新增、修改文件内容的权限。
(1)如果没有r,用vi编辑器,输入“:wq!”可以强制覆盖,但原文件内容会被清除,因此可以用echo追加内容到文件(echo "hello">> file.log)。
(2)删除文件(修改文件名等)的权限是受父目录的权限控制,和文件本身的权限无关。

可执行x:表示具有执行文件的权限。
(1)文件本身要能够执行
[root@testdb62 tmp]# ls -lh file.sh 
---------- 1 root root 0 Dec 15 17:00 file.sh
[root@testdb62 tmp]# ./file.sh
-bash: ./file.sh: Permission denied
 

下面三种方法都是通过其他命令来实现的
[root@testdb62 tmp]# . file.sh
[root@testdb62 tmp]#
[root@testdb62 tmp]# source file.sh
[root@testdb62 tmp]#
[root@testdb62 tmp]# sh file.sh2)普通用户必须还要有r权限才能够执行,无r就不能执行。
(3)root即使没有r权限,只要有x权限就能执行。
(4)root用户位没有执行权限,但只要其他权限位还有x权限,那它就能执行。

目录的读、写、执行权限说明
可读r:表示具有浏览目录下文件及子目录的权限,即 ls dir。
(1)没有x不能进入到目录里,即无法cd dir。
(2ls 列表可以看到所有的文件名,不过会提示无权访问目录下的文件。
(3)如果 ls -l 列表,则所有的属性会带有问号,也会提示无权访问目录下的文件,但是可以看到所有文件名。

可写w:表示具有增加、删除或修改目录内文件名(一般指文件名)的权限(需要x权限配合)。
(1)增加的不是文件内容,而是创建一个新的文件。
(2)修改的不是文件内容(这个看文件本身的权限),只能修改文件名,重命名文件(文件名是在目录的block中,看目录的权限全是w的就可以)。
(3)删除也是删除文件而不是看文件本身的权限,是看目录的权限,如果没有x权限则不能删除。

可执行x:表示具有进入目录的权限,例如:cd dir。
(1)没有r则无法进入列表。
(2)没有w则无法新建文件。

 

posted @ 2018-07-31 22:23  屠魔的少年  阅读(422)  评论(0)    收藏  举报