locate与find查找

locate:/var/lib/mlocate/mlocate.db
getfacl 目录

chmod权限管理

  • chmod(英文全拼:change mode)设置用户对文件的权限
  • 命令格式:chmod [-选项] 归属关系+-=权限类别 文件…
  • root用户可以修改任何文件和目录的权限
  • 文件所有者
  • 常用选项:
    • -R 递归修改,包含目录下所有的子文件与子目录
  • 归属关系:u 所有者 g 所属组 o 其他人
  • 权限类别: r 读取 w 写入 x 执行 - 没有权限
  • 操作:+ 添加权限 - 去除权限 = 重新定义权限
  • 权限数字表示:r —— 4 w —— 2 x —— 1 0 没有权限
  1. #查看文件详细属性
  2. [root@localhost ~]# ll hello
  3. -rw-r--r--. 1 root root 426 3 28 15:00 hello
  4. #为文件所有者添加执行权限
  5. [root@localhost ~]# chmod u+x hello
  6. [root@localhost ~]# ll hello
  7. -rwxr--r--. 1 root root 426 3 28 15:00 hello
  8. #为文件所属组添加写权限
  9. [root@localhost ~]# chmod g+w hello
  10. [root@localhost ~]# ll hello
  11. -rwxrw-r--. 1 root root 426 3 28 15:00 hello
  12. #为文件其他人添加写权限
  13. [root@localhost ~]# chmod o+w hello
  14. [root@localhost ~]# ll hello
  15. -rwxrw-rw-. 1 root root 426 3 28 15:00 hello
  16. #使用(逗号)可以同时为多个用户授权
  17. [root@localhost ~]# chmod g+x,o+x hello
  18. [root@localhost ~]# ll hello
  19. -rwxrwxrwx. 1 root root 426 3 28 15:00 hello
  20. #去除所有者执行权限
  21. [root@localhost ~]# chmod u-x hello
  22. [root@localhost ~]# ll hello
  23. -rw-rwxrwx. 1 root root 426 3 28 15:00 hello
  24. #去除所属组执行权限
  25. [root@localhost ~]# chmod g-x hello
  26. [root@localhost ~]# ll hello
  27. -rw-rw-rwx. 1 root root 426 3 28 15:00 hello
  28. #去除其他人执行权限
  29. [root@localhost ~]# chmod o-x hello
  30. [root@localhost ~]# ll hello
  31. -rw-rw-rw-. 1 root root 426 3 28 15:00 hello
  32. #同时去除ugo写权限
  33. [root@localhost ~]# chmod u-w,g-w,o-w hello
  34. [root@localhost ~]# ll hello
  35. -r--r--r--. 1 root root 426 3 28 15:00 hello
  36. #重新定义所有者权限
  37. [root@localhost ~]# chmod u=rwx hello
  38. [root@localhost ~]# ll hello
  39. -rwxr--r--. 1 root root 426 3 28 15:00 hello
  40. #重新定义所属组权限
  41. [root@localhost ~]# chmod g=rwx hello
  42. [root@localhost ~]# ll hello
  43. -rwxrwxr--. 1 root root 426 3 28 15:00 hello
  44. #重新定义其他人权限
  45. [root@localhost ~]# chmod o=rwx hello
  46. [root@localhost ~]# ll hello
  47. -rwxrwxrwx. 1 root root 426 3 28 15:00 hello
  48. #创建目录并设置目录权限
  49. [root@localhost ~]# mkdir /test
  50. [root@localhost ~]# ll -d /test
  51. drwxr-xr-x. 2 root root 6 4 11 14:30 /test
  52. #为目录所属组添加写权限
  53. [root@localhost ~]# chmod g+w /test
  54. [root@localhost ~]# ll -d /test
  55. drwxrwxr-x. 2 root root 6 4 11 14:30 /test
  56. #为目录其他人添加写权限
  57. [root@localhost ~]# chmod o+w /test
  58. [root@localhost ~]# ll -d /test
  59. drwxrwxrwx. 2 root root 6 4 11 14:30 /test
  60. [root@localhost ~]#
  61. #重新定义所有用户权限
  62. [root@localhost ~]# chmod u=rwx,g=rx,o=rx /test
  63. [root@localhost ~]# ll -d /test
  64. drwxr-xr-x. 2 root root 6 4 11 14:30 /test
  65. #同时为所有用户定义相同权限
  66. [root@localhost ~]# chmod ugo=rwx /test
  67. [root@localhost ~]# ll -d /test
  68. drwxrwxrwx. 2 root root 21 4 11 14:37 /test
  69. #权限数字定义方式
  70. [root@localhost ~]# ll hello
  71. -rwxrwxrwx. 1 root root 426 3 28 15:00 hello
  72. 所有者:rwx 4+2+1=7
  73. 所属组:r 4
  74. 其他人:r 4
  75. [root@localhost ~]# chmod 744 hello
  76. [root@localhost ~]# ll hello
  77. -rwxr--r--. 1 root root 426 3 28 15:00 hello
  78. 所有者:rw 4+2=6
  79. 所属组:rw 4+2=6
  80. 其他人:--- 0
  81. [root@localhost ~]# chmod 660 hello
  82. [root@localhost ~]# ll hello
  83. -rw-rw----. 1 root root 426 3 28 15:00 hello
  84. 所有者:rwx 4+2+1=7
  85. 所属组:wx 2+1=3
  86. 其他人:--- 0
  87. [root@localhost ~]# touch /hello.txt
  88. [root@localhost ~]# ll /hello.txt
  89. -rw-r--r--. 1 root root 0 4 11 14:45 /hello.txt
  90. [root@localhost ~]# chmod 730 /hello.txt
  91. [root@localhost ~]# ll /hello.txt
  92. -rwx-wx---. 1 root root 0 4 11 14:45 /hello.txt
  93. #去除所有用户权限
  94. [root@localhost ~]# chmod 000 /hello.txt
  95. [root@localhost ~]# ll /hello.txt
  96. ----------. 1 root student 0 4 11 14:45 /hello.txt
  97. #递归修改目录下所有子文件与子目录权限
  98. [root@localhost ~]# ll -d /test
  99. drwxrwxrwx. 2 root root 21 4 11 14:37 /test
  100. [root@localhost ~]# mkdir /test/xxoo
  101. [root@localhost ~]# ll -d /test/xxoo/
  102. drwxr-xr-x. 2 root root 6 4 11 14:54 /test/xxoo/
  103. [root@localhost ~]# ll /test/abc.txt
  104. -rw-r--r--. 1 root root 0 4 11 14:37 /test/abc.txt
  105. #默认用户在该目录下创建文件权限与父目录不一致
  106. #递归修改目录下所有子文件与子目录权限
  107. [root@localhost ~]# chmod -R 777 /test
  108. [root@localhost ~]# ll /test/abc.txt
  109. -rwxrwxrwx. 1 root root 0 4 11 14:37 /test/abc.txt
  110. [root@localhost ~]# ll -d /test/xxoo
  111. drwxrwxrwx. 2 root root 6 4 11 14:54 /test/xxoo
  112. #深入理解权限,
  113. [root@localhost ~]# mkdir /test1
  114. [root@localhost ~]# chmod 777 /test1
  115. [root@localhost ~]# ll -d /test1
  116. drwxrwxrwx. 2 root root 6 4 11 14:57 /test1
  117. #在该目录下创建文件与目录
  118. [root@localhost ~]# touch /test1/root.txt
  119. [root@localhost ~]# mkdir /test1/rootbak
  120. [root@localhost ~]# chmod o=rx /test1
  121. [root@localhost ~]# ll -d /test1
  122. drwxrwxr-x. 2 root root 6 4 11 14:59 /test1
  123. [root@localhost ~]# touch /test1/root.txt
  124. #普通用户对该目录如果拥有rwx权限是可以删除该目录下任何用户创建的文件(包括root)
  125. [user1@localhost ~]$ cd /test1
  126. [user1@localhost test1]$ ls
  127. root.txt
  128. [user1@localhost test1]$ ll root.txt
  129. -rw-r--r--. 1 root root 0 4 11 14:57 root.txt
  130. [user1@localhost test1]$ rm -rf root.txt
  131. [user1@localhost test1]$ ls
  132. rootbak
  133. [user1@localhost test1]$ rm -rf rootbak/
  134. [user1@localhost test1]$ ls
  135. [user1@localhost test1]$ ll -d /test1
  136. drwxrwxrwx. 2 root root 6 4 11 14:59 /test1
  137. 总结:
  138. 1.用户对文件拥有写权限可以增加/修改/删除文件里内容,并不能删除文件,删除文件取决于对文件的父目录有没有rwx权限
  139. 2.用户对目录拥有rwx权限可以查看/创建/修改/删除目录下的文件

