日期:2014-05-16  浏览次数:20700 次

Linux chmod 命令 详解
Linux chmod 命令 详解
      chmod 命令用的很多,但是对这个命令却不是很了解。 在网上搜了一下。 整理如下。



命令格式:

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

[root@qs-wg-db2 ~]# chmod --help

Usage: chmod [OPTION]... MODE[,MODE]... FILE...

  or:  chmod [OPTION]... OCTAL-MODE FILE...

  or:  chmod [OPTION]... --reference=RFILE FILE...

Change the mode of each FILE to MODE.



  -c, --changes           like verbose but report only when a change is made

      --no-preserve-root  do not treat `/' specially (the default)

      --preserve-root     fail to operate recursively on `/'

  -f, --silent, --quiet   suppress most error messages

  -v, --verbose           output a diagnostic for every file processed

      --reference=RFILE   use RFILE's mode instead of MODE values

  -R, --recursive         change files and directories recursively

      --help     display this help and exit

      --version  output version information and exit



Each MODE is of the form `[ugoa]*([-+=]([rwxXst]*|[ugo]))+'.



Report bugs to <bug-coreutils@gnu.org>.

[root@qs-wg-db2 ~]#





参数说明:

1. -cfvR 部分

       -c : 若该档案权限确实已经更改,才显示其更改动作

       -f : 若该档案权限无法被更改也不要显示错误讯息

       -v : 显示权限变更的详细资料

       -R : 对目前目录下的所有档案与子目录进行相同的权限变更(即以递回的方式逐个变更), 这个-R 用的还是很多的。



2.  Mode 部分

这部分可以分成如下3块: [who] operator [permission]

                                                 `[ugoa]*([-+=]([rwxXst]*|[ugo]))+'.



who的含义是:

       u 文件属主权限

       g 同组用户权限

       o 其它用户权限

       a 所有用户(包括以上三种)

operator的含义:

       + 增加权限

       - 取消权限

       =  唯一设定权限

permission的含义:

       r 读权限

       w 写权限

       x 执行权限

       X 表示只有当该档案是个子目录或者该档案已经被设定过为可执行。

       s 文件属主和组id

       l 给文件加锁,使其它用户无法访问



如: chmod a+x



3. 示例:

(1)将档案 file1.txt 设为所有人皆可读取 :

       chmod ugo+r file1.txt

(2)将档案 file1.txt 设为所有人皆可读取 :

       chmod a+r file1.txt
(3)将档案 file1.txt 与 file2.txt 设为该档案拥有者,与其所属同一个群体者可写入,但其他以外的人则不可写入 :

       chmod ug+w,o-w file1.txt file2.txt

(4)将 ex1.py 设定为只有该档案拥有者可以执行 :

       chmod u+x ex1.py

(5)将目前目录下的所