在类unix系统中,系统经过chmod命令来改变文件系统对象(文件及目录)的访问权限。它一样能够用来改变特别的模式标记。操做请求是由umask过滤。命令是change和mode的缩写。app
命令的语法:less
chmod [options] mode [,mode] file [file2 ...]ui
一般的执行选项option包括:spa
-R 递归, 即包含子目录里的目标unix
-f 即对全部目标强制执行,即使中间发生错误code
-v 显示详细信息orm
若是一个符号连接被指定,目标对象则产生对应变化。与符号连接直接关联的文件模式一般不会被用到。经过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命令的第一个字符表示对象类型,连字符表示纯文本文件。脚本能够被当前用户读写及执行,能够被工做组其余用户读及执行,其余用户则只能读取。blog
八进制模式递归
chmod数字表示支持最多四个八进制数字。最右侧的三个数字分别指代文件全部者、群组级其余用户的权限。当出现四个数字时,最左边数字表示专门的setuid、setgid及sticky标记。
# | Permission | rwx |
---|---|---|
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一般用下面一个或多个字母表示:
Reference | Class | Description |
---|---|---|
u | user | 文件全部者 |
g | group | 组成员用户 |
o | others | 即非文件全部者也非组成员用户的用户 |
a | all | 上面三种用户 |
chmod命令使用一个操做符来指定怎么调整文件的模式,支持下列操做符:
Operator | Description |
---|---|
+ | 为指定类添加指定模式 |
- | 为指定类移除指定模式 |
= | 为指定类指定所有的文件模式 |
模式指定了哪种权限将被添加或者移除,一些基本的模式对应基本的权限:
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