chmod命令

在类unix系统中,系统通过chmod命令来改变文件系统对象(文件及目录)的访问权限。它同样可以用来改变特别的模式标记。操作请求是由umask过滤。命令是change和mode的缩写。

命令的语法:

chmod [options] mode [,mode] file [file2 ...]

通常的执行选项option包括:

R 递归, 即包含子目录里的目标

f 即对所有目标强制执行,即便中间发生错误

v 显示详细信息

如果一个符号链接被指定,目标对象则产生对应变化。与符号链接直接关联的文件模式通常不会被用到。通过ls和stat命令可以查看文件模式。比如:

$ ls -l findPhoneNumbers.sh
-rwxr-xr--  1 dgerman  staff  823 Dec 16 15:03 findPhoneNumbers.sh
$ stat -c %a findPhoneNumbers.sh
754

 文件模式中的r,w,x分别表示读、写及执行访问。ls命令的第一个字符表示对象类型,连字符表示纯文本文件。脚本可以被当前用户读写及执行,可以被工作组其他用户读及执行,其他用户则只能读取。

八进制模式

chmod数字表示支持最多四个八进制数字。最右侧的三个数字分别指代文件所有者、群组级其他用户的权限。当出现四个数字时,最左边数字表示专门的setuid、setgid及sticky标记。

#Permissionrwx
7 read, write and execute rwx
6 read and wite rw-
5 read and execute r-x
4 read only r--
3 write and execute -wx
2 write only -w-
1 execute only --x
0 none ---

例如需要允许所有群组成员可以更新该文件,可以使用chmod 664 sharedFile,由于没有设置特殊标记,所有等同于chmod 0664 sharedFile。

符号模式

chmod命令同样支持划分更细的符号表示。它允许修改指定的模式并保持其它模式不变。符号模式由三部分组成,表示如下:

$ chmod [references] [operator] [modes] file ...

其中references表示权限作用的用户类型。如果没有指定则表示全部用户。references通常用下面一个或多个字母表示:

ReferenceClassDescription
u user 文件所有者
g group 组成员用户
o others 即非文件所有者也非组成员用户的用户
a all 上面三种用户

 chmod命令使用一个操作符来指定怎么调整文件的模式,支持下列操作符:

OperatorDescription
+ 为指定类添加指定模式
- 为指定类移除指定模式
= 为指定类指定全部的文件模式

 模式指定了哪一种权限将被添加或者移除,一些基本的模式对应基本的权限:

r <read> <read a file or list a directory's contents>

w <write> <write to a file or directory>

x <execute> <execute a file or recurse a directory tree>

X <special execute> <which is not a permission in itself but rather can be used instead of x. It applies execute permissions to directories regardless of their current permissions and applies execute permissions to a file which already has at least one execute permission bit already set (either user, group or other). It is only really useful when used with '+' and usually in combination with the -R option for giving group or other access to a big directory tree without setting execute permission on normal files (such as text files), which would normally happen if you just used "chmod -R a+rx .", whereas with 'X' you can do "chmod -R a+rX ." instead>

s <setuid/gid> <details in Special modes section>

t <sticky> <details in Special modes section>

多个符号标记可以同时指定,不需要逗号或空格。

参考:

https://en.wikipedia.org/wiki/Chmod

posted @ 2015-10-21 22:34  wmymartin  阅读(391)  评论(0编辑  收藏  举报