Linux文件系統權限管理基礎知識

一 .權限對象

  1. owner: 文件擁有者
  2. group:用戶組
  3. others:非文件擁有者,或者是用戶組以外的人,其他人.

二. 權限類型 rwx

[root@xiaoagiao tmp]# ls -l
total 12
-rw-r--r--. 1 root root  886 Mar  1 00:20 ac
-rw-r--r--. 1 root root    0 Mar  7 04:47 file1
-rw-r--r--. 1 root root    0 Mar  7 04:47 file2
-rw-r--r--. 1 root root  886 Mar  1 00:20 fstab
drwx-w-r-x. 3 root root 4096 Mar  9 00:46 log

例如 :rw-r–r--r-- (一共九位)
每三位爲一組:

  • 前三位:屬主
  • 中間三位:屬組
  • 後三位 : 其他人

三. rwx

1. 對於文件而言

  • r:可以獲取文件的數據
  • w:可以修改文件的數據
  • x:可以將此文件運行成爲進程

2. 對於目錄而言

  • r:可以使用ls命令獲取目錄下的文件列表
  • w:可以修改此目錄下的文件列表,可以創建或者刪除文件
  • x:可以cd到此目錄下,並且可以使用ls -l來獲取文件的詳細屬性信息

3. rwx可用數字代表

  • r : 4 (讀)
  • w :2(寫)
  • x :1(執行)

文件權限判斷邏輯

  • 文件的創建與刪除—>(文件所在目錄有W權限)
  1. 判斷用戶的身份,用戶以什麼身份去刪除文件,owner—>group—>others
  2. 如果是目錄的屬主:目錄權限位的左三位需要有w權限,若有則成功,無則失敗
  3. 如果不是目錄的屬主,是屬組,目錄權限位中三位需要有w權限,若有則成功,無則失敗
  4. 如果不是目錄屬於其他人,目錄權限位中三位需要有w權限,若有則成功,無則失敗
  • 文件內容的增加與刪除—>(文件本身需要有w權限)
  1. 判斷用戶身份。

  2. 對應權限,如果有則成功,無則失敗。


四.權限管理類命令

1. chmod

  • 三類用戶 (字母u,g o a 代表)
    u:屬主
    g:屬組
    o:其他人
    a:所有

  • 賦權表示法:rwx
    u=
    g=
    o=
    a=

chmod u=r-x file  //文件file屬主權限改爲r-x
chmod g=--x file  //文件file屬組權限改爲--x
chmod o=rwx file  //文件file其他人權限改爲rwx
chmod a=rwx file  //文件file所有權限改爲rwx
  • 授權表示法
    u+,u-
    g+,g-
    o+,o-
    a+,a-
chmod u+r file  //文件file屬主權限有r權限
chmod g+w file  //文件file屬組權限有w權限
chmod o+x file  //文件file其他人權限有x權限
chmod a+r file  //文件file所有權限有r權限
  • -R,遞歸修改
chmod  -R 777 file //file目錄下所有文件的權限改爲777。
chmod -R u=rwx file //file目錄下所有文件屬主權限改爲rwx。

2.chown修改文件的屬主或屬組

chown owner : group file

chown  owner : group file //file文件的屬主改爲owner,屬組改爲group。

chown owner file

chown owner file//file文件的屬主改爲owner。

chown :group file

chown :group file //file文件的屬組改爲group。

chown -R owner : group file

chown -R owner : group file //file目錄下所有文件的屬主改爲owner,屬組改爲group。

3.chgrp修改文件屬組

chgrp jerry file

chgrp jerry file //file文件的屬組改爲jerry。

只有管理員用戶可以修改文件或目錄的屬組,屬主和其他人


五. 進程的安全上下文

  • 進程對文件訪問權限的應用模型
  1. 進程的屬主,與文件的屬主是否相同,如果相同,則應用屬主權限
    否則,檢查進程的屬組是否和文件的屬組相同,如果相同,則應用屬組的權限
    否則,使用other權限。
  2. 將一個文件運行成爲一個進程,取決於用戶是否對這個文件有x權限,可執行權限

六 ,ALC訪問控制列表

定義: ACL(Access control list),即訪問控制列表,它是一系列規則的集合,ACL通過這些規則對不同的報文分類,進而對不同的報文進行不同的處理。

要查看acl列表可以使用: getfacl filename

[root@xiaoagiao file3]# getfacl aa
#file: aa
#owner: root
#group: root
user::rw-
group::r--
other::r--
  • 顯示的數據前面帶 # 的表示是文件的默認屬性,一二三行就是這個文件的默認屬性;
  • 第四行用戶名欄是空的這代表該文件所有者的權限;
  • 第五行用戶組欄是空的,這代表該文件的所屬用戶組的 權限;
  • 第六行表示該文件的其他人權限。

acl列表的管理

setfacl

選項:

-m 配置acl
-x 刪除指定的匹配規則
-b 關閉在該文件上開啓的acl列表,同時刪除所有的acl參數
-R 遞歸操作
  • -m–>setfacl -m u:user1:rwx file
