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

[root@localhost test]# ll
總用量 16
-rw-r--r--. 1 root root 43 8月   7 16:46 cat1
-rw-r--r--. 1 root root 19 8月   7 16:46 cat2
-rw-r--r--. 1 root root 57 8月   7 19:34 head
-rw-r--r--. 1 root root 55 8月   7 20:18 siting
[root@localhost test]# chmod u=rwx,g=rwx cat1 
[root@localhost test]# ll
總用量 16
-rwxrwxr--. 1 root root 43 8月   7 16:46 cat1
-rw-r--r--. 1 root root 19 8月   7 16:46 cat2
-rw-r--r--. 1 root root 57 8月   7 19:34 head
-rw-r--r--. 1 root root 55 8月   7 20:18 siting

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

授權表示法:直接操作一類用戶的一個權限爲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)

[root@localhost test]# chmod u+x,g+w cat2
[root@localhost test]# ll
總用量 16
-rwxrwxr--. 1 root root 43 8月   7 16:46 cat1
-rwxrw-r--. 1 root root 19 8月   7 16:46 cat2
-rw-r--r--. 1 root root 57 8月   7 19:34 head
-rw-r--r--. 1 root root 55 8月   7 20:18 siting


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

[root@localhost test]# chmod 755 head 
[root@localhost test]# ll
總用量 16
-rwxrwxr--. 1 root root 43 8月   7 16:46 cat1
-rwxrw-r--. 1 root root 19 8月   7 16:46 cat2
-rwxr-xr-x. 1 root root 57 8月   7 19:34 head
-rw-r--r--. 1 root root 55 8月   7 20:18 siting


3、chmod [OPTION]... --reference=RFILE FILE... 指定目標文件與所指文件的權限一致(不常用)

[root@localhost test]# chmod --reference=cat1 siting  
[root@localhost test]# ll
總用量 16
-rwxrwxr--. 1 root root 43 8月   7 16:46 cat1
-rwxrw-r--. 1 root root 19 8月   7 16:46 cat2
-rwxr-xr-x. 1 root root 57 8月   7 19:34 head
-rwxrwxr--. 1 root root 55 8月   7 20:18 siting

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...

[root@localhost test]# chown gentoo:fedore cat1
[root@localhost test]# ll
總用量 16
-rwxrwxr--. 1 gentoo fedore 43 8月   7 16:46 cat1
-rwxrw-r--. 1 root   root   19 8月   7 16:46 cat2
-rwxr-xr-x. 1 root   root   57 8月   7 19:34 head
-rwxrwxr--. 1 root   root   55 8月   7 20:18 siting

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

[root@localhost test]# chown --reference cat1 cat2
[root@localhost test]# ll
總用量 16
-rwxrwxr--. 1 gentoo fedore 43 8月   7 16:46 cat1
-rwxrw-r--. 1 gentoo fedore 19 8月   7 16:46 cat2
-rwxr-xr-x. 1 root   root   57 8月   7 19:34 head
-rwxrwxr--. 1 root   root   55 8月   7 20:18 siting


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


chgrp - change group ownership 修改屬組

表達格式:

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

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

[root@localhost test]# chgrp gentoo head 
[root@localhost test]# ll
總用量 16
-rwxrwxr--. 1 gentoo fedore 43 8月   7 16:46 cat1
-rwxrw-r--. 1 gentoo fedore 19 8月   7 16:46 cat2
-rwxr-xr-x. 1 root   gentoo 57 8月   7 19:34 head
-rwxrwxr--. 1 root   root   55 8月   7 20:18 siting
[root@localhost test]# chgrp --reference cat1 siting 
[root@localhost test]# ll
總用量 16
-rwxrwxr--. 1 gentoo fedore 43 8月   7 16:46 cat1
-rwxrw-r--. 1 gentoo fedore 19 8月   7 16:46 cat2
-rwxr-xr-x. 1 root   gentoo 57 8月   7 19:34 head
-rwxrwxr--. 1 root   fedore 55 8月   7 20:18 siting



umask :文件的權限反向掩碼,俗稱遮罩碼

作用它是爲了控制默認權限,不要使默認的文件和目錄具有全權而設的

    文件:666-umask

    目錄:777-umask

:之所以文件用666去減,表示文件默認不能擁有執行權限,如果減得的結果中有執行權限,則需+1


umask:查看當前umask

[root@localhost test]# umask
0022

umask MASK :設置umask  僅對當前shell進程有效

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

[root@localhost test]# umask 0002
[root@localhost test]# umask
0002
[root@localhost test]# touch umask1
[root@localhost test]# ll
總用量 16
-rwxrwxr--. 1 gentoo fedore 43 8月   7 16:46 cat1
-rwxrw-r--. 1 gentoo fedore 19 8月   7 16:46 cat2
-rwxr-xr-x. 1 root   gentoo 57 8月   7 19:34 head
-rwxrwxr--. 1 root   fedore 55 8月   7 20:18 siting
-rw-rw-r--. 1 root   root    0 8月   8 20:49 umask1

使用root用戶創建一個新文件umask1其權限爲664,umask爲0002,其新建文件的權限符合我們的設定:666-002=664。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章