说明

  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:递归处理目录下的所有文件
示例
  1. 示例文件
  2. [root@localhost test]# ll test_01.txt
  3. -rw-r--r--. 1 root root 0 May 6 07:22 test_01.txt
  4. 1、将权限全部设置为空
  5. [root@localhost test]# chmod a= test_01.txt #方法1,使用a
  6. [root@localhost test]# ll test_01.txt
  7. ----------. 1 root root 0 May 6 07:22 test_01.txt
  8. [root@localhost test]# chmod 000 test_01.txt #使用数字
  9. [root@localhost test]# ll test_01.txt
  10. ----------. 1 root root 0 May 6 07:22 test_01.txt
  11. 2、设置可执行权限,其他类型w或者r
  12. [root@localhost test]# chmod u+x test_01.txt #设置属主具有可执行权限
  13. [root@localhost test]# ll test_01.txt
  14. ---x------. 1 root root 0 May 6 07:22 test_01.txt
  15. [root@localhost test]# chmod g+x test_01.txt #设置属组具有可执行权限
  16. [root@localhost test]# ll test_01.txt
  17. ---x--x---. 1 root root 0 May 6 07:22 test_01.txt
  18. [root@localhost test]# chmod o+x test_01.txt #设置其他用户具有可执行权限
  19. [root@localhost test]# ll test_01.txt
  20. ---x--x--x. 1 root root 0 May 6 07:22 test_01.txt
  21. [root@localhost test]# chmod g-x test_01.txt #去掉属组的可执行权限
  22. [root@localhost test]# ll test_01.txt
  23. ---x-----x. 1 root root 0 May 6 07:22 test_01.txt
  24. 3、使用数字来设置
  25. [root@localhost test]# ll test_01.txt
  26. ----------. 1 root root 0 May 6 07:22 test_01.txt
  27. [root@localhost test]# chmod 777 test_01.txt #设置所有都可以读可写可执行
  28. [root@localhost test]# ll test_01.txt
  29. -rwxrwxrwx. 1 root root 0 May 6 07:22 test_01.txt
  30. [root@localhost test]# chmod 740 test_01.txt #属主所有权限,属组可读权限,其他没有任何权限
  31. [root@localhost test]# ll test_01.txt
  32. -rwxr-----. 1 root root 0 May 6 07:22 test_01.txt
  33. [root@localhost test]# chmod 755 test_01.txt #属主所有权限,属组和其他可读可写权限
  34. [root@localhost test]# ll test_01.txt
  35. -rwxr-xr-x. 1 root root 0 May 6 07:22 test_01.txt
  36. 4、-R参数的使用
  37. [root@localhost test]# chmod -R 777 dir1/
  38. [root@localhost test]# ll dir1/
  39. total 0
  40. -rwxrwxrwx. 1 root root 0 May 7 22:34 test1.txt
  41. -rwxrwxrwx. 1 root root 0 May 7 22:34 test2.txt
  42. -rwxrwxrwx. 1 root root 0 May 7 22:34 test3.txt
  43. [root@localhost test]# ll
  44. total 0
  45. drwxrwxrwx. 2 root root 54 May 7 22:34 dir1
普通文件的读写执行权限说明

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

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

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

目录的读写执行权限

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