【轉】每天一個linux命令(27):linux chmod命令

來源:互聯網
上載者:User

標籤:

原文網址:http://www.cnblogs.com/peida/archive/2012/11/29/2794010.html

chmod命令用於改變linux系統檔案或目錄的存取權限。用它控制檔案或目錄的存取權限。該命令有兩種用法。一種是包含字母和操作符運算式的文字設定法;另一種是包含數位數字設定法。

Linux系統中的每個檔案和目錄都有訪問許可許可權,用它來確定誰可以通過何種方式對檔案和目錄進行訪問和操作。
  檔案或目錄的存取權限分為唯讀,唯寫和可執行三種。以檔案為例,唯讀許可權表示只允許讀其內容,而禁止對其做任何的更改操作。可執行許可權表示允許將該檔案作為一個程式執行。檔案被建立時,檔案所有者自動擁有對該檔案的讀、寫和可執行許可權,以便於對檔案的閱讀和修改。使用者也可根據需要把存取權限設定為需要的任何組合。
  有三種不同類型的使用者可對檔案或目錄進行訪問:檔案所有者,同組使用者、其他使用者。所有者一般是檔案的建立者。所有者可以允許同組使用者有權訪問檔案,還可以將檔案的存取權限賦予系統中的其他使用者。在這種情況下,系統中每一位使用者都能訪問該使用者擁有的檔案或目錄。
  每一檔案或目錄的存取權限都有三組,每組用三位表示,分別為檔案屬主的讀、寫和執行許可權;與屬主同組的使用者的讀、寫和執行許可權;系統中其他使用者的讀、寫和執行許可權。當用ls -l命令顯示檔案或目錄的詳細資料時,最左邊的一列為檔案的存取權限。 例如:

命令: 

ls -al

輸出:

[[email protected] test]# ll -al

總計 316lrwxrwxrwx 1 root root     11 11-22 06:58 linklog.log -> log2012.log

-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

-rw-r--r-- 1 root root     61 11-13 06:03 log2013.log

-rw-r--r-- 1 root root      0 11-13 06:03 log2014.log

-rw-r--r-- 1 root root      0 11-13 06:06 log2015.log

-rw-r--r-- 1 root root      0 11-16 14:41 log2016.log

-rw-r--r-- 1 root root      0 11-16 14:43 log2017.log

我們以log2012.log為例:

-rw-r--r-- 1 root root 296K 11-13 06:03 log2012.log

第一列共有10個位置,第一個字元指定了檔案類型。在通常意義上,一個目錄也是一個檔案。如果第一個字元是橫線,表示是一個非目錄的檔案。如果是d,表示是一個目錄。從第二個字元開始到第十個共9個字元,3個字元一組,分別表示了3組使用者對檔案或者目錄的許可權。許可權字元用橫線代表空許可,r代表唯讀,w代表寫,x代表可執行。

例如:
  - rw- r-- r--
  表示log2012.log是一個普通檔案;log2012.log的屬主有讀寫權限;與log2012.log屬主同組的使用者只有讀許可權;其他使用者也只有讀許可權。

  確定了一個檔案的存取權限後,使用者可以利用Linux系統提供的chmod命令來重新設定不同的存取權限。也可以利用chown命令來更改某個檔案或目錄的所有者。利用chgrp命令來更改某個檔案或目錄的使用者組。 

chmod命令是非常重要的,用於改變檔案或目錄的存取權限。使用者用它控制檔案或目錄的存取權限。chmod命令詳細情況如下。

1. 命令格式:

chmod [-cfvR] [--help] [--version] mode file   

2. 命令功能:

用於改變檔案或目錄的存取權限,用它控制檔案或目錄的存取權限。

3. 命令參數:

必要參數:
-c 當發生改變時,報告處理資訊
-f 錯誤資訊不輸出
-R 處理指定目錄以及其子目錄下的所有檔案
-v 運行時顯示詳細處理資訊

選擇參數:
--reference=<目錄或者檔案> 設定成具有指定目錄或者檔案具有相同的許可權
--version 顯示版本資訊
<許可權範圍>+<使用權限設定> 使許可權範圍內的目錄或者檔案具有指定的許可權
<許可權範圍>-<使用權限設定> 刪除許可權範圍的目錄或者檔案的指定許可權
<許可權範圍>=<使用權限設定> 設定許可權範圍內的目錄或者檔案的許可權為指定的值

許可權範圍:
u :目錄或者檔案的當前的使用者
g :目錄或者檔案的當前的群組
o :除了目錄或者檔案的目前使用者或群組之外的使用者或者群組
a :所有的使用者及群組

許可權代號:
r :讀許可權,用數字4表示
w :寫入權限,用數字2表示
x :執行許可權,用數字1表示
- :刪除許可權,用數字0表示
s :特殊許可權 

 