[root@xiaoagiao file3]# getfacl aa
#file: aa
#owner: root
#group: root
user::rw-
group::r--
other::r--

[root@xiaoagiao file3]# ll -l aa
-rw-r--r--. 1 root root 0 Mar 12 05:00 aa
[root@xiaoagiao file3]# setfacl -m u:user1:rwx aa
[root@xiaoagiao file3]# ll -l aa
-rw-rwxr--+ 1 root root 0 Mar 12 05:00 aa
//權限最後出現了加號,代表開啓了acl。

[root@xiaoagiao file3]# getfacl aa
#file: aa
#owner: root
#group: root
user::rw-
user:user1:rwx  //針對user這個用戶的權限是rwx,它就是我們通過setfacl 命令設置的  acl 規則
group::r--
mask::rwx  //表示此文件的權限閥值,即 所設置的用戶和組的權限必須在mask所定的權限最大值以內纔會生效
other::r--
  • setfacl -m g:jerry:rwx file.acl
    指定jerry這個組的用戶對file.acl這個文件有rwx的權限.
  • setfacl -x u:user1 file
    —>刪除指定的用戶user1 的匹配規則
[root@xiaoagiao file3]# getfacl aa
#file: aa
#owner: root
#group: root
user::rw-
user:user1:rwx   //刪除指定的用戶user1 的匹配規則
group::r--
group:jerry:rw-
mask::rwx
other::r--

[root@xiaoagiao file3]# setfacl -x u:user1 aa
[root@xiaoagiao file3]# getfacl aa
#file: aa
#owner: root
#group: root
user::rw-
group::r--
group:jerry:rw-  //指定組的匹配規則並沒有改變。
mask::rw-
other::r--
  • setfacl -x g:jerry file.acl
    —>刪除指定組的acl匹配規則。
  • setfacl -b file.cal
    —>關閉在該文件上開啓的acl列表,同時刪除所有的acl參數
[root@xiaoagiao file3]# getfacl aa
#file: aa
#owner: root
group: root
user::rw-
user:user1:rwx
group::r--
group:jerry:rw-
mask::rwx
other::r--

[root@xiaoagiao file3]# setfacl -b aa
[root@xiaoagiao file3]# getfacl aa
#file: aa
#owner: root
#group: root
user::rw-
group::r--
other::r--
//關閉在該文件上開啓的acl列表,同時刪除所有的acl參數

  • setfacl -m d:g:user1 dic.acl
    —>針對目錄指定acl,目錄下新建的文件將繼承目錄的acl匹配規則。
[root@xiaoagiao file3]# setfacl -m d:u:user1:rwx dic
[root@xiaoagiao file3]# getfacl dic
#file: dic
#owner: root
#group: root
user::rwx
group::r-x
other::r-x
default:user::rwx
default:user:user1:rwx
default:group::r-x
default:mask::rwx
default:other::r-x

[root@xiaoagiao file3]# cd dic
[root@xiaoagiao dic]# touch ad
[root@xiaoagiao dic]# getfacl ad
#file: ad
#owner: root
#group: root
user::rw-
user:user1:rwx                  #effective:rw-
group::r-x                      #effective:r--
mask::rw-
other::r--
//新建的文件中自然會有目錄的acl規則。

七, 特殊權限

SUID:

  • 只對二進制程序有效。
  • 程序需要具有可執行權限。
  • 只在執行程序過程中有效(進程)。
  • 執行者擁有屬主權。

表現 :屬主權限的x變爲s。

[root@xiaoagiao dic]# ls -l
total 4
-rw-r--r--. 1 root root 0 Mar 12 20:12 ad

[root@xiaoagiao dic]# chmod u+s ad  //給ad文件的屬主權限賦予s權限。

[root@xiaoagiao dic]# ll -l
total 4
-rwSr--r--. 1 root root 0 Mar 12 20:12 ad
//屬主權限。X出變爲s。
//當程序成爲進程時,執行者擁有屬主權限。

SGID

  • 只對二進制程序有效。
  • 執行者將擁有屬組權限。
  • 程序需要有可執行權。
  • 主要作用於目錄,目錄下創建新的文件,新的文件數組與目錄屬組權限相同。
[root@xiaoagiao file3]# ll -l dic
total 4
-rw-r--r--. 1 root root 0 Mar 12 20:12 ad
[root@xiaoagiao file3]# chmod g+s dic
[root@xiaoagiao file3]# ll
total 4
drwxr-sr-x. 2 root root 4096 Mar 12 20:12 dic
//屬組權限的執行權限變爲s。

SBIT

  • 只對目錄有效。
  • 用戶在此目錄下創建的文件只有所屬用戶和root可刪除。
[root@xiaoagiao file3]# chmod o+t dic
[root@xiaoagiao file3]# ll
total 4
drwxr--r-t. 2 root root 4096 Mar 12 20:49 dic  
//其他人權限的執行權限變爲t。
//該目錄下創建的文件只有所屬用戶和root可刪除。


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