umask预设权限

  • umask用于显示或设置创建文件的权限掩码
  • 命令格式:umask [-p] [-S] [mode]
  1. root@localhost ~]# mkdir /test2
  2. [root@localhost ~]# ll -d /test2
  3. drwxr-xr-x. 2 root root 6 4 11 15:05 /test2
  4. [root@localhost ~]# umask --help
  5. umask: 用法:umask [-p] [-S] [模式]
  6. #查看目录默认权限掩码,以数字形式显示
  7. [root@localhost ~]# umask -p
  8. umask 0022
  9. #查看目录默认权限掩码,以字母形式显示
  10. [root@localhost ~]# umask -S
  11. u=rwx,g=rx,o=rx
  12. #设置目录默认权限掩码,为所属组添加写权限
  13. [root@localhost ~]# umask g+w
  14. [root@localhost ~]# mkdir /test3
  15. [root@localhost ~]# ll -d /test3
  16. drwxrwxr-x. 2 root root 6 4 11 15:09 /test3
  17. #去除目录默认权限掩码
  18. [root@localhost ~]# umask g-w
  19. [root@localhost ~]# mkdir /test4
  20. [root@localhost ~]# ll -d /test4
  21. drwxr-xr-x. 2 root root 6 4 11 15:10 /test4

chown归属关系管理

  • chown(英文全拼:change owner)用于设置文件的所有者和所属组关系
  • 命令格式:
    • chown [-选项] 所有者:所属组 文档 #同时修改所有者和所属组身份
    • chown [-选项] 所有者 文档 #只修改所有者身份
    • chown [-选项] :所属组 文档 #只修改所属组身份
  • 常用选项:
    • -R 递归修改
  1. #创建文件
  2. [root@localhost ~]# chmod 744 /hello.txt
  3. [root@localhost ~]# ll /hello.txt
  4. -rwxr--r--. 1 root student 0 4 11 14:45 /hello.txt
  5. #修改文件所有者为user1用户
  6. [root@localhost ~]# chown user1 /hello.txt
  7. [root@localhost ~]# ll /hello.txt
  8. -rwxr--r--. 1 user1 student 0 4 11 14:45 /hello.txt
  9. #修改文件所有者与所属组为lisi
  10. [root@localhost ~]# chown lisi:lisi /hello.txt
  11. [root@localhost ~]# ll /hello.txt
  12. -rwxr--r--. 1 lisi lisi 4 4 11 15:26 /hello.txt
  13. #创建目录
  14. [root@localhost ~]# mkdir /test5
  15. [root@localhost ~]# ll -d /test5
  16. drwxr-xr-x. 2 root root 6 4 11 15:30 /test5
  17. #修改目录所有者与所属组为lisi
  18. [root@localhost ~]# chown lisi:lisi /test5
  19. [root@localhost ~]# ll -d /test5
  20. drwxr-xr-x. 2 lisi lisi 6 4 11 15:30 /test5
  21. [root@localhost ~]# touch /test5/root.txt
  22. [root@localhost ~]# ll /test5/root.txt
  23. -rw-r--r--. 1 root root 0 4 11 15:31 /test5/root.txt
  24. #递归修目录下所有子文件与子目录归属关系
  25. [root@localhost ~]# chown -R lisi:lisi /test5
  26. [root@localhost ~]# ll /test5/root.txt
  27. -rw-r--r--. 1 lisi lisi 0 4 11 15:31 /test5/root.txt

