Linux常用命令(二十二),linux常用命令
chmod命令用於改變linux系統檔案或目錄的存取權限。用它控制檔案或目錄的存取權限。該命令有兩種用法。一種是包含字母和操作符運算式的文字設定法;另一種是包含數位數字設定法。
Linux系統中的每個檔案和目錄都有訪問許可許可權,用它來確定誰可以通過何種方式對檔案和目錄進行訪問和操作。
檔案或目錄的存取權限分為唯讀,唯寫和可執行三種。以檔案為例,唯讀許可權表示只允許讀其內容,而禁止對其做任何的更改操作。可執行許可權表示允許將該檔案作為一個程式執行。檔案被建立時,檔案所有者自動擁有對該檔案的讀、寫和可執行許可權,以便於對檔案的閱讀和修改。使用者也可根據需要把存取權限設定為需要的任何組合。
有三種不同類型的使用者可對檔案或目錄進行訪問:檔案所有者,同組使用者、其他使用者。所有者一般是檔案的建立者。所有者可以允許同組使用者有權訪問檔案,還可以將檔案的存取權限賦予系統中的其他使用者。在這種情況下,系統中每一位使用者都能訪問該使用者擁有的檔案或目錄。
每一檔案或目錄的存取權限都有三組,每組用三位表示,分別為檔案屬主的讀、寫和執行許可權;與屬主同組的使用者的讀、寫和執行許可權;系統中其他使用者的讀、寫和執行許可權。當用ls -l命令顯示檔案或目錄的詳細資料時,最左邊的一列為檔案的存取權限。 例如:
[root@localhost test]# ll -al-rwxrwxrwx 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. 命令參數:
a. 必要參數:
-c 當發生改變時,報告處理資訊
-f 錯誤資訊不輸出
-R 處理指定目錄以及其子目錄下的所有檔案
-v 運行時顯示詳細處理資訊
b.選擇參數:
--reference = <目錄或者檔案> 設定成具有指定目錄或者檔案具有相同的許可權
--version 顯示版本資訊
<許可權範圍> + <使用權限設定> 使許可權範圍內的目錄或者檔案具有指定的許可權
<許可權範圍> - <使用權限設定> 刪除許可權範圍的目錄或者檔案的指定許可權
<許可權範圍> = <使用權限設定> 設定許可權範圍內的目錄或者檔案的許可權為指定的值
c.許可權範圍:
u :目錄或者檔案的當前的使用者
g :目錄或者檔案的當前的群組
o :除了目錄或者檔案的目前使用者或群組之外的使用者或者群組
a :所有的使用者及群組
d.許可權代號:
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. 命令執行個體:
執行個體一. 增加檔案所有使用者組可執行許可權
命令: chmod a+x log2012.log
說明: 檔案屬主(u)增加執行許可權, 與檔案屬主同組使用者(g)增加執行許可權, 其他使用者(o)增加執行許可權
[root@localhost test]# ls -al log2012.log -rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log[root@localhost test]# chmod a+x log2012.log [root@localhost test]# ls -al log2012.log -rwxr-xr-x 1 root root 302108 11-13 06:03 log2012.log[root@localhost test]#
執行個體二. 同時修改不同使用者權限
命令: chmod ug+w,o-x log2012.log
說明: 檔案屬主(u)增加寫入權限, 與檔案屬主同組使用者(g)增加寫入權限, 其他使用者(o)刪除執行許可權
[root@localhost test]# ls -al log2012.log -rwxr-xr-x 1 root root 302108 11-13 06:03 log2012.log[root@localhost test]# chmod ug+w,o-x log2012.log [root@localhost test]# ls -al log2012.log -rwxrwxr-- 1 root root 302108 11-13 06:03 log2012.log
執行個體三. 刪除檔案許可權
命令: chmod a-x log2012.log
說明: 刪除所有使用者的可執行許可權
[root@localhost test]# ls -al log2012.log -rwxrwxr-- 1 root root 302108 11-13 06:03 log2012.log[root@localhost test]# chmod a-x log2012.log [root@localhost test]# ls -al log2012.log -rw-rw-r-- 1 root root 302108 11-13 06:03 log2012.log
執行個體四. 使用"="設定許可權
命令: chmod u=x log2012.log
說明: 撤銷使用者原來所有許可權, 然後使擁有者具有可執行許可權
[root@localhost test]# ls -al log2012.log -rw-rw-r-- 1 root root 302108 11-13 06:03 log2012.log[root@localhost test]# chmod u=x log2012.log [root@localhost test]# ls -al log2012.log ---xrw-r-- 1 root root 302108 11-13 06:03 log2012.log
執行個體五. 對一個目錄及其子目錄所有檔案添加許可權
命令: chmod -R u+x test4
說明: 遞迴地給test4目錄下所有檔案和子目錄的屬主分配許可權
[root@localhost test]# cd test4[root@localhost test4]# ls -aldrwxr-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[root@localhost test4]# cd ..[root@localhost test]# chmod -R u+x test4[root@localhost test]# cd test4[root@localhost test4]# ls -aldrwxr-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
執行個體六. 其他一些例子
1. chmod 751 file 給file的屬主分配讀,寫,執行許可權; 給file所在的組分配讀, 執行許可權; 給其他使用者指派執行的許可權
2. chmod u=rwx,g=rx,o=x file 功能同(1)
3. chmod =r file 為所有使用者指派讀許可權
4. chmod 444 file 功能同(3)
5. chmod a-wx, a+r file 功能同(3)
原文: http://www.cnblogs.com/peida/archive/2012/11/29/2794010.html