天天看點

Linux基礎之檔案權限詳解

Linux中對于權限的制定雖然沒有Windows的那麼精細,但是如果你了解并掌握Linux中檔案的權限知識,也可以像Windows那樣對權限做到精确配置。

Linux中的檔案權限是什麼?

如何檢視Linux中的檔案權限

[root@localhost test]# ll -d /test/

drwxr-xr-x. 2 root root 52 8月   7 20:18 /test/

上面的rwxr-xr-x即為檔案的權限位共九位。下面分别對其進行介紹。

                rwx∣r-x∣r-x

                ↓    ↓   ↓

                屬主 屬組 其他

前三個為屬主位:建立該檔案者或被指定的檔案所屬者

中間三個為屬組位:檔案的所屬組,在該組内的非屬主使用者對該檔案擁有該屬組權限。

最後三個Other位:other使用者,既不屬于屬主又不在屬組的使用者

          r:讀權限    w:寫權限    x:執行權限

檔案中rwx的具體含義:

      r:可以使用類似cat等指令檢視檔案内容

      w:可以編輯或删除此檔案

      x:可以在指令提示符下當做指令送出給核心運作

目錄中rwx的具體含義:

      r:可以對此目錄執行ls以列出内部的所有檔案

      w:可以在此目錄建立檔案:

      x:可以使用cd切換進此目錄,也可以使用ls -l檢視内部檔案的詳細資訊

下面請看一個對應關系

          000 ---  對應十進制0

          001 --x  對應十進制1

          010 -w-  對應十進制2

          011 -wx  對應十進制3

          100 r--  對應十進制4

          101 r-x  對應十進制5

          110 rw-  對應十進制6

          111 rwx  對應十進制7

上面rwx三位與三位二進制對應,是以權限也可以用數字表達

比如:

  755代表rwxr-xr-x  664代表rw-rw-r--

管理Linux中的檔案權限:

chmod chown chgrp umask

chmod  修改檔案權限位指令

chmod - change file mode bits

表達格式:

       chmod [OPTION]... MODE[,MODE]... FILE...

       chmod [OPTION]... OCTAL-MODE FILE...

       chmod [OPTION]... --reference=RFILE FILE...

常用選項:

      -R 遞歸,将設定的權限應用到下面的所有檔案

1、chmod [OPTION]... MODE[,MODE]... FILE...

賦權表示法:u=屬主  g=屬組  o=其他  a=所有

直接操作一類使用者的所有權限位 rwx

    寫法:u=rwx

1

2

3

4

5

6

7

8

9

10

11

12

13

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

<code>總用量 16</code>

<code>-rw-r--r--. 1 root root 43 8月   7 16:46 cat1</code>

<code>-rw-r--r--. 1 root root 19 8月   7 16:46 cat2</code>

<code>-rw-r--r--. 1 root root 57 8月   7 19:34 </code><code>head</code>

<code>-rw-r--r--. 1 root root 55 8月   7 20:18 siting</code>

<code>[root@localhost </code><code>test</code><code>]</code><code># chmod u=rwx,g=rwx cat1 </code>

<code>-rwxrwxr--. 1 root root 43 8月   7 16:46 cat1</code>

同時更改多個所屬對象權限,中間用“,”隔開

授權表示法:直接操作一類使用者的一個權限為r,w,x

寫法:u+(r|w|x) u-(r|w|x) g+(r|w|x) g-(r|w|x) o+(r|w|x) o-(r|w|x)

a+(r|w|x) a-(r|w|x)

<code>[root@localhost </code><code>test</code><code>]</code><code># chmod u+x,g+w cat2</code>

<code>-rwxrw-r--. 1 root root 19 8月   7 16:46 cat2</code>

2、chmod [OPTION]... OCTAL-MODE FILE...

<code>[root@localhost </code><code>test</code><code>]</code><code># chmod 755 head </code>

<code>-rwxr-xr-x. 1 root root 57 8月   7 19:34 </code><code>head</code>

3、chmod [OPTION]... --reference=RFILE FILE... 指定目标檔案與所指檔案的權限一緻(不常用)

<code>[root@localhost </code><code>test</code><code>]</code><code># chmod --reference=cat1 siting  </code>

<code>-rwxrwxr--. 1 root root 55 8月   7 20:18 siting</code>

siting與cat1檔案的權限保持一緻

chown 修改屬主屬組

chown - change file owner and group

       chown [OPTION]... [OWNER][:[GROUP]] FILE...

       chown [OPTION]... --reference=RFILE FILE...

    -R 遞歸修改該

1、chown [OPTION]... [OWNER][:[GROUP]] FILE...

<code>[root@localhost </code><code>test</code><code>]</code><code># chown gentoo:fedore cat1</code>

<code>-rwxrwxr--. 1 gentoo fedore 43 8月   7 16:46 cat1</code>

<code>-rwxrw-r--. 1 root   root   19 8月   7 16:46 cat2</code>

<code>-rwxr-xr-x. 1 root   root   57 8月   7 19:34 </code><code>head</code>

<code>-rwxrwxr--. 1 root   root   55 8月   7 20:18 siting</code>

2、chown [OPTION]... --reference=RFILE FILE...

<code>[root@localhost </code><code>test</code><code>]</code><code># chown --reference cat1 cat2</code>

<code>-rwxrw-r--. 1 gentoo fedore 19 8月   7 16:46 cat2</code>

因為chown既可以改屬主又可以改屬組是以下面這個chgrp指令就被打入冷宮,為了緬懷一下它,這裡還是簡要介紹下

chgrp - change group ownership 修改屬組

       chgrp [OPTION]... GROUP FILE...

       chgrp [OPTION]... --reference=RFILE FILE...

14

<code>[root@localhost </code><code>test</code><code>]</code><code># chgrp gentoo head </code>

<code>-rwxr-xr-x. 1 root   gentoo 57 8月   7 19:34 </code><code>head</code>

<code>[root@localhost </code><code>test</code><code>]</code><code># chgrp --reference cat1 siting </code>

<code>-rwxrwxr--. 1 root   fedore 55 8月   7 20:18 siting</code>

umask :檔案的權限反向掩碼,俗稱遮罩碼

作用:它是為了控制預設權限,不要使預設的檔案和目錄具有全權而設的

    檔案:666-umask

    目錄:777-umask

注:之是以檔案用666去減,表示檔案預設不能擁有執行權限,如果減得的結果中有執行權限,則需+1

umask:檢視目前umask

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

<code>0022</code>

umask MASK :設定umask  僅對目前shell程序有效

若要長期修改umask的值,可以把它寫進/etc/profile(全局有效)或~/.profile(個人)或~/.bash_profile中

<code>[root@localhost </code><code>test</code><code>]</code><code># umask 0002</code>

<code>0002</code>

<code>[root@localhost </code><code>test</code><code>]</code><code># touch umask1</code>

<code>-rw-rw-r--. 1 root   root    0 8月   8 20:49 umask1</code>

使用root使用者建立一個新檔案umask1其權限為664,umask為0002,其建立檔案的權限符合我們的設定:666-002=664。

本文轉自 紫色的茶碗 51CTO部落格,原文連結:http://blog.51cto.com/chawan/1835800,如需轉載請自行聯系原作者

繼續閱讀