Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux 檔案權限完整教學:ls -l 輸出解讀、chmod 符號/數字模式與常見權限對照表

編輯:Linux 教學

查看檔案權限

ls -l filename

輸出範例:-rw-rw-r--(共 10 字元)

位置字元含義
第 1 字元-檔案類型:-=一般檔案、d=目錄、l=符號連結、b=區塊裝置、c=字元裝置
第 2-4 字元rw-擁有者權限:r=讀、w=寫、x=執行、-=無
第 5-7 字元rw-群組權限
第 8-10 字元r--其他人權限

權限字元對應數值

權限數值
r (讀)4
w (寫)2
x (執行)1
- (無)0

三組權限相加:擁有者 rw- = 4+2+0 = 6,群組 rw- = 6,其他 r-- = 4 → 664。

chmod 修改權限

符號模式(直觀易懂)

chmod [ugoa][+-=][rwx] 檔案

# u=user(擁有者) g=group(群組) o=other(其他) a=all(全部)

# 範例
chmod o+w file.txt        # 其他人加入寫入權限
chmod go-rw file.txt      # 群組與其他人移除讀寫權限
chmod u+x,go-r script.sh  # 擁有者加入執行,群組其他移除讀取
chmod a=rw file.txt       # 所有人設為唯讀寫(移除執行)

數字模式(快速精確)

chmod 755 script.sh   # 擁有者 rwx(7),群組 rx(5),其他 rx(5)
chmod 644 file.txt    # 擁有者 rw(6),群組 r(4),其他 r(4)
chmod 600 secret.key  # 僅擁有者 rw(6)
chmod 700 private/    # 僅擁有者 rwx(7)

常見權限組合對照表

符號權限數字權限適用場景
-rw-------600私密金鑰、密碼檔、僅擁有者讀寫
-rw-r--r--644一般文字檔、設定檔、網頁檔案
-rwx------700私人腳本、執行檔
-rwxr-xr-x755公開腳本、系統執行檔、目錄標準權限
-rwx--x--x711僅允許進入目錄/執行,不可列出內容
-rw-rw-rw-666所有人可讀寫(不建議,無執行權)
-rwxrwxrwx777所有人完整存取(極度危險,嚴禁用於生產環境)

特殊權限位

位元數值符號對檔案效果對目錄效果
SUID4s (擁有者 x 位)執行時取得檔案擁有者權限(如 /usr/bin/passwd)無
SGID2s (群組 x 位)執行時取得檔案群組權限新建檔案繼承目錄群組
Sticky1t (其他 x 位)無僅檔案擁有者/root 可刪除(如 /tmp)
chmod 4755 /path/to/file  # 設定 SUID
chmod 2755 /shared/dir    # 設定 SGID
chmod 1777 /tmp           # 設定 Sticky bit

遞迴修改目錄樹權限

# 僅修改目錄加入 x,檔案不加 x
find /path -type d -exec chmod 755 {} \;
find /path -type f -exec chmod 644 {} \;

# 或使用 chmod -R 搭配 X(大寫:僅目錄或已有 x 的檔案加入 x)
chmod -R u+rwX,go+rX,go-w /path

預設權限 umask

umask 定義新建檔案/目錄的預設權限「遮罩」。

umask 022   # 預設檔案 644 (666-022),目錄 755 (777-022)
umask 077   # 最高隱私:檔案 600,目錄 700

永久生效請加入 ~/.bashrc 或 /etc/profile。

Copyright © Windows教程網 All Rights Reserved