SetUID特殊权限

  • SetUID(SUID):对于一个可执行的文件用了SUID权限后,普通用户在执行该文件后,临时拥有文件所有者的身份,该权限只在程序执行过程中有效,程序执行完毕后用户恢复原有身份
  • SetUID权限会附加在所有者的 x 权限位上,所有者的 x 权限标识会变成 s
  • 设置SetUID命令格式:chmod u+s 文件名
  1. #搜索命令绝对路径
  2. [root@localhost ~]# which passwd
  3. /usr/bin/passwd
  4. [root@localhost ~]# ll /usr/bin/passwd
  5. -rwsr-xr-x. 1 root root 27832 6 10 2014 /usr/bin/passwd
  6. [root@localhost ~]# which cat
  7. /usr/bin/cat
  8. [root@localhost ~]# ll /usr/bin/cat
  9. -rwxr-xr-x. 1 root root 54160 10 31 2018 /usr/bin/cat
  10. #普通用户使用cat命令是默认无法查看/etc/shadow文件内容
  11. [lisi@localhost ~]$ cat /etc/shadow
  12. cat: /etc/shadow: 权限不够
  13. #设置SUID权限
  14. [root@localhost ~]# chmod u+s /usr/bin/cat
  15. [root@localhost ~]# ll /usr/bin/cat
  16. -rwsr-xr-x. 1 root root 54160 10 31 2018 /usr/bin/cat
  17. #普通用户再次使用cat命令时临时获取文件所有者身份
  18. [lisi@localhost ~]$ cat /etc/shadow
  19. #去除SUID权限
  20. [root@localhost ~]# chmod u-s /usr/bin/cat
  21. [root@localhost ~]# ll /usr/bin/cat
  22. -rwxr-xr-x. 1 root root 54160 10 31 2018 /usr/bin/cat
  23. [root@localhost ~]# which vim
  24. /usr/bin/vim
  25. [root@localhost ~]# ll /usr/bin/vim
  26. -rwxr-xr-x. 1 root root 2294208 10 31 2018 /usr/bin/vim
  27. #为vim设置SUID权限
  28. [root@localhost ~]# chmod u+s /usr/bin/vim
  29. [root@localhost ~]# ll /usr/bin/vim
  30. -rwsr-xr-x. 1 root root 2294208 10 31 2018 /usr/bin/vim
  31. [root@localhost ~]# ll /etc/passwd
  32. -rw-r--r--. 1 root root 2737 4 10 17:26 /etc/passwd
  33. [root@localhost ~]# chmod u-s /usr/bin/vim
  34. [root@localhost ~]# vim /etc/passwd

