天天看點

【Linux】一步一步學Linux——chmod指令(110)

00. 目錄

文章目錄

  • ​​00. 目錄​​
  • ​​01. 指令概述​​
  • ​​02. 指令格式​​
  • ​​03. 常用選項​​
  • ​​04. 參考示例​​
  • ​​05. 總結​​
  • ​​06. 附錄​​

01. 指令概述

chmod - 改變檔案的通路權限

​chmod指令​的英文原意是“change the permissions mode of a file”,我們簡稱為“change mode”,意為用來改變檔案或目錄權限的指令,但是隻有檔案的屬主和超級使用者root才能執行這個指令。有兩種模式,一種是采用權限字母和操作符表達式;另一種是采用數字。

​檔案十個字元意義如下圖所示​

【Linux】一步一步學Linux——chmod指令(110)

​權限進制表示​

【Linux】一步一步學Linux——chmod指令(110)

​rwx對檔案意義如下:​

r:可以使用内容檢視類的指令來顯示檔案相關内容

w: 可以使用編輯器修改檔案内容,但需要配置r權限一起使用

x:可以将檔案發起一個程序進行執行

​rwx對目錄意義如下:​

r:可以使用ls指令檢視目錄内容的檔案資訊,但使用cd切換不了其它目錄

w:可以建立、删除目錄裡的檔案,也可以删除目前目錄,但需要配合rx兩個權限一起使用

x:可以使用ls -l指令來檢視目錄内容的檔案資訊,并且可以使用cd指令切換此目錄為工作目錄,但需配合r權限一起使用

​權限範圍的表示法如下​:

​u​

​ User,即檔案或目錄的擁有者;

​g​

​ Group,即檔案或目錄的所屬群組;

​o​

​ Other,除了檔案或目錄擁有者或所屬群組之外,其他使用者皆屬于這個範圍;

​a​

​ All,即全部的使用者,包含擁有者,所屬群組以及其他使用者;

​r​

​ 讀取權限,數字代号為“4”;

​w​

​ 寫入權限,數字代号為“2”;

​x​

​ 執行或切換權限,數字代号為“1”;

​-​

​ 不具任何權限,數字代号為“0”;

​s​

​ 特殊功能說明:變更檔案或目錄的權限。

​權限設定​

  • 增權重限
  • 取消權限 =
  • 唯一設定權限

02. 指令格式

用法:chmod [選項]... 模式[,模式]... 檔案...
 或:chmod [選項]... 八進制模式 檔案...
 或:chmod [選項]... --reference=參考檔案 檔案...      

03. 常用選項

将每個檔案的模式更改為指定值。

  -c, --changes         類似 --verbose,但隻在有更改時才顯示結果
      --no-preserve-root        不特殊對待根目錄(預設)
      --preserve-root           禁止對根目錄進行遞歸操作
  -f, --silent, --quiet 去除大部份的錯誤資訊
  -v, --verbose         為處理的所有檔案顯示診斷資訊
      --reference=參考檔案      使用指定參考檔案的模式,而非自行指定權限模式
  -R, --recursive               以遞歸方式更改所有的檔案及子目錄
      --help            顯示此幫助資訊并退出
      --version         顯示版本資訊并退出

每種 MODE 都應屬于這類形式"[ugoa]*([-+=]([rwxXst]*|[ugo]))+"。      

04. 參考示例

​4.1 全部使用者增加讀權限​

[deng@itcast test]$ chmod a+r file 
[deng@itcast test]$ ls -l file 
-rw-rw-r-- 1 deng deng 0 8月   6 20:06 file
[deng@itcast test]$ 

#或者
[deng@itcast test]$ chmod  ugo+r file      

​4.2 所有使用者減去讀權限​

[deng@itcast test]$ chmod ugo-r file 
[deng@itcast test]$ ls -l file 
--w--w---- 1 deng deng 0 8月   6 20:06 file
[deng@itcast test]$ 

#或者
[deng@itcast test]$ chmod a-r file      

​4.3 檔案所屬者和所屬組增加讀寫權限​

[deng@itcast test]$ chmod ug+rw file
[deng@itcast test]$ ls -l file 
-rw-rw---- 1 deng deng 0 8月   6 20:06 file
[deng@itcast test]$      

​4.4 檔案其它設定權限為rw​

[deng@itcast test]$ chmod o=rw file
[deng@itcast test]$ ls -l file 
-rw-rw-rw- 1 deng deng 0 8月   6 20:06 file
[deng@itcast test]$      

​4.5 檔案所屬組設定權限為rwx,所屬組設定為rw,其它設定為r​

[deng@itcast test]$ chmod u=rwx,g=rw,o=r file 
[deng@itcast test]$ ls -l file 
-rwxrw-r-- 1 deng deng 0 8月   6 20:06 file
[deng@itcast test]$      

​4.6 檔案所屬者和所屬組追加寫,其它撤銷寫權限​

[deng@itcast test]$ chmod ug+w,o-w file
[deng@itcast test]$ ls -l file 
-rwxrw-r-- 1 deng deng 0 8月   6 20:06 file
[deng@itcast test]$      

​4.7 檔案所屬者增加可執行權限​

[deng@itcast test]$ chmod  u+x file 
[deng@itcast test]$ ls -l file 
-rwxrw-r-- 1 deng deng 0 8月   6 20:06 file
[deng@itcast test]$      

​4.8 遞歸将test目錄下所有的檔案和目錄追加讀權限​

[deng@itcast test]$ chmod -R a+r test/      

​4.9 檔案所有使用者撤銷執行權限​

[deng@itcast test]$ chmod a-x file      

​4.10 所有人追加可執行權限​

[deng@itcast test]$ chmod +x file 
[deng@itcast test]$ ls -l file 
-rwxrwxr-x 1 deng deng 0 8月   6 20:06 file
[deng@itcast test]$ 

#等價于
[deng@itcast test]$ chmod a+x file      

​4.11 設定檔案所屬者讀寫,所屬組和其它讀權限​

[deng@itcast test]$ chmod 644 file 
[deng@itcast test]$ ls -l file 
-rw-r--r-- 1 deng deng 0 8月   6 20:06 file
[deng@itcast test]$      

​4.12 将所有使用者設定為隻讀權限​

[deng@itcast test]$ chmod 444 file 
[deng@itcast test]$ ls -l file 
-r--r--r-- 1 deng deng 0 8月   6 20:06 file
[deng@itcast test]$      

等價于

[deng@itcast test]$ chmod a-wx,a+r file 
[deng@itcast test]$ ls -l file 
-r--r--r-- 1 deng deng 0 8月   6 20:06 file
[deng@itcast test]$      

​4.13 将所有的使用者設定為隻讀權限​

[deng@itcast test]$ chmod =r file 
[deng@itcast test]$ ls -l file 
-r--r--r-- 1 deng deng 0 8月   6 20:06 file
[deng@itcast test]$      

​4.14 檔案所屬者讀寫執行權限,檔案所屬組讀和執行權限,檔案其它執行權限​

[deng@itcast test]$ chmod 751 file 
[deng@itcast test]$ ls -l file 
-rwxr-x--x 1 deng deng 0 8月   6 20:06 file
[deng@itcast test]$      

等價于

[deng@itcast test]$ chmod u=rwx,g=rx,o=x file 
[deng@itcast test]$ ls -l file 
-rwxr-x--x 1 deng deng 0 8月   6 20:06 file
[deng@itcast test]$      

05. 總結

【Linux】一步一步學Linux——chmod指令(110)

06. 附錄

繼續閱讀