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 數字/符號模式、chown/chgrp 所有權變更

編輯:Linux 教學

ls -l 權限欄位解讀

-rw-r--r--  1 user group 1024 Jan 1 10:00 file.txt
│││││││││
│││││││└─ 其他人權限:r--
││││││└──── 群組權限:r--
│││││└────── 擁有者權限:rw-
││││└─────── 檔案類型:- (一般檔), d (目錄), l (連結) ...
  • 首字元 d 表示目錄,- 一般檔,l 符號連結。
  • 隱藏檔以 . 開頭(ls -a 可見)。

權限數字代碼(八進位)

例:-rw-r--r-- → 擁有者 4+2=6,群組 4,其他 4 → 644。

chmod 兩種模式

數字模式

chmod 644 file.txt        # 擁有者 rw-, 群組 r--, 其他 r--
chmod 755 script.sh       # 擁有者 rwx, 群組 r-x, 其他 r-x

符號模式

chmod u+x file            # 擁有者加執行權
chmod g-w file            # 群組減寫入權
chmod o=r file            # 其他人設為唯讀
chmod a+rX dir            # 所有人加讀權,目錄加執行權 (X 只對目錄/已有執行權的檔生效)
  • u=user(擁有者)、g=group、o=others、a=all。
  • +增加、-移除、=精確設定。

所有權變更

chown user:group file     # 同時改擁有者與群組
chown user file           # 僅改擁有者
chgrp group file          # 僅改群組
chown -R user:group dir   # 遞迴修改目錄樹

實用技巧

  • chmod 600 ~/.ssh/id_rsa:SSH 私鑰僅擁有者可讀寫。
  • find /var/www -type f -exec chmod 644 {} \;:批量修正檔案權限。
  • find /var/www -type d -exec chmod 755 {} \;:批量修正目錄權限。
Copyright © Windows教程網 All Rights Reserved
權限二進位數值
r (讀)1004
w (寫)0102
x (執行/進入)0011