chmod:改变文件或目录的权限

xiaohai 2021-05-05 16:59:16 1497人围观 标签: Linux 
简介chmod命令用于改变文件或目录的权限,但是只有文件或目录的属主和超级用户root才能修改。
说明

  chmod命令用于改变文件或目录的权限,但是只有文件或目录的属主和超级用户root才能修改。

格式

  chmod [选项] [模式] [file]

-rwxrwxrwx:权限未:
第1位:表示文件的类型
第2~4:位:表示文件属主的权限
第5~7:位:表示文件属组的权限
第8~10:位:表示文件其他的权限

这里的模式可以是权限字母或者是数字,权限对应信息:

  • r:read可读权限 4
  • w:write可写权限 2
  • x:execute可执行权限 1
  • -:没有任何权限

u:文件所属用户
g:文件所属用户组
o:其他用户
a:所有,u+g+o

  • +:增加权限
  • -:减去权限
  • =:设置权限
常用参数
  • -R:递归处理目录下的所有文件
示例
示例文件
[root@localhost test]# ll test_01.txt 
-rw-r--r--. 1 root root 0 May  6 07:22 test_01.txt

1、将权限全部设置为空
[root@localhost test]# chmod a= test_01.txt  #方法1,使用a
[root@localhost test]# ll test_01.txt 
----------. 1 root root 0 May  6 07:22 test_01.txt

[root@localhost test]# chmod 000 test_01.txt  #使用数字
[root@localhost test]# ll test_01.txt 
----------. 1 root root 0 May  6 07:22 test_01.txt


2、设置可执行权限,其他类型w或者r
[root@localhost test]# chmod u+x test_01.txt  #设置属主具有可执行权限
[root@localhost test]# ll test_01.txt 
---x------. 1 root root 0 May  6 07:22 test_01.txt
[root@localhost test]# chmod g+x test_01.txt #设置属组具有可执行权限
[root@localhost test]# ll test_01.txt 
---x--x---. 1 root root 0 May  6 07:22 test_01.txt
[root@localhost test]# chmod o+x test_01.txt  #设置其他用户具有可执行权限
[root@localhost test]# ll test_01.txt 
---x--x--x. 1 root root 0 May  6 07:22 test_01.txt
[root@localhost test]# chmod g-x test_01.txt #去掉属组的可执行权限
[root@localhost test]# ll test_01.txt 
---x-----x. 1 root root 0 May  6 07:22 test_01.txt


3、使用数字来设置
[root@localhost test]# ll test_01.txt 
----------. 1 root root 0 May  6 07:22 test_01.txt
[root@localhost test]# chmod 777 test_01.txt #设置所有都可以读可写可执行
[root@localhost test]# ll test_01.txt 
-rwxrwxrwx. 1 root root 0 May  6 07:22 test_01.txt
[root@localhost test]# chmod 740 test_01.txt  #属主所有权限,属组可读权限,其他没有任何权限
[root@localhost test]# ll test_01.txt 
-rwxr-----. 1 root root 0 May  6 07:22 test_01.txt
[root@localhost test]# chmod 755 test_01.txt #属主所有权限,属组和其他可读可写权限
[root@localhost test]# ll test_01.txt 
-rwxr-xr-x. 1 root root 0 May  6 07:22 test_01.txt

4、-R参数的使用
[root@localhost test]# chmod -R 777 dir1/
[root@localhost test]# ll dir1/
total 0
-rwxrwxrwx. 1 root root 0 May  7 22:34 test1.txt
-rwxrwxrwx. 1 root root 0 May  7 22:34 test2.txt
-rwxrwxrwx. 1 root root 0 May  7 22:34 test3.txt
[root@localhost test]# ll
total 0
drwxrwxrwx. 2 root root 54 May  7 22:34 dir1
普通文件的读写执行权限说明

1、r:表示具有读取/阅读文件内容的权限
2、w:表示具有修改文件内容的权限,注意:如果没有r权限,那么保存文件将会覆盖内容,所以最好使用追加来把内容写入进去。删除文件不受w权限的控制,而是受目录权限的控制。
3、x:具有执行文件的权限,直接执行肯定不行,但是可以通过其他方式来执行

[root@localhost test]# cat test_01.txt 
echo "abc"
[root@localhost test]# ll test_01.txt 
-rw-------. 1 root root 11 May  7 22:45 test_01.txt
[root@localhost test]# ./test_01.txt  #本身不具备执行权限
-bash: ./test_01.txt: Permission denied
[root@localhost test]# . test_01.txt #相当于source
abc
[root@localhost test]# source test_01.txt 
abc
[root@localhost test]# sh test_01.txt 
abc

普通用户必须要r权限才能进行执行
root用户没有r权限,只要有x权限就可以
root用户位没有执行,单其他具有执行权限x,那么它就可以执行

目录的读写执行权限

1、r:浏览目录下的子文件,没有x权限就不能进入到目录下
2、w:新增、删除和修改目录内文件名的权限,必须要x权限配合
3、x:表示具有进入目录的权限,没有r无法显示列表,没有w无法创建文件