每天一个Linux命令(23)chmod命令

    chmod命令用来变更文件或目录的权限。

    在UNIX系统家族里,文件或目录权限的控制分别以读取、写入、执行3种一般权限来区分,另有3种特殊权限可供运用。用户可以使用chmod指令去变更文件与目录的权限,设置方式采用文字或数字代号皆可。符号连接的权限无法变更,如果用户对符号连接修改权限,其改变会作用在被连接的原始文件。

     

    (1)用法:

    用法:  chmod  [选项]     [mode] 模式  文件

         或 chmod  [-cfvR] [--help] [--version] mode file

    命令有两种用法:一种是包含字母和操作符表达式的文字设定法;另一种是包含数字的数字设定法。

    (2)功能:

    功能:用于改变文件或目录的访问权限,用它控制文件或目录的访问权限。

    (3)参数详解:

      1) -R    ——recursive:              递归处理,将指令目录下的所有文件及子目录一并处理

      2) -v    ——verbose :               显示指令执行过程

      3) --reference=<参考文件或目录>:          把指定文件或目录的所属群组全部设成和参考文件或目录的所属群组相同

      4) <权限范围>+<权限设置>:           开启权限范围的文件或目录的该选项权限设置

          <权限范围> -<权限设置>:                               关闭权限范围的文件或目录的该选项权限设置

          <权限范围>=<权限设置>:                               指定权限范围的文件或目录的该选项权限设置

    (4)权限范围的表示法:
      1) u         User    即文件或目录的拥有者

      2) g         Group     即文件或目录的所属群组

      3) o         Other         除了文件或目录拥有者或所属群组之外,其他用户皆属于这个范围

      4) a         All      即全部的用户,包含拥有者,所属群组以及其他用户

      5) r                          读取权限,数字代号为“4”

      6) w           写入权限,数字代号为“2”

      7) x                          执行或切换权限,数字代号为“1”

      8) -                          不具任何权限,数字代号为“0”

      9) s                          特殊功能说明:变更文件或目录的权限

    (5)实例:

      1)[root@localhost Documents]# chmod a+x core.log          为所有用户组添加可执行权限

[root@localhost Documents]# ll -al core.log         //ll -al 文件名与 ls -l 文件名貌似没啥区别
-rw-r--r--. 1 root root 27 5月  19 04:21 core.log
[root@localhost Documents]# ls -l core.log
-rw-r--r--. 1 root root 27 5月  19 04:21 core.log
[root@localhost Documents]# chmod a+x core.log     
[root@localhost Documents]# ll -al core.log
-rwxr-xr-x. 1 root root 27 5月  19 04:21 core.log

      2)[root@localhost Documents]# chmod ug+w,o-x core.log       同时为不同用户添加或删除权限

[root@localhost Documents]# ll -al core.log
-rwxr-xr-x. 1 root root 27 5月  19 04:21 core.log
[root@localhost Documents]# chmod ug+w,o-x core.log
[root@localhost Documents]# ll -l core.log
-rwxrwxr--. 1 root root 27 5月  19 04:21 core.log

      3)[root@localhost Documents]# chmod g=x,o=rwx core.log        设置文件的u,g,o的三个权限。这里是设置文件所有者权限为可执行,其他用户可以读写执行,组用户权限不变

[root@localhost Documents]# ll -l core.log
-rwxrwxr--. 1 root root 27 5月  19 04:21 core.log
[root@localhost Documents]# chmod g=x,o=rwx core.log
[root@localhost Documents]# ls -l core.log
-rwx--xrwx. 1 root root 27 5月  19 04:21 core.log

      4)[root@localhost Documents]# chmod -R o=--- findDir      递归的设置findDir文件夹下的文件及文件夹的其他用户权限为不具备任何权限

[root@localhost Documents]# chmod -R o=--- findDir
[root@localhost Documents]# find . -name "Dir" -print
./Dir
[root@localhost Documents]# find . -name "Dir" -exec ls -l {} ;
总用量 12
--w-------. 1 root root 664 5月   9 07:59 head_text
--w-------. 1 root root  45 5月   9 08:15 less1
--w-------. 1 root root  57 5月   9 08:16 less2
[root@localhost Documents]# ls -l Dir
总用量 12
--w-------. 1 root root 664 5月   9 07:59 head_text
--w-------. 1 root root  45 5月   9 08:15 less1
--w-------. 1 root root  57 5月   9 08:16 less2

      5)[root@localhost Documents]# chmod -R u=r,g=r,o=r findDir 与  [root@localhost Documents]# chmod -R =rx Dir    等价地给ugo的用户设置权限

[root@localhost Documents]# chmod -R u=r,g=r,o=r findDir
[root@localhost Documents]# ls -l findDir
总用量 0
-r--r--r--. 1 root root 0 5月  17 04:18 p1.pdf
-r--r--r--. 1 root root 0 5月  17 04:18 p2.pdf
-r--r--r--. 1 root root 0 5月  17 03:50 t1.txt
-r--r--r--. 1 root root 0 5月  17 04:02 T1.txt
-r--r--r--. 1 root root 0 5月  19 04:58 t2.txt
-r--r--r--. 1 root root 0 5月  17 04:02 T2.txt
[root@localhost Documents]# ls -l Dir
总用量 12
--w-------. 1 root root 664 5月   9 07:59 head_text
--w-------. 1 root root  45 5月   9 08:15 less1
--w-------. 1 root root  57 5月   9 08:16 less2
[root@localhost Documents]# chmod -R =rx Dir           //设置所有组用户权限为rw之后,以前的所有者的w权限就没了
[root@localhost Documents]# ls -l Dir
总用量 12
-r-xr-xr-x. 1 root root 664 5月   9 07:59 head_text
-r-xr-xr-x. 1 root root  45 5月   9 08:15 less1
-r-xr-xr-x. 1 root root  57 5月   9 08:16 less2

      6)[root@localhost Documents]# chmod 444 find        用数字给文件设置权限

[root@localhost Documents]# chmod 444 find
[root@localhost Documents]# ls -l find
-r--r--r--. 1 root root 0 5月  19 04:16 find
[root@localhost Documents]# chmod =r newlocate                //两种方法等价,但同时都会将原有的权限清除
[root@localhost Documents]# ls -l newlocate
-r--r--r--. 1 root root 0 5月  15 18:21 newlocate

    (6)其他:

      Linux用户分为:拥有者、组群(Group)、其他(other)。

      Linux系统中,预设的情況下,系统中所有的帐号与一般身份使用者,以及root的相关信息, 都是记录在/etc/passwd文件中。每个人的密码则是记录在/etc/shadow文件下。 此外,所有的组群名称记录在/etc/group內!

原文地址:https://www.cnblogs.com/MenAngel/p/5515077.html