Linux shell chmod 命令

返回上一级

Linux shell chmod 命令可以藉以控制文件如何被他人所调用。

Linux/Unix 的文件调用权限分为三级 : 文件拥有者、群组、其他

使用权限

所有使用者

chmod 命令语法格式

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

参数

mode

权限设定字串,格式如下

 [ugoa...][[+-=][rwxX]...][,...]

其中:

  • u 表示该文件的拥有者,g 表示与该文件的拥有者属于同一个群体(group)者,o 表示其他以外的人,a 表示这三者皆是。

  • + 表示增加权限、- 表示取消权限、= 表示唯一设定权限。

  • r 表示可读取,w 表示可写入,x 表示可执行,X 表示只有当该文件是个子目录或者该文件已经被设定过为可执行。

其它参数说明

  • -c : 若该文件权限确实已经更改,才显示其更改动作
  • -f : 若该文件权限无法被更改也不要显示错误讯息
  • -v : 显示权限变更的详细资料
  • -R : 对目前目录下的所有文件与子目录进行相同的权限变更(即以递回的方式逐个变更)
  • --help : 显示辅助说明
  • --version : 显示版本

范例

将文件 demo1.txt 设为所有人皆可读取 :

chmod ugo+r demo1.txt

示例

[root@localhost ~]# ll
total 72
drwxr-xr-x. 2 root root     6 Sep 22 05:16 abc
-rw-------. 1 root root  1233 Sep 20 17:18 anaconda-ks.cfg
-rw-r--r--. 1 root root   607 Sep 22 11:55 demo1.txt
-rw-r--r--. 1 root root   656 Sep 22 17:02 demo2.txt
-rw-r--r--. 1 root root  1361 Sep 22 17:06 demo3.txt
-rw-r--r--. 1 root root   612 Sep 22 15:02 demo.txt
-rw-r--r--. 1 root root 33903 Sep 22 10:18 man-help.txt
-rw-r--r--. 1 root root     0 Sep 22 09:01 README
drwxr-xr-x. 2 root root   133 Sep 22 14:31 shell
-rw-r--r--. 1 root root   115 Sep 22 10:40 testfile
-rw-r--r--. 1 root root    41 Sep 22 01:37 testfile_1
-rw-r--r--. 1 root root    20 Sep 22 10:40 xx00
-rw-r--r--. 1 root root    95 Sep 22 10:40 xx01
[root@localhost ~]# chmod ugo+r demo1.txt
[root@localhost ~]# ll
total 72
drwxr-xr-x. 2 root root     6 Sep 22 05:16 abc
-rw-------. 1 root root  1233 Sep 20 17:18 anaconda-ks.cfg
-rw-r--r--. 1 root root   607 Sep 22 11:55 demo1.txt
-rw-r--r--. 1 root root   656 Sep 22 17:02 demo2.txt
-rw-r--r--. 1 root root  1361 Sep 22 17:06 demo3.txt
-rw-r--r--. 1 root root   612 Sep 22 15:02 demo.txt
-rw-r--r--. 1 root root 33903 Sep 22 10:18 man-help.txt
-rw-r--r--. 1 root root     0 Sep 22 09:01 README
drwxr-xr-x. 2 root root   133 Sep 22 14:31 shell
-rw-r--r--. 1 root root   115 Sep 22 10:40 testfile
-rw-r--r--. 1 root root    41 Sep 22 01:37 testfile_1
-rw-r--r--. 1 root root    20 Sep 22 10:40 xx00
-rw-r--r--. 1 root root    95 Sep 22 10:40 xx01

将文件 demo1.txt 设为所有人皆可读取 :

chmod a+r demo1.txt

示例

[root@localhost ~/shell]# ll
total 24
-rw-r--r--. 1 root root  93 Sep 19 11:43 demo1.sh
-rw-r--r--. 1 root bin  120 Sep 22 12:11 demo1.txt
-rwxr-xr-x. 1 root root 219 Sep 19 11:43 demo2.sh
-rw-r--r--. 1 root bin  618 Sep 22 14:31 demo6.txt
-rw-r--r--. 1 root root   0 Sep 19 16:06 demo.cpp
-rw-r--r--. 1 root root 109 Sep 22 12:05 demo-let.sh
-rw-r--r--. 1 root root   0 Sep 19 16:06 main.c
-rw-r--r--. 1 root root 113 Sep 21 00:14 w.sh
[root@localhost ~]# chmod a+r demo1.txt
[root@localhost ~]# ll
total 72
drwxr-xr-x. 2 root root     6 Sep 22 05:16 abc
-rw-------. 1 root root  1233 Sep 20 17:18 anaconda-ks.cfg
-rw-r--r--. 1 root root   607 Sep 22 11:55 demo1.txt
-rw-r--r--. 1 root root   656 Sep 22 17:02 demo2.txt
-rw-r--r--. 1 root root  1361 Sep 22 17:06 demo3.txt
-rw-r--r--. 1 root root   612 Sep 22 15:02 demo.txt
-rw-r--r--. 1 root root 33903 Sep 22 10:18 man-help.txt
-rw-r--r--. 1 root root     0 Sep 22 09:01 README
drwxr-xr-x. 2 root root   133 Sep 22 14:31 shell
-rw-r--r--. 1 root root   115 Sep 22 10:40 testfile
-rw-r--r--. 1 root root    41 Sep 22 01:37 testfile_1
-rw-r--r--. 1 root root    20 Sep 22 10:40 xx00
-rw-r--r--. 1 root root    95 Sep 22 10:40 xx01

将文件 demo1.txt 与 demo2.txt 设为该文件拥有者,与其所属同一个群体者可写入,但其他以外的人则不可写入 :

chmod ug+w,o-w demo1.txt demo2.txt

将 demo1.sh 设定为只有该文件拥有者可以执行 :

chmod u+x demo1.sh

将目前目录下的所有文件与子目录皆设为任何人可读取 :

chmod -R a+r *

此外chmod也可以用数字来表示权限如 :

chmod 777 file

语法为:

chmod abc file

其中a,b,c各为一个数字,分别表示User、Group、及Other的权限。

r=4,w=2,x=1

  • 若要rwx属性则4+2+1=7;
  • 若要rw-属性则4+2=6;
  • 若要r-x属性则4+1=5。
chmod a=rwx file

chmod 777 file

效果相同

chmod ug=rwx,o=x file

chmod 771 file

效果相同

若用chmod 4755 filename可使此程序具有root的权限

返回上一级

Linux Shell 命令大全

关于   |   FAQ   |   我们的愿景   |   广告投放   |  博客

  简单教程,简单编程 - IT 入门首选站

Copyright © 2013-2022 简单教程 twle.cn All Rights Reserved.