歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux知識:影響文件的讀寫執行的因素

Linux知識:影響文件的讀寫執行的因素

日期:2017/2/28 17:55:37   编辑:Linux教程
之所以把這部份的內容單列出來,是因為這部份的內容是基於我們對用戶管理及文件權限了解的基礎上進行的。比如一個文件的讀、寫、執行,它要受到哪幾方面的影響。
  一個文件能不能被讀取,要受到它的屬主、屬組及其它用戶權限的影響,還要受到其父目錄權限的影響。我們來舉個例子;
[root@localhost ~]# cd /home 注:進入/home 目錄;
[root@localhost home]# mkdir RedHatdir 注:創建一個目錄redhatdir
[root@localhost home]# touch redhatdir/test.txt 注:創建一個文件test.txt
[root@localhost home]# chmod 700 redhatdir/ 注:修改redhatdir的權限,為屬主可讀可寫可執行,屬組和其它用戶無權限;
[root@localhost home]# ls -ld redhatdir/ 注:查看redhatdir的屬性;
drwx------ 2 root root 4096 04-25 13:01 redhatdir/
[root@localhost home]# ls -lr redhatdir/ 注:查看test.txt 文件的屬性;
總計 0
-rw-r--r-- 1 root root 0 04-25 13:02 test.txt
[root@localhost home]# su beinan 注:我們切換到普通用戶beinan
[beinan@localhost home]$ cd redhatdir/ 注:進入redhatdir目錄,以beinan用戶身份。
bash: cd: redhatdir/: 權限不夠
[beinan@localhost home]$ more redhatdir/test.txt
redhatdir/test.txt: 權限不夠

  解釋:我們通過這個例子來看,為什麼test.txt在其它用戶權位上擁有可讀權限r--,但我們用普通用戶還不能查看它的內容呢?這是因為他的父目錄沒有其它用戶的何讀權限。我們是不是redhatdir目錄的其它用戶可讀權限打開,就能讓普通用戶beinan能讀取 test.txt的內容了呢?
[root@localhost home]# chmod 704 redhatdir/
[root@localhost home]# ls -ld redhatdir/
drwx---r-- 2 root root 4096 04-25 13:02 redhatdir
[root@localhost home]# su beinan
[beinan@localhost home]$ cd redhatdir/
bash: cd: redhatdir/: 權限不夠

  看來如果不設置屬組的權限,只打開屬主的權限及其它用戶在redhatdir目錄的讀權限的情況下,其它用戶是不能訪問的;我們應該把test.txt父目錄的 redhatdir 的屬主的讀、寫、執行要打開,還要把父目錄的屬組的讀和執行權限打開,其它用戶的讀和執行權限打開,也就是要擁有 rwxr-xr-x 權限,這樣文件的其它用戶才能訪問。
[root@localhost home]# chmod 755 redhatdir/
[root@localhost home]# more redhatdir/test.txt

  好象這塊說的不太清楚,如果您看不太明白,多多chmod 練習練習,也沒有什麼難的。
  其實為文件分配權限的最終目的是讓文件的屬主有何權限,讓屬組下的用戶有何權限,讓其它用戶有何權限。文件權限是和用戶管理相關聯的,所以理解這方面的內容還得了解用戶管理。
Copyright © Linux教程網 All Rights Reserved