天天看點

檔案或目錄的權限與屬性

   在開始今天的話題之前,我們首先來回顧下ls指令。在ls指令中參數“-l”會顯示出來目标的詳細資訊,如下所示:

<code>[root@server02 ~]# ls -l /tmp/</code>

<code>總用量 4</code>

<code>-rwx------. 1 root root 836 5月  27 06:19 ks-script-ogzDFA</code>

<code>drwxr-xr-x. 5 root root  75 5月  30 05:26 test</code>

<code>drwxr-xr-x. 2 root root   6 5月  30 04:15 test1</code>

<code>drwxr-xr-x. 3 root root  17 5月  30 04:16 test2</code>

<code>-rw-------. 1 root root   0 5月  27 06:10 yum.log</code>

<code>[root@server02 ~]#</code>

    在前幾帖中,我們曾提到過第一列(上圖的“-”和“d”等)标記的是對象的檔案類型。而後面還有10位,最後一位“.”表示開啟了selinux,當關閉後,這個辨別位将會消失。而中間的“rwxr-xr-x”等9位字元标記的是對象的權限。

    這9位字元中,前三位描述的是所有者(如上圖某行的第一個root,表示所有者是root使用者)的權限,中間三位描述的是所屬組(如上圖某行的第二個root,表示所屬組是root組)的權限,最後三位則描述的是除所有者和所屬組外其他人的權限。其中“r”表示可讀,“w”表示可寫,“x”表示可執行,而“-”則表示的是沒有該項權限。

    在使用時,我們把“r”規定值為4,“w”為2,“x”為1,以及“-”為0,如rwx=7、r-x=5等。這樣也就友善我們描述對象的權限。

chmod

1、指令作用

  賦予檔案或目錄權限

2、指令格式

  chmod [options] file

3、參數

  -R 對目前目錄下的所有檔案和子目錄進行相同的權限變更。

4、樣例

<code>[root@server02 test]# ll</code>

<code>總用量 0</code>

<code>-rw-r--r--. 1 root root 0 5月  30 07:03 1.txt</code>

<code>-rw-r--r--. 1 root root 0 5月  30 07:03 2.txt</code>

<code>drwxr-xr-x. 2 root root 6 5月  30 07:04 file1</code>

<code>[root@server02 test]# chmod 777 1.txt</code>

<code>[root@server02 test]# chmod u=rwx,g=rw,o=r 2.txt</code>

<code>[root@server02 test]# chmod g+w file1</code>

<code>-rwxrwxrwx. 1 root root 0 5月  30 07:03 1.txt</code>

<code>-rwxrw-r--. 1 root root 0 5月  30 07:06 2.txt</code>

<code>drwxrwxr-x. 2 root root 6 5月  30 07:04 file1</code>

<code>[root@server02 test]#</code>

注:推薦使用數字的方式賦予權限。

chown

  更改檔案或目錄的所有者或所屬組

  chown [options] file

<code>-rwxrw-r--. 1 root root 0 5月  30 07:03 2.txt</code>

<code>[root@server02 test]# chown user1 1.txt</code>

<code>[root@server02 test]# chown user2:user2 2.txt</code>

<code>[root@server02 test]# chown :user3 file1</code>

<code>-rwxrwxrwx. 1 user1 root  0 5月  30 07:03 1.txt</code>

<code>-rwxrw-r--. 1 user2 user2 0 5月  30 07:03 2.txt</code>

<code>drwxrwxr-x. 2 root  user3 6 5月  30 07:04 file1</code>

注:chgrp指令也可以更改所屬組,因為chown也可以更改,不推薦使用

   通過chown和chgrp兩條指令,我們可以賦予檔案或目錄權限,也可以更換其所有者和所屬組。那麼,當一個新的檔案和目錄被建立的時候,它們的權限是怎麼設定的呢?

<code>[root@server02 test]# mkdir directory</code>

<code>[root@server02 test]# touch 1.txt</code>

<code>-rw-r--r--. 1 root root 0 5月  30 07:29 1.txt</code>

<code>drwxr-xr-x. 2 root root 6 5月  30 07:28 directory</code>

    可以看到預設生成的目錄權限是755,檔案權限是644。這樣設定的原因是,目錄需要執行權限才能cd進入目錄下,而檔案不需要執行權限,這樣更安全些。而完成這個權限設定的功臣就是linux系統内的umask值。

<code>[root@server02 test]# umask</code>

<code>0022</code>

   umask值預設為0022。目錄采用777(rwxrwxrwx),檔案采用666(rw-rw-rw-)的初始值,和umask值通過減權限(不是減法)的方式運算出目錄和檔案的初始權限。

<code>例1:umask 002    002&lt;-------w-&gt;  </code>

<code>當建立一個檔案   666&lt;rw-rw-rw-&gt; - 002&lt;-------w-&gt; = 664&lt;rw-rw-r--&gt;</code>

<code>當建立一個目錄   777&lt;rwxrwxrwx&gt; - 002&lt;-------w-&gt; = 775&lt;rwxrwxr-x&gt;</code>

<code>例2: umask 033   033&lt;----wx-wx&gt;</code>

<code>當建立一個檔案   666&lt;rw-rw-rw-&gt; - 033&lt;----wx-wx&gt; = 644&lt;rw-r--r--&gt;</code>

<code>當建立一個目錄   777&lt;rwxrwxrwx&gt; - 033&lt;----wx-wx&gt; = 744&lt;rwxr--r--&gt;</code>

chattr

  改變檔案或目錄屬性

  chattr [options] file

  -i 不可以增加、追加、删除和更改,重命名任何内容和資訊。

  -a 隻可以追加内容,不可以删除、更改和重命名。

<code>[root@server02 test]# chattr +i 1.txt</code>

<code>[root@server02 test]# chattr +a 2.txt</code>

<code>[root@server02 test]# chattr -i 1.txt</code>

lsattr

  檢視檔案或目錄屬性

  lsattr [options] file

  -d 列出目錄的屬性資訊。

  -a 列出目前檔案或目錄下的所有檔案(包含隐藏檔案)的屬性資訊。

  -R 列出目錄下的所有檔案和子目錄及其下的所有檔案的屬性資訊。

<code>[root@server02 test]# lsattr</code>

<code>---------------- ./directory</code>

<code>----i----------- ./1.txt</code>

<code>-----a---------- ./2.txt</code>

<code>----i----------- ./dir1</code>

<code>[root@server02 test]# lsattr -a</code>

<code>---------------- ./.</code>

<code>---------------- ./..</code>

<code>---------------- ./.3.txt</code>

<code>[root@server02 test]# lsattr -d</code>

<code>---------------- .</code>

<code>[root@server02 test]# lsattr -R directory</code>

<code>-----a---------- directory/test.txt</code>

<code>---------------- directory/dir2</code>

<code>directory/dir2:</code>

<code>-----a---------- directory/dir2/100.txt</code>

<code></code>

本文轉自Grodd51CTO部落格,原文連結:http://blog.51cto.com/juispan/1933264,如需轉載請自行聯系原作者

繼續閱讀