首页·终端命令

chmod

权限与归属

用法

chmod 755 [path]

设置文件和目录的权限位;chmod -R 755 会把普通文件也变成可执行,所以想放过文件就用大写 X。

常用选项

选项含义
0755Numeric: owner, group, others, each read 4 + write 2 + execute 1
u+wSymbolic: who (u g o a), then + - =, then r w x
+xAdd the execute bit, the usual fix for a script
-RApply to a whole tree
+XExecute for directories only, never for plain files
-vName each file whose mode changed
-hBSD and macOS: change the symlink itself, not its target

示例

chmod +x deploy.sh

Makes the script runnable.

chmod -R u+rwX,go+rX,go-w site/

Capital X keeps files non-executable.

chmod 600 ~/.ssh/id_ed25519

What 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