字段含義簡述
Linux提供目錄和檔案級别的權限管理。每個檔案和目錄都有一個owner(所有者)和一個group(使用者組)。一個檔案或目錄可以對owner、所屬group中的使用者和其他所有使用者開放不同的權限。
- 對于一個檔案,“r”代表讀權限,“w”代表寫權限。
- 對于一個目錄,“r”代表檢視目錄下内容的權限,“w”代表在目錄中建立或删除新的檔案或目錄的權限,“x”代表通路該目錄的子目錄的權限。
當你執行ll ,通常會看到類似于下圖的情況:

和權限有關的是“drwxr-xr-x”、“root”和“root”這三個字段。其中:
- 第一個字段“drwxr-xr-x”包含了下面資訊:
- 第一個字元顯示該行末尾的路徑是檔案還是目錄:
- 如果第一個字元是“d”代表該路徑對應一個目錄;
- 如果第一個字元是“-”則代表該路徑對應一個檔案。
- 後九個字元可分為三組三個的字元:
- 第一組的三個字元代表該路徑的owner的權限;
- 第二組的三個字元代表該路徑所屬的group的權限;
- 第三組的三個字元代表所有其他使用者對該路徑擁有的權限。
-
每組三個字元中的第一個對應“r”,第二個對應“w”,第三個對應“x”,如果對應的位置顯示是字
母,則代表對應使用者有字母所代表的權限,如果是“-”則代表沒有權限。
- 作業系統下檔案系統的“rwx”權限可以用數字表示——比如“rwxrwxrwx” =777,“rwxr-xr-x” = 755等等。rwxrwxrwx表示的二進制為:111 111 111=7 7 7,表示owner的權限為rwx,group的權限為rwx,其他使用者的權限為rwx。
- 第二個字段“root”對應着該行末尾路徑的owner。
- 第三個四段“root”對應該行末尾路徑所屬的group。
權限管理指令
前置準備
建立目錄:
mkdir Demo
在目錄Demo中建立檔案hello.sh,檔案内容如下:
echo "hello world!"
操作指令
chown 修改檔案或目錄的owner或組
- 修改檔案或目錄的owner示例:
-
将 Demo 的屬主更改為"rancher":
chown rancher Demo/
[root@weltest ~]# chown rancher Demo/
[root@weltest ~]# ll
總用量 91424
-rw-------. 1 root root 1261 2月 6 17:05 anaconda-ks.cfg
drwxr-xr-x 2 root root 145 2月 8 09:11 cad
drwxr-xr-x 2 rancher root 22 2月 11 11:26 Demo
-rw-r--r--. 1 root root 655 2月 6 10:28 modules.sh
-rw------- 1 root root 93609984 2月 10 16:37 tiller2.16.1.tar
-
将Demo的owner更改為root,group更改為docker:
chown root:docker Demo/
[root@weltest ~]# chown root:docker Demo/
[root@weltest ~]# ll
總用量 91424
-rw-------. 1 root root 1261 2月 6 17:05 anaconda-ks.cfg
drwxr-xr-x 2 root root 145 2月 8 09:11 cad
drwxr-xr-x 2 root docker 22 2月 11 11:26 Demo
-rw-r--r--. 1 root root 655 2月 6 10:28 modules.sh
-rw------- 1 root root 93609984 2月 10 16:37 tiller2.16.1.tar
[root@weltest ~]#
-
将Demo目錄及其子目錄下的所有檔案的owner都更改為rancher
chown -hR rancher Demo/
[root@weltest ~]# ll | grep Demo
drwxr-xr-x 2 root docker 22 2月 11 11:26 Demo
[root@weltest ~]# ll Demo/
總用量 4
-rw-r--r-- 1 root root 21 2月 11 11:26 hello.sh
[root@weltest ~]# chown -hR rancher Demo/
[root@weltest ~]# ll Demo/
總用量 4
-rw-r--r-- 1 rancher root 21 2月 11 11:26 hello.sh
[root@weltest ~]#
-
将Demo目錄及其子目錄下的所有檔案的owner都更改為rancher,group改為root:
chown -hR rancher:root Demo/
[root@weltest ~]# ll | grep Demo
drwxr-xr-x 2 root docker 22 2月 11 11:26 Demo
[root@weltest ~]# ll Demo/
總用量 4
-rw-r--r-- 1 root root 21 2月 11 11:26 hello.sh
[root@weltest ~]# chown -hR rancher:root Demo/
[root@weltest ~]# ll | grep Demo
drwxr-xr-x 2 rancher root 22 2月 11 11:26 Demo
[root@weltest ~]# ll Demo/
總用量 4
-rw-r--r-- 1 rancher root 21 2月 11 11:26 hello.sh
[root@weltest ~]#
chgrp 修改目錄或使用者的group
- 修改檔案或目錄的group示例:
-
修改目錄Demo的group為docker
chgrp docker Demo/
[root@weltest ~]# ll | grep Demo
drwxr-xr-x 2 rancher root 22 2月 11 11:26 Demo
[root@weltest ~]# chgrp docker Demo/
[root@weltest ~]# ll | grep Demo
drwxr-xr-x 2 rancher docker 22 2月 11 11:26 Demo
[root@weltest ~]# ll Demo/
總用量 4
-rw-r--r-- 1 rancher root 21 2月 11 11:26 hello.sh
[root@weltest ~]#
-
修改目錄Demo及其子檔案的grou為rancher
chgrp -hR rancher Demo/
[root@weltest ~]# ll | grep Demo
drwxr-xr-x 2 rancher docker 22 2月 11 11:26 Demo
[root@weltest ~]# chgrp -hR rancher Demo/
[root@weltest ~]# ll | grep Demo
drwxr-xr-x 2 rancher rancher 22 2月 11 11:26 Demo
[root@weltest ~]# ll Demo/
總用量 4
-rw-r--r-- 1 rancher rancher 21 2月 11 11:26 hello.sh
[root@weltest ~]#
chmod 修改檔案或目錄的權限
- 修改檔案或目錄權限,如果需要給子目錄或子檔案賦予權限需要帶上-R參數,這裡不給出操具體操作示例
-
修改目錄Demo權限為777
chmod 777 Demo/
[root@weltest ~]# ll | grep Demo
drwxr-xr-x 2 rancher rancher 22 2月 11 11:26 Demo
[root@weltest ~]# chmod 777 Demo/
[root@weltest ~]# ll | grep Demo
drwxrwxrwx 2 rancher rancher 22 2月 11 11:26 Demo
[root@weltest ~]#
-
給目錄的group組減去r權限
chmod g-r Demo或者chmod 737 Demo
加的指令為:chmod g+r Demo或者chmod 777 Demo
[root@weltest ~]# ll | grep Demo
drwxrwxrwx 2 rancher rancher 22 2月 11 11:26 Demo
[root@weltest ~]# chmod g-r Demo
[root@weltest ~]# ll | grep Demo
drwx-wxrwx 2 rancher rancher 22 2月 11 11:26 Demo
[root@weltest ~]#
-
給目錄的owner減去r權限
chmod u-r Demo或者chmod 333 Demo
增加r權限為:chmod u+r Demo或者chmod 733 Demo
[root@weltest ~]# ll
總用量 91424
-rw-------. 1 root root 1261 2月 6 17:05 anaconda-ks.cfg
drwxr-xr-x 2 root root 145 2月 8 09:11 cad
drwx-wx-wx 2 rancher rancher 22 2月 11 11:26 Demo
-rw-r--r--. 1 root root 655 2月 6 10:28 modules.sh
-rw------- 1 root root 93609984 2月 10 16:37 tiller2.16.1.tar
[root@weltest ~]# chmod u-r Demo
[root@weltest ~]# ll
總用量 91424
-rw-------. 1 root root 1261 2月 6 17:05 anaconda-ks.cfg
drwxr-xr-x 2 root root 145 2月 8 09:11 cad
d-wx-wx-wx 2 rancher rancher 22 2月 11 11:26 Demo
-rw-r--r--. 1 root root 655 2月 6 10:28 modules.sh
-rw------- 1 root root 93609984 2月 10 16:37 tiller2.16.1.tar
[root@weltest ~]#
-
給目錄的其他使用者減去r權限
chmod o-w Demo或者chmod 331 Demo
增加r權限為:chmod o+w Demo或者chmod 333 Demo
[root@weltest ~]# ll | grep Demo
d-wx-wx-wx 2 rancher rancher 22 2月 11 11:26 Demo
[root@weltest ~]# chmod o-w Demo/
[root@weltest ~]# ll | grep Demo
d-wx-wx--x 2 rancher rancher 22 2月 11 11:26 Demo
[root@weltest ~]# chmod 333 Demo/
[root@weltest ~]# ll | grep Demo
d-wx-wx-wx 2 rancher rancher 22 2月 11 11:26 Demo
[root@weltest ~]#
-
給目錄的owner、grou、其他使用者增加r權限
chmod a+r Demo或者chmod 777 Demo
增加r權限為:chmod a-x Demo或者chmod 666Demoa
[root@weltest ~]# chmod a+r Demo/
[root@weltest ~]# ll
總用量 91424
-rw-------. 1 root root 1261 2月 6 17:05 anaconda-ks.cfg
drwxr-xr-x 2 root root 145 2月 8 09:11 cad
drwxrwxrwx 2 rancher rancher 22 2月 11 11:26 Demo
-rw-r--r--. 1 root root 655 2月 6 10:28 modules.sh
-rw------- 1 root root 93609984 2月 10 16:37 tiller2.16.1.tar
[root@weltest ~]# chmod a-w Demo/
[root@weltest ~]# ll | grep Demo
dr-xr-xr-x 2 rancher rancher 22 2月 11 11:26 Demo
[root@weltest ~]#