字段含義簡述
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!"
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-0qWmz6BU-1581501190169)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\1581495929760.png)]
操作命令
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 ~]#
-