SetGID特殊权限

  • SetGID(SGID):当对一个可执行的二进制文件设置了SGID后,普通用户在执行该文件时临时拥有其所属组的权限,该权限只在程序执行过程中有效,程序执行完毕后用户恢复原有组身份
  • 当对一个目录作设置了SGID权限后,普通用户在该目录下创建的文件的所属组,均与该目录的所属组相同
  • SetGID权限会附加在所属组的 x 权限位上,所属组的 x 权限标识会变成 s
  • 设置SetGID命令格式:chmod g+s 文件名
  1. [root@localhost ~]# mkdir /test6
  2. [root@localhost ~]# chmod 777 /test6
  3. [root@localhost ~]# ll -d /test6
  4. drwxrwxrwx. 2 root root 6 4 11 15:59 /test6
  5. #为目录设置SGID权限
  6. [root@localhost ~]# chmod g+s /test6
  7. [root@localhost ~]# ll -d /test6
  8. drwxrwsrwx. 2 root root 6 4 11 15:59 /test6
  9. #SGID权限会附加在所属组执行权限位,所属组执行权限变为s
  10. [root@localhost ~]# touch /test6/1.txt
  11. [root@localhost ~]# ll /test6/1.txt
  12. -rw-r--r--. 1 root root 0 4 11 16:00 /test6/1.txt
  13. #修改目录所属组为lisi组
  14. [root@localhost ~]# chown :lisi /test6
  15. [root@localhost ~]# ll -d /test6
  16. drwxrwsrwx. 2 root lisi 19 4 11 16:00 /test6
  17. #SGID对目录设置后,在该目录下创建的任何文件都会继承父目录的所属组
  18. [root@localhost ~]# touch /test6/2.txt
  19. [root@localhost ~]# ll /test6/2.txt
  20. -rw-r--r--. 1 root lisi 0 4 11 16:01 /test6/2.txt

