天天看點

Linux之檔案與目錄權限差别

Linux是一個多使用者多任務系統。而為了讓各個使用者具有較保密的檔案資料,是以檔案的權限管理就變的很重要了。Linux一般将檔案可存取的身份分為三個類别,分别是owner(檔案擁有者)/group(群組使用者)/others(其他使用者),且三種身份各有read/write/execute等權限。

[[email protected]_0_10_centos test]$ ls -l
總用量 8
drwxr-xr-x 2 root root 4096 4月  12 14:37 dir
-rw-r--r-- 1 root root    5 4月  12 14:47 file
           

以dir目錄為例 drwxr-xr-x 2 root root 4096 4月 12 14:37 dir

  • 第一列(drwxr-xr-x):第一位為檔案類型。當為[d]則是目錄,當為[-]則是檔案;若是[l]則表示為連結檔案(linkfile);若是[b]則表示為裝置檔案裡面的可供儲存的周邊裝置(可随機存取裝置);若是[c]則表示為裝置檔案裡面的序列埠裝置,例如鍵盤、滑鼠(一次性讀取裝置)。

    其餘每三位為一組(順序固定就是以rwx為順序,如果某位有該權限就顯示對應的字母,沒有則顯示-),第一組為owner所屬權限即rwx;第二組為group所屬權限即r-x;第三組為other所屬權限即r-x。

  • 第二列 (2)為該檔案或目錄的連接配接數
  • 第三列(root)為該檔案或目錄的擁有者
  • 第四列(root)為該檔案或目錄的所屬群組
檔案權限之rwx

檔案的權限都是針對于檔案的内容而言:

  • r(read):可讀取此一檔案的實際内容,如讀取文本檔案的文字内容等;
  • w(write):可以編輯、新增或者是修改該檔案的内容(但不含删除該檔案);
  • x(execute):該檔案具有可以被系統執行的權限。
目錄權限之rwx

目錄的主要内容相當于記錄檔案名的清單。目錄就相當于抽屜,而檔案就相當于抽屜裡的檔案夾。

  • r(read contents in directory) : 表示具有讀取該目錄下的檔案名資料。
  • w(modify contents of directory): 表示具有改動該目錄下檔案名的權限。即:
    • 建立新的檔案與目錄;
    • 删除已經存在的檔案與目錄(不論該檔案的權限為何!)
    • 将已存在的檔案或目錄進行更名;
    • 搬移該目錄内的檔案、目錄位置。總之,目錄的w權限就與該目錄下面的檔案名異動有關就對了
  • x (access directory):表示能否進入該目錄作為工作目錄。
[[email protected]_0_10_centos test]$ ls -l
total 4
drwxr-xr-x 2 root root 4096 Apr 12 14:19 dir 
-rw-r--r-- 1 root root    0 Apr 11 16:00 file
[[email protected]_0_10_centos test]$ echo "haha" >> file
-bash: file: 權限不夠      //file檔案的other使用者隻具有r權限,故隻能檢視檔案内容,而無法修改其内容
[[email protected]_0_10_centos test]$ rm file
rm: remove write-protected regular empty file ‘file’? y
rm: cannot remove ‘file’: Permission denied  //test檔案夾的other隻具有rx權限,故無法删除修改test目錄下的檔案
[[email protected]_0_10_centos test]$ cd dir/        //dir目錄的other使用者具有x權限,故可以進入該目錄
[[email protected]_0_10_centos dir]$ touch file   
touch: cannot touch ‘file’: Permission denied   //dir目錄的other使用者沒有具有w權限,是以不可修改該目錄下的檔案名結構,即新增删除等。

[[email protected]_0_10_centos test]$ sudo chmod o=r dir  //将dir檔案夾的other使用者權限改為r
[[email protected]_0_10_centos test]$ ls -l
total 4
drwxr-xr-- 2 root root 4096 Apr 12 14:37 dir
-rw-r--r-- 1 root root    0 Apr 11 16:00 file
cd dir/                       
bash: cd: dir/: Permission denied    //因為dir檔案夾other使用者不具有x權限故無法進入該目錄
[[email protected]_0_10_centos test]$ ls -l dir
ls: cannot access dir/file2: Permission denied
total 0
-????????? ? ? ? ?            ? file2     //  //因為dir檔案夾other使用者隻具有r權限而不具有x權限,隻能讀取該檔案夾下的檔案名而無法擷取詳細的檔案資訊
[[email protected]_0_10_centos test]$ ls dir
ls: cannot access dir/file2: Permission denied
file2

[[email protected]_0_10_centos test]$ sudo chown hml dir   //将目錄的所屬者改為hml使用者
[[email protected]_0_10_centos test]$ sudo chown hml file  //将檔案的所屬者改為hml使用者
[[email protected]_0_10_centos test]$ ls -l
total 4
drwxr-xr-x 2 hml root 4096 Apr 12 14:19 dir
-rw-r--r-- 1 hml root    0 Apr 11 16:00 file
[[email protected]_0_10_centos test]$ echo "haha" >> file     //file檔案所屬者具有w權限,故可修改其内容
[[email protected]_0_10_centos test]$ cat file
haha
[[email protected]_0_10_centos test]$ touch dir/file   //dir目錄具有w權限,故可以新增删除等操作
[[email protected]_0_10_centos test]$ ls dir
file  file2
[[email protected]_0_10_centos test]$ rm dir/file 
[[email protected]_0_10_centos test]$ ls dir
file2

           

繼續閱讀