參考鳥哥私房菜:http://vbird.dic.ksu.edu.tw/
先說一件比較坑的事:前幾天我在使用公司的一臺ftp服務器的時候,手賤在根目錄下執行了一下代碼
sudo chmod 777 / -R
duang 一下,系統崩了。root進不去了,各種各樣的問題接踵而至,補救方法試了很多,但都不行。最後無奈的重新安裝了系統。幸好沒有什麼重要的東西。
出現這種問題的原因:對文件的權限管理一知半解,只知道設置了777權限之後,就可以高枕無憂的使用。最終導致不可饒術的錯誤。只能說一知半解害死人,學藝不精坑死人。
樓主通定思定,於是重新學習了對文件的權限的管理這一塊兒。
首先對權限管理的命令:
chgrp //設置組的權限 chown //設置用戶和組的權限 chmod //設置讀寫執行權限
先查看一下/root 目錄下文件的權限:
[root@localhost ~]# ls -ll install.log -rw-r--r--. 1 root root 37332 11月 30 21:11 install.log 打印信息第一部分: -rw-r--r-- //第1位表示文件類型,這裏表示普通文件,如果是d的話代表目錄,如果是l表示鏈接 2-4位,表示文件所屬用戶的權限,這裏rw-,表示所屬用戶擁有讀寫權限 5-7位,表示文件所屬的組的權限,這裏r--,表示所屬組擁有隻讀權限 8-10位,表示除用戶和組外其他的用戶和組的權限,這裏r--,表示其他用戶和組擁有隻讀權限 可以這樣表示: type user group other 類型 用戶 組 其他 第二部分:1 表示文檔鏈接節點有1個 第三部分:root root 表示該文件的用戶爲root,所屬的組爲root 第四部分:37332 表示該文件大小爲37332字節 第五部分:11月 30 21:11 表示時間 第六部分:install.log 表示文件名 文件信息完整格式: 文件類型 文件權限 文件鏈接節點 所屬用戶 所屬組 文件大小 最後一次編輯時間 文件名
通過權限管理命令來更改權限:
chgrp更改文件所屬組: [root@localhost ~]# chgrp test install.log [root@localhost ~]# ls -ll install.log -rw-r--r--. 1 root test 37332 11月 30 21:11 install.log 文件所屬組爲test,所以test組的成員對install.log文件有讀的權限。
chown更改文件所屬的用戶:
[root@localhost ~]# chown test install.log [root@localhost ~]# ls -ll install.log -rw-r--r--. 1 test test 37332 11月 30 21:11 install.log 文件所屬用戶爲test.test用戶對install.log文件有讀寫權限
chown同時更改用戶和組:
[root@localhost ~]# chown root:root install.log [root@localhost ~]# ls -ll install.log -rw-r--r--. 1 root root 37332 11月 30 21:11 install.log 通過執行,chown user:group filename 同時更改用戶和組
chmod 更改文件的權限
r代表對文件可讀,w代表對文件可寫,x代表對文件可執行 chmod 命令格式: chmod u=rwx,g=rwx,o=rwx filename //u表示用戶,g表示組,o表示其他 chmod +x filename //增加可執行權限 chmod -x filename //減去可執行權限 [root@localhost ~]# ls -ll install.log -rw-r--r--. 1 root root 37332 11月 30 21:11 install.log [root@localhost ~]# chmod u+x,g+x,o+x install.log [root@localhost ~]# ls -ll install.log -rwxr-xr-x. 1 root root 37332 11月 30 21:11 install.log //增加了,可執行權限 [root@localhost ~]# chmod -x install.log [root@localhost ~]# ls -ll install.log -rw-r--r--. 1 root root 37332 11月 30 21:11 install.log //減少了可執行權限 用數字方式更改權限: rwx 分別用4,2,1表示, 4+2+1 = 7 //全部權限 4+2 = 6 //可讀,可寫 4 + 1 = 5 //可讀,可執行 2 + 1 = 3 //可寫可執行 chmod 777 filename //用戶,組,其他都是可讀,可寫,可執行 chmod 755 filename //用戶可讀,可寫,可執行,組和其他可讀,可執行 [root@localhost ~]# chmod 777 install.log [root@localhost ~]# ls -ll install.log -rwxrwxrwx. 1 root root 37332 11月 30 21:11 install.log //恢復原來權限 [root@localhost ~]# chmod 644 install.log [root@localhost ~]# ls -ll install.log -rw-r--r--. 1 root root 37332 11月 30 21:11 install.log 文件和目錄權限功能區別: file rwx //讀寫執行 r讀 代表可以讀寫文件的內容 w寫 可以編輯、新增或者是修改該檔案的內容(但不能刪除該文件) x執行 文件可以做爲二進制文件來執行,僅僅有這樣的權限,執行是否成功,要看文件內容 dir rwx //讀寫執行 r 代表可以讀寫文件的內容 w 建立新文件與目錄 刪除已經存在的文件與目錄 更改文件或者目錄的名字 移動文件或者目錄到別的目錄 x 代表可以進入文件