chmod
权限与归属
用法
chmod 755 [path]设置文件和目录的权限位;chmod -R 755 会把普通文件也变成可执行,所以想放过文件就用大写 X。
常用选项
| 选项 | 含义 |
|---|---|
| 0755 | Numeric: owner, group, others, each read 4 + write 2 + execute 1 |
| u+w | Symbolic: who (u g o a), then + - =, then r w x |
| +x | Add the execute bit, the usual fix for a script |
| -R | Apply to a whole tree |
| +X | Execute for directories only, never for plain files |
| -v | Name each file whose mode changed |
| -h | BSD and macOS: change the symlink itself, not its target |
示例
chmod +x deploy.shMakes the script runnable.
chmod -R u+rwX,go+rX,go-w site/Capital X keeps files non-executable.
chmod 600 ~/.ssh/id_ed25519What ssh insists on for a private key.
数字模式和符号模式说的是同一件事:755 和 u=rwx,go=rx 是同一个权限。
怎么看
- 方括号 [ ] 表示这部分可以不写。
- 省略号 … 表示可以写多个。
- 选项区分大小写——有些命令里 -r 和 -R 不是一回事。
常见问题
Q. chmod 是做什么的?
设置文件和目录的权限位;chmod -R 755 会把普通文件也变成可执行,所以想放过文件就用大写 X。
Q. 怎么写?
chmod 755 [path] —— 方括号表示可以省略的部分。
Q. 值得记的选项有几个?
这里列了 7 个,完整列表在 man chmod。这条命令属于权限与归属。
相关命令
man 手册: man chmod