檔案和目錄的安全上下文(涉及一些檔案系統)
當我們敲下指令發起一個程序去操作(讀取,複制,修改,删除等)一個目錄或檔案時,程序會根據我們的身份(目錄的屬主,屬組,其他),對照目錄或檔案的屬主,屬組和其他的權限去執行。隻有我們擁有目錄相應的權限,程序才能執行。程序在比對時是有順序的,當你是檔案的屬主,程序便不會去對照我們的屬組身份。
目錄和檔案的權限詳講:
一、目錄的權限詳講:
當執行者對目錄有讀權限時:
執行者可以讀取到目錄内都有哪些檔案,可以看到目錄内的目錄或檔案的名稱,但是對于該目錄下的檔案或目錄的屬性資訊能否看到,取決于執行者對該目錄或檔案的權限。
[redhat tmp]$ cd test
[[email protected] test]$ mkdir test.read
[[email protected] ~]$ chmod 400 /tmp/test/test.read/
[[email protected] test]$ su - root
[[email protected] ~]# cd /tmp/test/test.read/
[[email protected] test.read]# touch a b c
[[email protected] test.read]# ll
總用量 0
-rw-r--r--. 1 root root 0 3月 7 22:59 a
-rw-r--r--. 1 root root 0 3月 7 22:59 b
-rw-r--r--. 1 root root 0 3月 7 22:59 c
[[email protected] test.read]# su - redhat
[[email protected] ~]$ ll /tmp/test/test.read/
ls: 無法通路/tmp/test/test.read/a: 權限不夠
ls: 無法通路/tmp/test/test.read/b: 權限不夠
ls: 無法通路/tmp/test/test.read/c: 權限不夠
總用量 0
?????????? ? ? ? ? ? a
?????????? ? ? ? ? ? b
?????????? ? ? ? ? ? c
仔細看上面不難發現當執行者對目錄僅有讀權限時,是沒有辦法看到目錄内檔案的屬性的,即使你對檔案有讀的權限。
原因是當執行者對目錄僅有都權限時,相當于執行者隻有能向裡看的權限,卻沒有辦法進入。
具體原因是目錄内的目錄名和檔案名儲存在本目錄的block中,(本目錄的block儲存的是本目錄下的檔案名和和檔案名對應的inode号),至于目錄内的檔案和目錄的屬性則記錄在該block所指的inode中。而我們僅有對該目錄的讀的權限,隻能讀到該目錄的block,即該目錄下的檔案名和inode号,而又進入不了該目錄,就無法站在該block上去看該block所指的inode資訊。
[[email protected] test]$ ls -id test.read/
33556153 test.read/
當執行者對目錄有操作的權限時,執行者就可以進入目錄内的檔案進行檢視了。相當于執行者可以進到目錄的門裡面了,當然就可以随便看了。
[[email protected] test]$ chmod 500 /tmp/test/test.read/
[[email protected] test]$ ll /tmp/test/test.read/
總用量 0
-rw-r--r--. 1 root root 0 3月 7 22:59 a
-rw-r--r--. 1 root root 0 3月 7 22:59 b
-rw-r--r--. 1 root root 0 3月 7 22:59 c
但是當執行者沒有寫的權限時,執行者僅僅能進入目錄,看看而已(就像我們去别人的家,進門了,看是可以,要想動動手,還是不可以的。)也就是此時執行者還不能在該目錄下建立,删除,重命名檔案。
[[email protected] test]$ cd test.read/
[[email protected] test.read]$ mkdir woaini
mkdir: 無法建立目錄"woaini": 權限不夠
[[email protected] test.read]$ rm -rf a
rm: 無法删除"a": 權限不夠
[[email protected] test.read]$ mv a A
mv: 無法将"a" 移動至"A": 權限不夠
隻有執行者有了寫的權限時,才能夠進行删除,建立,重命名該目錄下的檔案或目錄。
[[email protected] test]$ chmod 500 /tmp/test/test.read/
[[email protected] test]$ ll /tmp/test/test.read/
總用量 0
-rw-r--r--. 1 root root 0 3月 7 22:59 a
-rw-r--r--. 1 root root 0 3月 7 22:59 b
-rw-r--r--. 1 root root 0 3月 7 22:59 c
[[email protected] test]$ cd test.read/
[[email protected] test.read]$ mkdir woaini
mkdir: 無法建立目錄"woaini": 權限不夠
[[email protected] test.read]$ rm -rf a
rm: 無法删除"a": 權限不夠
[[email protected] test.read]$ mv a A
mv: 無法将"a" 移動至"A": 權限不夠
[[email protected] test.read]$ chmod 700 /tmp/test/test.read/
[[email protected] test.read]$ rm a
rm:是否删除有寫保護的普通空檔案 "a"?y
[[email protected] test.read]$ mkdir A
[[email protected] test.read]$ mv b B
[[email protected] test.read]$ ll
總用量 0
drwxrwxr-x. 2 redhat redhat 6 3月 7 23:36 A
-rw-r--r--. 1 root root 0 3月 7 22:59 B
-rw-r--r--. 1 root root 0 3月 7 22:59 c
二、檔案的權限詳解:
當你對檔案的讀,寫,執行不了解的時候,說明你基本上應該放棄了,Linux的大門基本上就不向你開放了。
三、聯系實際總結:
①删除一個目錄内的檔案,執行者首先要能夠進入目錄,再進入才能删除目錄内的檔案,是以需要具備wx權限。
建立一個檔案的原理同删除。
②複制一個檔案時,就是要讀取一下該檔案,是以有r權限就可以複制檔案了。