访问权限:

  • 读取 r : 允许查看文件内容,显示目录列表;
  • 写入 w : 允许修改文件内容,允许在目录中新建、移动、删除文件或子目录;
  • 可执行 x :允许运行程序、切换目录;

归属(所有权):

  • 属主 : 拥有该文件或目录的用户帐号;
  • 属组 : 拥有该文件或目录的组帐号;

一、权限管理命令:chmod

centos sh文件授权 linux授权文件夹命令_递归


关于以上命令的说明:

  • 只有文件所有者管理员root才可以更改文件的权限;
  • u表示所有者(user);g表示所属组(group);o表示其他人(other);a便是所有人(all)。
  • + 表示增加权限;- 表示减权限;= 表示指定权限,不管以前什么权限 。
  • 通过逗号,可以同时做多个授权;
  • -R选项,更改会将这个目录以及该目录下所有文件的权限都会进行修改;
  • 示例(1)如下:
[root@root tmp]# touch test.txt
[root@root tmp]# ls
keyring-MP8AWS      test.txt             vmware-root_1577-2092775956
keyring-S5dwFE      virtual-root.JaBNIV  vmware-root_1589-2083797903
orbit-gdm           virtual-root.u5jKQr  vmware-user1
[root@root tmp]# ls -l test.txt
-rw-r--r--. 1 root root 0 7月  27 18:08 test.txt
[root@root tmp]# chmod u+x test.txt 
[root@root tmp]# ls -l test.txt
-rwxr--r--. 1 root root 0 7月  27 18:08 test.txt
#可以发现test.txt文件已经由x控制权限了,
[root@root tmp]# chmod g+w,o-r test.txt 
[root@root tmp]# ls -l test.txt
-rwxrw----. 1 root root 0 7月  27 18:08 test.txt
#通过逗号,可以同时做多个授权。
[root@root tmp]# chmod g=rwx test.txt 
[root@root tmp]# ls -l test.txt
-rwxrwx---. 1 root root 0 7月  27 18:08 test.txt
#通过等号=,就可以指定特定权限。
1、数字表示权限的方式

权限的数字表示:

  • r -------- 4
  • w -------- 2
  • x -------- 1

例如:

centos sh文件授权 linux授权文件夹命令_子目录_02

范例:

$ chmod g+w testfile
#赋予文件testfile所属组写权限

$ chmod -R 777 testdir
#修改目录testfile及其目录下文件为所有用户具有全部权限

  • 示例(2)如下:通过数字的方式来修改文件的权限
[root@root tmp]# ls -l test.txt 
-rwxrwx---. 1 root root 0 7月  27 18:08 test.txt
[root@root tmp]# chmod 777 test.txt 
[root@root tmp]# ls -l test.txt 
-rwxrwxrwx. 1 root root 0 7月  27 18:08 test.txt
#通过数字的方式来修改文件的权限
  • 示例(3)如下:***-R递归修改***
[root@root tmp]# mkdir -p /tmp/a/b
[root@root tmp]# ls -ld /tmp/a
drwxr-xr-x. 3 root root 4096 7月  27 18:43 /tmp/a
[root@root tmp]# ls -ld /tmp/a/b
drwxr-xr-x. 2 root root 4096 7月  27 18:43 /tmp/a/b
[root@root tmp]# chmod 777 /tmp/a
[root@root tmp]# ls -ld /tmp/a
drwxrwxrwx. 3 root root 4096 7月  27 18:43 /tmp/a
[root@root tmp]# ls -ld /tmp/a/b
drwxr-xr-x. 2 root root 4096 7月  27 18:43 /tmp/a/b
#虽然修改了a目录的权限,但是a目录下b目录的权限并没有更改
[root@root tmp]# chmod -R 777 /tmp/a
[root@root tmp]# ls -ld /tmp/a/b
drwxrwxrwx. 2 root root 4096 7月  27 18:43 /tmp/a/b
#加上-R递归选项后,会更改a目录即a目录下所有的文件和目录的权限
1.1> 文件目录权限总结

centos sh文件授权 linux授权文件夹命令_centos sh文件授权_03


关于以上的说明:

  • 删除一个文件的前提条件是对这个文件所在的目录有写权限,才可以把这个文件删除掉。对一个文件有写权限,只能修改文件的内容,而不能删除该文件;
  • 对目录的权限,如果查看目录下的文件有哪些和信息不许有 r 权限,如果要进入这个目录必须有 x 权限,所以在Linux当中目录的r权限和x权限都是成对出现的;
  • 删除一个文件

以上权限可以执行的命令

file(文件):

  • r : cat、more、head、tail、less等等;
  • w : vim
  • x : script、command

directory(目录):

  • r : ls
  • w : touch、mkdir、rmdir、rm等等
  • x : cd命令

示例如下:

centos sh文件授权 linux授权文件夹命令_centos sh文件授权_04

二、设置文件和目录的归属命令:chown

格式:

  • chown 属主 文件或目录
  • chown :属组 文件或目录
  • chown 属主:属组 文件或目录

常用命令选项:

  • -R : 对目录及该目录下的所有文件和子目录都进行相同的操作,即递归更改指定目录下所有文件、子目录的归属;

示例说明:

在这里插入代码片