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

Unix/Linux檔案權限修改命令chmod詳細教學

編輯:Linux 教學

chmod是Unix/Linux系統中修改檔案存取權限的核心命令,支援符號模式與絕對模式(八進位)兩種語法,靈活控制擁有者、群組、其他人的讀寫執行權限。

符號模式

chmod [who] [op] [permission] file...
代碼對象
uUser(檔案擁有者)
gGroup(所屬群組)
oOthers(其他人)
aAll(所有人,等同ugo)
運算子動作
+新增權限
-移除權限
=設定為指定權限(移除原有)
權限含義
rRead(可讀)
wWrite(可寫)
xExecute(可執行/進入目錄)

範例

chmod u+w test report      # 擁有者新增寫入權限
chmod u-x abc.c            # 擁有者移除執行權限
chmod u+rwx myfile1        # 擁有者完整權限
chmod ugo+rwx myfile2      # 所有人完整權限
chmod a=r,ug+w shared.txt  # 所有人可讀,擁有者與群組可寫

絕對模式(八進位)

chmod xyz file...

x、y、z分別為擁有者、群組、其他人的權限值(0-7)。

計算公式:值 = 4*r + 2*w + 1*x(r/w/x為0或1)。

值權限組合二進位
7rwx111
6rw-110
5r-x101
4r--100
3-wx011
2-w-010
1--x001
0---000

範例

chmod 751 ncp
# 擁有者 7=rwx,群組 5=r-x,其他 1=--x

遞迴修改與特殊位元

chmod -R 755 /var/www       # 遞迴修改目錄樹
chmod u+s /usr/bin/su       # 設定SUID(以擁有者身分執行)
chmod g+s /home/shared      # 設定SGID(新檔繼承群組)
chmod +t /tmp               # 設定Sticky bit(僅擁有者可刪除)

實務建議

  • 腳本檔需chmod +x script.sh賦予執行權限。
  • Web目錄建議755(目錄)、644(檔案),敏感設定檔600。
  • 使用chmod --reference=ref_file target_file複製參考檔權限。
  • 謹慎使用777,暴露寫入風險。

精通chmod是維護系統安全與多使用者協作的基本功。

Copyright © Windows教程網 All Rights Reserved