天天看點

chmod-修改檔案權限

chmod

是Linux修改檔案權限的指令

這裡的

-R

為遞歸操作,即同時修改子目錄下所有檔案。

權限

由 數字 或 字元 進行表示

1、數字

Linux中檔案所屬的角色有3個:owner、group、other,它們又各自擁有自己的read、write、execute權限,即共9個基本權限。read、write、execute分别用 r、w、x 來表示,其對應的數字為

r  = 
w  = 
x  = 
           

将數字進行累加,得到的值就代表了相應的權限

以owner角色為例,假設有檔案 test.sh ,owner對檔案同時擁有r、w、x3個權限,那麼它的權限為 r+w+x => 4+2+1 = 7,這裡的 7 就是owner這個角色對檔案所擁有的的權限值

同理,假設group角色對檔案僅擁有r、w權限,那麼它的權限值為r+w => 4+2 = 6;other僅擁有w權限,它的權限值為 x => 1

此時,拼接3個角色所計算出來的權限值,就是檔案的權限了。即[owner][group][other] => [7][6][1] = 761,更改檔案權限

chmod

的文法為

$chmod  test.sh
           

如果某角色對檔案沒有任何權限,那麼其對應的權限值為 0 。以上面例子為基礎,我們希望将檔案的other角色的權限去除,即不給other角色任何權限。

other角色所擁有的權限值為 0 ,那麼更改檔案權限的

chmod

文法為

$chmod  test.sh
           

2、字元

以u、g、o分别代表角色owner、group、other,同時以 a 代表all(即同時代表3個角色)。reead、write、execute依然以 r、w、x 代表,此時

chmod

文法為

$chmod u=rwx,g=rwx,o=rwx test.sh
           

等價于

$chmod a=rwx test.sh
           

假設owner使用者對檔案 test.sh 擁有r、w、x權限,group角色擁有r、w權限,other角色擁有r權限,此時

chmod

文法為

$chmod u=rwx,g=rw,o=r test.sh
           

若此時希望将other角色權限去除,即不給于other角色任何權限,那麼指令就變成了

$chmod u=rwx,g=rw test.sh
           

為了友善單獨設定某個角色的某個權限,可以使用

+

-

進行操作。

+

為添權重限,

-

為去除權限

假設我們需要給group角色去除 w 權限,

chmod

的文法為

$chmod g-w test.sh
           

為other使用者添加 r 權限

$chmod o+r test.sh
           

值得注意的是,a 代表了所有角色,而角色符預設時,預設為 a ,即

$chmod a+x test.sh
           

等價于

$chmod +x test.sh
           

繼續閱讀