linux中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

輸出:

[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]#

說明:
  即設定文件log2012.log的屬性為:文件屬主(u) 增加執行權限;與文件屬主同組用戶(g) 增加執行權限;其他用戶(o) 增加執行權限。
 

實例2:同時修改不同用戶權限

命令:

chmod ug+w,o-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 

[root@localhost 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

輸出:

[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

說明:
  刪除所有用戶的可執行權限 
 

實例4:使用“=”設置權限 

命令:

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

說明:

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

實例5:對一個目錄及其子目錄所有文件添加權限 

命令:

chmod -R u+x test4

輸出:

[root@localhost test]# cd test4

[root@localhost 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

[root@localhost test4]# cd ..

[root@localhost test]# chmod -R u+x test4

[root@localhost test]# cd test4

[root@localhost 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

說明:

同上例


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM