天天看點

linux基礎--檔案權限管理

ls -l中顯示的内容如下:

<code>-rwxrw-r‐-1 root root 1213 Feb 2 09:39 file01</code>

- 10個字元确定不同使用者能對檔案幹什麼

- 第一個字元代表檔案(-)、目錄(d),連結(l)

- 其餘字元每3個一組(rwx),讀(r)、寫(w)、執行(x)

- 第一組rwx:(屬主)檔案所有者的權限是讀、寫和執行

- 第二組rw-:(屬組)與檔案所有者同一組的使用者的權限是讀、寫但不能執行

- 第三組r--:(其他使用者)不與檔案所有者同組的其他使用者的權限是讀不能寫和執行

也可用數字表示為:r=4,w=2,x=1  是以rwx=4+2+1=7

- 1 表示連接配接的檔案數

- root 表示使用者屬主

- root表示使用者屬組

- 1213 表示檔案大小(位元組)

- Feb 2 09:39 表示最後修改日期

- file01 表示檔案名

權限管理:

chown、chgrp、chmod、umask

chown:改變檔案屬主和屬組

<code>[root@liang-study home]</code><code># ll yull.txt </code>

<code>-rw-r--r--. 1 root root 0 Dec 14 23:16 yull.txt</code>

<code>[root@liang-study home]</code><code># chown user02:user01 yull.txt </code>

<code>-rw-r--r--. 1 user02 user01 0 Dec 14 23:16 yull.txt</code>

-R:遞歸修改目錄下所有檔案的屬主和屬組

--reference 參考檔案 被修改檔案:将被修改檔案的屬主和屬組修改為參考檔案的屬主和屬組

chgrp:修改檔案的屬組

<code>-rw-r--r--. 1 user02 root 0 Dec 14 23:16 yull.txt</code>

chmod:修改檔案權限

 修改三組使用者權限

<code>[root@liang-study home]</code><code># chmod 755 yull.txt   #修改權限為755</code>

  修改某類使用者的權限

<code>[root@liang-study home]</code><code># chmod g=rwx yull.txt   #修改組權限</code>

<code>[root@liang-study home]</code><code># chmod a=rwx yull.txt    #修改所有權限</code>

<code>[root@liang-study home]</code><code># chmod o=w,u=rwx yull.txt   #修改其他使用者為寫入權限,屬主為讀寫執行</code>

 修改某類使用者的某個權限

<code>[root@liang-study home]</code><code># chmod o+w yull.txt    #給其他使用者添加寫權限</code>

<code>[root@liang-study home]</code><code># chmod o-x yull.txt    #給其他使用者删除執行權限</code>

umask:設定檔案的遮罩碼

什麼是umask

   你的系統管理者必須要為你設定一個合理的 umask值,以確定你建立的檔案具有所希望的預設權限,防止其他非同組使用者對你的檔案具有寫權限。在已經登入之後,可以按照個人的偏好使用umask命 令來改變檔案建立的預設權限。相應的改變直到退出該shell或使用另外的umask指令之前一直有效。一般來說,umask指令是在/etc /profile檔案中設定的,每個使用者在登入時都會引用這個檔案,是以如果希望改變所有使用者的umask,可以在該檔案中加入相應的條目。如果希望永久 性地設定自己的umask值,那麼就把它放在自己$HOME目錄下的.profile或.bash_profile或.bashrc檔案中。

    管理者的umask為:0022

    普通使用者的umask為:0002

          建立檔案的預設權限就是666-umask

          建立目錄的預設權限就是777-umask

    是以,假設使用root使用者登入,umask為0022,則建立檔案的權限的為666-022=644

               假設使用普通使用者登入,umask為0002,則建立檔案的權限的為666-002=664

    可以通過umask來設定umask值。

例子:

<code>[root@liang-study tmp]</code><code># umask 023</code>

<code>[root@liang-study tmp]</code><code># touch a.txt</code>

<code>[root@liang-study tmp]</code><code># mkdir test</code>

<code>[root@liang-study tmp]</code><code># ll</code>

<code>total 4</code>

<code>-rw-r--r-- 1 root root    0 Dec 16 17:26 a.txt</code>

<code>drwxr-xr-- 2 root root 4096 Dec 16 17:26 </code><code>test</code>

注:上例中,我們設定了umask碼為023,是以建立目錄test時權限為777-023=754

  按照前面的叙述,建立檔案a.txt檔案時應為666-023為643,但實際表現為644,這是因為linux預設不允許檔案有執行權限,故如果umask設定為檔案有執行權限,在建立時會自動加1。

本文轉自  亮公子  51CTO部落格,原文連結:http://blog.51cto.com/iyull/1882856

繼續閱讀