父目錄的權限對子目錄有沒有影響?[Linux]


問題源頭:

  登錄到服務器(實驗室分的一個服務器賬號)上,想在當前目錄下創建一個文件,但提示“文件系統只讀”,無法創建文件。通過ls -l查看當前用戶在當前目錄的權限,發現具有rwx權限。所以在想會不會是因為我在父目錄中沒有w權限導致我無法在當前目錄創建文件。但發現在父目錄中我也是rwx權限...(最后,我感覺這個問題可能是服務器管理員對硬盤加了寫保護) 

  於是我做了以下的實驗,想驗證"父目錄的權限對子目錄的權限有沒有影響?"  例如,某用戶在父目錄(testDir)沒有寫權限,那么該用戶在當前目錄(testDir/abc/)能否執行寫操作?

lxw ~$ ls -ld testDir/
drwxrwxr-x 3 lxw lxw 4096 Dec 26 09:10 testDir/
lxw ~$ chmod 500 testDir/
lxw ~$ ls -ld testDir/ dr-x------ 3 lxw lxw 4096 Dec 26 09:10 testDir/
lxw ~$ cd testDir/
lxw testDir$ touch parentFile  #合理,對testDir無w權限
touch: cannot touch ‘parentFile’: Permission denied
lxw testDir$ ls -l
total 8
drwxrwxr-x 2 lxw lxw 4096 Dec 26 10:07 abc
-rw-rw-r-- 1 lxw lxw    4 Dec 26 09:10 txt
lxw testDir$ cd abc/
lxw abc$ ls
lxw abc$ touch childFile     #父目錄雖然沒有w權限,但只要我能夠進入當前目錄(具有x權限),我就能夠按照我在當前目錄的權限,進行操作。
lxw abc$ ls
childFile

  所以,經過上面的驗證,父目錄雖然沒有w權限,但只要我能夠進入當前目錄(具有x權限),我就能夠按照我在當前目錄的權限,進行讀寫操作。

  現在我讓testDir/目錄只保留x權限:

lxw ~$ chmod 100 testDir/
lxw ~$ ll -d testDir/ d--x------ 3 lxw lxw 4096 Dec 26 09:10 testDir//
lxw ~$ cd testDir/
lxw testDir$ ls       #合理, 沒有r權限
ls: cannot open directory .: Permission denied
lxw testDir$ ll -d abc   #雖然我在testDir/沒有r權限,無法看其有哪些文件,但若我事先已知道它有哪些文件,我就可以通過ls對這些文件進行查看。
drwxrwxr-x 2 lxw lxw 4096 Dec 26 10:25 abc/
lxw testDir$ ll txt       #同上
-rw-rw-r-- 1 lxw lxw 4 Dec 26 09:10 txt
lxw testDir$ touch a     #合理, 沒有w權限
touch: cannot touch ‘a’: Permission denied
lxw testDir$ cd abc
lxw abc$ ls
childFile
lxw abc$ ll -ld .
drwxrwxr-x 2 lxw lxw 4096 Dec 26 10:13 ./
lxw abc$ touch childFile2  #雖然我在父目錄中沒有rw權限,但只要我能進入該目錄(x權限),我就可以按照我在當前目錄中的權限進行操作。
lxw abc$ ls -l
total 0
-rw-rw-r-- 1 lxw lxw 0 Dec 26 10:13 childFile
-rw-rw-r-- 1 lxw lxw 0 Dec 26 10:25 childFile2

  所以,通過上面的實驗,我認為,只要父目錄具有x權限,父目錄的權限和子目錄的權限沒有直接的關系,不會相互影響

  最后,通過下面的例子進一步說明:

lxw ~$ chmod 600 testDir/
lxw ~$ ls -ld testDir/ drw------- 3 lxw lxw 4096 Dec 26 09:10 testDir/
lxw ~$ cd testDir/    #合理,沒有x權限
bash: cd: testDir/: Permission denied
lxw ~$ ls -l testDir/   #有r權限
ls: cannot access testDir/txt: Permission denied
ls: cannot access testDir/abc: Permission denied
total 0
d????????? ? ? ? ?            ? abc
-????????? ? ? ? ?            ? txt
lxw ~$ cd testDir/abc bash: cd: testDir/abc: Permission denied

  最后一條語句, 我對abc目錄具有rwx權限,對testDir具有rw權限,但我無法進入testDir/abc(父目錄的權限影響到了子目錄的權限),原因正是我對testDir沒有x權限。

  如果您有不同的觀點,歡迎您與我交流。

 


免責聲明!

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



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