chmod 權限字串或八進位數字明確定義了 Unix 系統上哪些使用者可以讀取、寫入或執行檔案或目錄。標準的 9 字元符號表示法(例如 rwxr-xr--)會拆成三組三位元,分別代表擁有者、群組和其他人,而 4 位數的八進位值(例如 0754)則將相同的權限加上任何 setuid、setgid 或 sticky 位元編碼成單一數字。手動在這些格式之間轉換容易出錯,特別是涉及特殊位元時更是如此。Chmod 計算機消除了猜測,能將任何有效的權限快照轉換成其標準的八進位與符號形式,包括特殊位元,讓您可以複製所需的精確 chmod 指令。

表示 Unix 權限的兩種方式
Unix 系統以兩種等效的方式表示檔案權限:9 字元的符號字串和 4 位數的八進位數字。符號字串依序列出擁有者、群組和其他人的讀取(r)、寫入(w)和執行(x)旗標。例如,rwxr-xr-- 表示擁有者擁有完整權限,群組可以讀取和執行,而其他人只能讀取。八進位數字將這些旗標壓縮成四位數:第一位數編碼 setuid(4)、setgid(2)和 sticky(1),接下來三位數則代表擁有者、群組和其他人,分別是讀取(4)、寫入(2)和執行(1)的總和。同樣的 rwxr-xr-- 權限轉換成八進位即為 0754——前導的 0 表示沒有特殊位元,7 授予擁有者讀取+寫入+執行,5 授予群組讀取+執行,4 授予其他人僅讀取。
這些格式可以互換,但各有不同的用途。符號表示法易於閱讀,出現在 ls -l 的輸出中,而八進位值則較為精簡,可直接在 chmod 指令中使用。Chmod 計算機讓您可以在兩者之間即時切換,因此在套用變更之前,您可以選擇偏好的形式來檢查權限。
特殊位元如何改變權限行為
除了基本的讀取、寫入和執行旗標外,Unix 權限還包含三個特殊位元,會改變程式執行的方式或目錄的行為。setuid 位元(4)使可執行檔以擁有者的權限而非使用者的權限執行,這對於像 passwd 這類需要 root 權限的程式至關重要。setgid 位元(2)對群組權限做同樣的事情,並強制目錄中新建立的檔案繼承該目錄的群組。sticky 位元(1)將目錄中的檔案刪除權限限制為檔案擁有者,這就是為什麼 /tmp 會使用它來防止使用者刪除彼此的暫存檔。
這些特殊位元編碼在八進位權限值的第一位數中。例如,4755 設定 setuid 位元並授予所有人 rwxr-xr-x,而 1777 則在允許所有使用者完整存取的目錄上設定 sticky 位元。在符號表示法中,setuid 以擁有者執行位置上的 s 顯示(例如 rwsr-xr-x),setgid 以群組執行位置上的 s 顯示,sticky 則以其他人執行位置上的 t 顯示。Chmod 計算機同時以八進位和符號形式顯示這些特殊位元,讓您在執行 chmod 之前能確認它們是否存在。
當 setuid 或 setgid 已設定但對應的執行位元不存在時,特殊位元會以不同的大小寫顯示:擁有者或群組的執行位置上會出現大寫的 S。這種狀態通常沒有意義,往往表示檔案設定錯誤。沒有執行位元的 sticky 位元則以大寫的 T 顯示。在 ls -l 輸出中辨識這些大寫字母,有助於發現可能會被忽略的權限不一致問題。
| 特殊位元 | 八進位值 | 符號表示法 | 效果 |
|---|---|---|---|
| setuid | 4 | 擁有者執行位置上的 s |
以擁有者的權限執行可執行檔 |
| setgid | 2 | 群組執行位置上的 s |
以群組的權限執行可執行檔;新檔案繼承目錄的群組 |
| sticky | 1 | 其他人執行位置上的 t |
將目錄中的檔案刪除權限限制為檔案擁有者 |
使用 Chmod 計算機逐步轉換權限
- 在瀏器中開啟 Chmod 計算機。
- 選擇轉換方向:八進位轉符號,或符號轉八進位。
- 以要求的嚴格格式輸入權限值:
- 八進位:4 位數字(例如
0755或4755)。 - 符號:9 字元字串(例如
rwxr-xr-x),或以逗號分隔的列表(例如u=rwx,g=rx,o=rx)。
- 八進位:4 位數字(例如
- 檢視結果:
- 標準的 4 位數八進位值。
- 9 字元的符號字串。
- 針對擁有者、群組和其他人拆解權限的表格。
- 複製產生的
chmod指令範本(例如chmod 0755 filename)。 - 在套用之前,使用
ls -l確認目標檔案目前的權限,使用ls -ld(針對目錄)檢查擁有權,並確認新權限符合最低權限原則。 - 在終端機中執行
chmod指令。
常見的權限情境及其數值
大多數檔案和目錄都屬於幾種標準的權限模式。擁有者應可讀取和寫入、其他人僅可讀取的普通檔案使用 0644(符號:rw-r--r--)。需要執行權限才能進入的目錄通常使用 0755(符號:rwxr-xr-x)供共享存取,或使用 0700(符號:rwx------)供私人使用。可執行腳本或程式經常使用 0755 以允許所有人執行,而敏感的執行檔可能會使用 4755(符號:rwsr-xr-x)來啟用 setuid。
對於多位使用者協作的共享目錄,setgid 位元(2775,符號:rwxrwsr-x)可確保新檔案繼承目錄的群組,避免權限不符。sticky 位元(1777,符號:rwxrwxrwt)對於像 /tmp 這類使用者需要建立檔案但不應刪除彼此檔案的目錄來說是必要的。Chmod 計算機透過顯示每個情境精確的八進位和符號值,幫助您正確套用這些模式。
| 情境 | 八進位值 | 符號表示法 | 使用案例 |
|---|---|---|---|
| 私人檔案 | 0600 | rw------- |
設定檔、私人文件 |
| 公開可讀取檔案 | 0644 | rw-r--r-- |
網頁、共享文件 |
| 私人目錄 | 0700 | rwx------ |
家目錄、私人專案 |
| 共享目錄 | 0755 | rwxr-xr-x |
全系統腳本、共享工具 |
| 含 setgid 的共享目錄 | 2775 | rwxrwsr-x |
協作專案目錄 |
| Sticky 目錄 | 1777 | rwxrwxrwt |
像 /tmp 這類的暫存目錄 |
八進位與符號表示法:何時該使用哪一種
八進位表示法在腳本和自動化中特別出色,因為簡潔和明確的解析在這些情境中很重要。像 0640 這樣的單一數字易於嵌入組態管理工具、shell 迴圈或 Dockerfile 的 RUN 指令中。另一方面,當您想在不覆寫現有權限的情況下進行針對性調整時,符號表示法是理想的選擇。像 chmod g+w file 或 chmod o-r file 這類指令可以新增或移除特定旗標,同時保留其他所有設定——這種操作若用八進位表示會很麻煩。
對於命令列上的日常互動使用,許多管理員偏好符號表示法,因為它的閱讀方式就像英文:「給予群組寫入權限」可直接轉譯為 g+w。對於文件、教學或稽核而言,ls -l 產生的 9 字元字串提供了一目了然的檢視,這是八進位無法比擬的。Chmod 計算機能即時在這兩個世界之間轉換,讓您可以以符號形式思考權限,然後將其部署為精簡的八進位值——反之亦然——無需手動轉換而產生錯誤。
讀取與解讀 ls -l 輸出
ls -l 指令會在每行開頭產生一個 10 字元的權限字串。第一個字元識別檔案類型:連字號(-)表示普通檔案,d 表示目錄,l 表示符號連結,c 表示字元裝置,b 表示區塊裝置。剩下的九個字元構成本文中討論的符號權限字串。例如,-rwxr-xr-x 描述一個權限為 0755 的普通檔案,而 drwxrwxrwt 描述一個設定了 sticky 位元、權限為 1777 的目錄。
ls -l 輸出中的其他欄位顯示連結數、擁有者名稱、群組名稱、檔案大小、修改時間和檔名。擁有權與權限一樣重要:即使是不歸您擁有的檔案上有 0777,若不先取得擁有權或使用較高權限,也無法修改。在稽核權限時,請結合使用 ls -l 與 stat 或 find 進行更深入的檢查。Chmod 計算機能補足這些指令的不足,讓您解碼或編碼任何遇到的權限字串,包括在執行位置顯示為 s、S、t 或 T 的特殊位元。
Why Manual Calculation Leads to Mistakes
Calculating chmod permissions manually is prone to errors because it requires remembering the numeric values for each flag (4 for read, 2 for write, 1 for execute) and summing them correctly for each of the three user classes. Special bits add another layer of complexity, as they share the same numeric values but affect different parts of the permission string. For example, confusing setuid (4) with the owner’s read flag (also 4) can lead to granting unintended privileges. Symbolic notation avoids some of these pitfalls but introduces its own ambiguities, such as whether u=rwx,g=rx,o=rx should include special bits or not.
Manual errors often go unnoticed until they cause security issues or break functionality. A misplaced 7 in the octal value could grant write access to everyone, while forgetting the execute bit on a directory makes it inaccessible. The Chmod Calculator eliminates these risks by validating your input and showing the exact permission breakdown in both octal and symbolic forms. It also highlights special bits, so you can confirm their presence before applying changes.
For developers working across multiple systems, the calculator ensures consistency. Different Unix-like systems may interpret symbolic notation slightly differently, but octal values are universally understood. By converting your intended permissions to octal, you can use the same chmod command on Linux, macOS, BSD, or even Windows Subsystem for Linux without surprises. The calculator also serves as a quick reference for permission values, reducing the need to memorize numeric codes or look them up repeatedly.
Security Best Practices When Changing Permissions
Permissions are a cornerstone of Unix security, so changes should always follow the principle of least privilege: grant only the access necessary for a task to function. Avoid 0777 for any file or directory accessible by multiple users, and never set setuid or setgid on scripts or binaries you didn’t compile yourself, as these bits can be exploited to escalate privileges. When deploying web applications, configuration files containing database credentials or API keys should use 0600 or 0640 to restrict access to the owner or a specific group.
Before running chmod recursively with the -R flag, double-check the target path. A misplaced space or wildcard can change permissions on entire directory trees, sometimes breaking system functionality or exposing sensitive data. The Chmod Calculator is a helpful checkpoint: generate the exact command you intend to run, verify the octal or symbolic value matches your expectations, and then apply it. Combining this habit with regular audits using find (e.g., find / -perm -4000 to locate setuid files) keeps your system both functional and secure.
More on this topic: How to Check if Your JSON Format Is Correct.
Related reading: Calculate Chmod Values Quickly Using Octal and Symbolic Notation.