首頁·終端機命令

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