Sticky BIT特殊权限

  • Sticky BIT(SBIT):该权限只针对于目录有效,当普通用户对一个目录拥有w和x权限时,普通用户可以在此目录下拥有增删改的权限,应为普通用户对目录拥有w权限时,是可以删除此目录下的所有文件
  • 如果对一个目录设置了SBIT权限,除了root可以删除所有文件以外,普通用户就算对该目录拥有w权限,也只能删除自己建立的文件,不能删除其他用户建立的文件
  • SBIT权限会附加在其他人的 x 权限位上,其他人的 x 权限标识会变成 t
  • 设置SBIT命令格式:chmod o+t 目录名
  1. #为目录设置SBIT
  2. [root@localhost ~]# chmod o+t /test
  3. [root@localhost ~]# ll -d /test
  4. drwxrwxrwt. 2 root root 6 4 11 16:07 /test
  5. [lisi@localhost test]$ ls
  6. kenji.txt laowang.txt lisi.txt
  7. [lisi@localhost test]$ rm -rf *
  8. rm: 无法删除"kenji.txt": 不允许的操作
  9. rm: 无法删除"laowang.txt": 不允许的操作

FACL访问控制列表

  • FACL(Filesystemctl Access Control List)文件系统访问控制列表:利用文件扩展属性保存额外的访问控制权限,单独为每一个用户量身定制一个权限
  • 命令格式:setfacl 选项 归属关系:权限 文档
  • 常用选项:
    • -m 设置权限
    • -x 删除指定用户权限
    • -b 删除所有用户权限
  1. #为natasha用户设置ACL权限
  2. [root@localhost ~]# setfacl -m u:natasha:rx /yunwei/
  3. [root@localhost ~]# ll -d /yunwei/
  4. drwxrwx---+ 2 root yunwei 54 4 11 16:43 /yunwei/
  5. [root@localhost ~]# ll -d /test
  6. drwxrwxrwt. 2 root root 42 4 11 16:11 /test
  7. #查看目录ACL权限
  8. [root@localhost ~]# getfacl /yunwei
  9. getfacl: Removing leading '/' from absolute path names
  10. # file: yunwei
  11. # owner: root
  12. # group: yunwei
  13. user::rwx
  14. user:natasha:r-x
  15. group::rwx
  16. mask::rwx
  17. other::---
  18. #用户测试权限
  19. [natasha@localhost ~]$ ls /yunwei/
  20. hell.sh kenji.txt lisi.txt
  21. [natasha@localhost yunwei]$ rm -rf kenji.txt
  22. rm: 无法删除"kenji.txt": 权限不够
  23. [natasha@localhost yunwei]$ touch natasha.txt
  24. touch: 无法创建"natasha.txt": 权限不够
  25. [natasha@localhost yunwei]$ vim kenji.txt
  26. [root@localhost ~]# setfacl -m u:tom:rx /yunwei
  27. [root@localhost ~]# setfacl -m u:jack:rx /yunwei
  28. [root@localhost ~]# setfacl -m u:hary:rx /yunwei
  29. [root@localhost ~]# getfacl /yunwei
  30. getfacl: Removing leading '/' from absolute path names
  31. # file: yunwei
  32. # owner: root
  33. # group: yunwei
  34. user::rwx
  35. user:hary:r-x
  36. user:tom:r-x
  37. user:natasha:r-x
  38. user:jack:r-x
  39. group::rwx
  40. mask::rwx
  41. other::---
  42. #删除指定用户ACL权限
  43. [root@localhost ~]# setfacl -x u:tom /yunwei
  44. [root@localhost ~]# getfacl /yunwei
  45. getfacl: Removing leading '/' from absolute path names
  46. # file: yunwei
  47. # owner: root
  48. # group: yunwei
  49. user::rwx
  50. user:hary:r-x
  51. user:natasha:r-x
  52. user:jack:r-x
  53. group::rwx
  54. mask::rwx
  55. other::---
  56. #删除所有用户ACL权限
  57. [root@localhost ~]# setfacl -b /yunwei
  58. [root@localhost ~]# getfacl /yunwei
  59. getfacl: Removing leading '/' from absolute path names
  60. # file: yunwei
  61. # owner: root
  62. # group: yunwei
  63. user::rwx
  64. group::rwx
  65. other::---