該命令有兩種用法。一種是包含字母和操作符運算式的文字設定法;另一種是包含數位數字設定法。
  1). 文字設定法:
  chmod [who] [+ | - | =] [mode] 檔案名稱
  2). 數字設定法
  我們必須首先瞭解用數字表示的屬性的含義:0表示沒有許可權,1表示可執行許可權,2表示可寫入權限,4表示可讀許可權,然後將其相加。所以數字屬性的格式應為3個從0到7的八位元,其順序是(u)(g)(o)。
  例如,如果想讓某個檔案的屬主有“讀/寫”二種許可權,需要把4(可讀)+2(可寫)=6(讀/寫)。
  數字設定法的一般形式為:
  chmod [mode] 檔案名稱

 

數字與字元對應關係如下:

r=4,w=2,x=1
若要rwx屬性則4+2+1=7
若要rw-屬性則4+2=6;
若要r-x屬性則4+1=7。 

4. 使用執行個體:
執行個體1:增加檔案所有使用者組可執行許可權

命令:

chmod a+x log2012.log

輸出:

[[email protected] test]# ls -al log2012.log 

-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

[[email protected] test]# chmod a+x log2012.log 

[[email protected] test]# ls -al log2012.log 

-rwxr-xr-x 1 root root 302108 11-13 06:03 log2012.log

[[email protected] test]#

說明:
  即設定檔案log2012.log的屬性為:檔案屬主(u) 增加執行許可權;與檔案屬主同組使用者(g) 增加執行許可權;其他使用者(o) 增加執行許可權。
 

執行個體2:同時修改不同使用者權限

命令:

chmod ug+w,o-x log2012.log

輸出:

[[email protected] test]# ls -al log2012.log 

-rwxr-xr-x 1 root root 302108 11-13 06:03 log2012.log

[[email protected] test]# chmod ug+w,o-x log2012.log 

[[email protected] test]# ls -al log2012.log 

-rwxrwxr-- 1 root root 302108 11-13 06:03 log2012.log


說明:
  即設定檔案text的屬性為:檔案屬主(u) 增加寫入權限;與檔案屬主同組使用者(g) 增加寫入權限;其他使用者(o) 刪除執行許可權

 

執行個體3:刪除檔案許可權

命令:

chmod a-x log2012.log

輸出:

[[email protected] test]# ls -al log2012.log 

-rwxrwxr-- 1 root root 302108 11-13 06:03 log2012.log

[[email protected] test]# chmod a-x log2012.log 

[[email protected] test]# ls -al log2012.log 

-rw-rw-r-- 1 root root 302108 11-13 06:03 log2012.log

說明:
 刪除所有使用者的可執行許可權 
 

執行個體4:使用“=”設定許可權 

命令:

chmod u=x log2012.log

輸出:

[[email protected] test]# ls -al log2012.log 

-rw-rw-r-- 1 root root 302108 11-13 06:03 log2012.log

[[email protected] test]# chmod u=x log2012.log 

[[email protected] test]# ls -al log2012.log 

---xrw-r-- 1 root root 302108 11-13 06:03 log2012.log

說明:

撤銷原來所有的許可權,然後使擁有者具有可讀許可權 

 

執行個體5:對一個目錄及其子目錄所有檔案添加許可權 

命令:

chmod -R u+x test4

輸出:

[[email protected] test]# cd test4

[[email protected] test4]# ls -al

總計 312drwxrwxr-x 2 root root   4096 11-13 05:50 .

drwxr-xr-x 5 root root   4096 11-22 06:58 ..

-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:54 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:54 log2014.log

[[email protected] test4]# cd ..

[[email protected] test]# chmod -R u+x test4

[[email protected] test]# cd test4

[[email protected] test4]# ls -al

總計 312drwxrwxr-x 2 root root   4096 11-13 05:50 .

drwxr-xr-x 5 root root   4096 11-22 06:58 ..

-rwxr--r-- 1 root root 302108 11-12 22:54 log2012.log

-rwxr--r-- 1 root root     61 11-12 22:54 log2013.log

-rwxr--r-- 1 root root      0 11-12 22:54 log2014.log

說明:

遞迴地給test4目錄下所有檔案和子目錄的屬主分配許可權 

 

其他一些執行個體:

1). 

命令:

chmod 751 file   

說明:

給file的屬主分配讀、寫、執行(7)的許可權,給file的所在組分配讀、執行(5)的許可權,給其他使用者指派執行(1)的許可權

2). 

命令:

chmod u=rwx,g=rx,o=x file 

說明:

上例的另一種形式

3). 

命令

chmod =r file 

說明:                    

為所有使用者指派讀許可權

3). 

命令:

chmod 444 file 

說明: 

   同上例

4). 

命令:

chmod a-wx,a+r   file

說明:

同上例

【轉】每天一個linux命令(27):linux chmod命令

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.