ACL通路控制
概述:通路控制,當檔案或目錄的權限不能在完全滿足通路控制的實作時,可以使用acl進行設定通路權限。
即,對一個檔案或者目錄設定個别(特殊)使用者對其有操作的權限
ACL通路控制的作用:
1,對于程式來說,可以賦予某個使用者對這個程式的acl通路控制,例如,單獨為普通使用者設定為對shutdown 指令有所有的權限。
2,對目錄來說,具有acl通路控制權限,那麼所有使用者在此目錄下建立的文檔或目錄将會自動繼承該目錄acl控制政策
注:
1,在安裝系統的時候建立的磁盤分區具有acl的特性;
[root@localhost ~]# tune2fs -l /dev/sda2 | grep acl
Default mount options: user_xattr acl
2,系統安裝後所劃分的磁盤分區預設不支援acl
3,可使用tune2fs-l /dev/sdb1 | grep acl來檢視一個檔案系統是否支援acl
如果支援,則會有相應的輸出資訊,如上面的1所示的輸出内容,如果不支援,則
表示不支援。
4,tune2fs -l指令隻能檢視安裝系統時候所劃分的檔案系統是否支援acl,建立立的分區檢視的時候隻能用mount | grep 裝置名
[root@localhost /]# mount | grep sda6
/dev/sda6 on /data/sda6 typeext3 (rw,acl)
[root@localhost /]#
一:檢視ACL控制政策
[root@localhost ~]# df -h / //首先檢視根目錄的檔案系統
檔案系統容量已用 可用 已用% 挂載點
/dev/sda2 19G 2.3G 16G 13% /
[root@localhost ~]# tune2fs -l /dev/sda2 | grep acl //檢視/dev/sda2檔案
//系統是否支援acl,可以看到輸出結果中包含acl
[root@localhost ~]# getfacl Desktop/ //檢視Desktop的acl控制政策
# file: Desktop
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
[root@localhost ~]#
注:預設情況下,未設定任何額外的acl政策。
二:定義ACL通路控制政策:setfacl
setfacl指令:
-格式:setfacl [選項] u:使用者名:權限檔案…
setfacl [選項] g:組名:權限檔案…
選項有:
-m :定義一條acl政策
-x :清除指定的acl政策
-b :清除所有已設定的acl政策
-R :遞歸設定acl政策
-d :為目錄設預設acl權限(子文檔自動繼承)
示例1:setfacl的-m選項
為/root設定acl政策,使user1具有rx權限
[root@localhost ~]# getfacl /root/ //首先檢視一下/root目錄的acl權限
getfacl: Removing leading '/'from absolute path names
# file: root
other::---
//可以看到,除了基本的權限之外,沒有acl通路控制清單
[root@localhost ~]# ll -d /root/
drwxr-x--- 17 root root 409602-18 21:43 /root/
[root@localhost ~]#su - user1 //切換到user1使用者,并嘗試進入/root目錄
[user1@localhost ~]$ cd /root/
-bash: cd: /root/: 權限不夠 //user1使用者沒有權限進入/root目錄
[user1@localhost ~]$ exit
logout
[root@localhost ~]#setfacl -m user:user1:r-x /root
//給/root目錄添加一條acl政策,使使用者user1對/root目錄具有r和x的權限。
[root@localhost /]# ll -d /root/
drwxr-x---+ 17 root root 4096 02-18 21:43 /root/
//可以看到,設定了acl政策的目錄的權限位将會有一個“+”标示!!!檔案也一樣
[root@localhost ~]# getfacl /root/ //檢視/root目錄的acl權限
user:user1:r-x //可以看到,這裡有一條acl政策,user1具有r和x權限
mask::r-x
[root@localhost ~]# su - user1 //再次切換到user1使用者
[user1@localhost ~]$ cd /root/ //可以看到,user1可以成功進入/root目錄
[user1@localhost root]$ ls
anaconda-ks.cfg Desktop file1.txt file2.txt install.log install.log.syslog
[user1@localhost root]$ pwd
/root
[user1@localhost root]$
示例2:setfacl的-d選項
建立一個目錄/acltest,并設定權限為所有人具有所有權限
設定/acltest目錄的acl權限為user1具有rwx
分别使用root使用者和user2使用者建立目錄test1、test2以及文檔file1.txt、file2.txt
檢視test1、test2、file1.txt、file2.txt的acl政策。
具體操作:
[root@localhost ~]# mkdir /acltest //建立/acltest目錄
[root@localhost ~]# chmod 777 /acltest/ //設定權限為777
[root@localhost ~]# getfacl /acltest/ //檢視acl政策,這時沒設定
# file: acltest
[root@localhost ~]# setfacl -dm u:user1:rwx /acltest/
//為/acltest目錄設定acl政策為使用者user1具有rwx權限
//注:設定使用者時候user可以縮寫為u
[root@localhost ~]# getfacl /acltest/
group::rwx
other::rwx
default:user::rwx
default:user:user1:rwx //預設的acl政策
default:group::r-x
default:mask::rwx
default:other::r-x
[root@localhost ~]# mkdir /acltest/test1 //root使用者建立test1目錄
[root@localhost ~]# touch /acltest/file1.txt //root使用者的檔案file1.txt
[root@localhost ~]# su - user2
[user2@localhost ~]$ mkdir /acltest/test2
[user2@localhost ~]$ touch /acltest/file2.txt
[user2@localhost ~]$ exit
[root@localhost ~]# getfacl /acltest/test* /acltest/file*
//檢視test1、test2、file1.txt以及file2.txt的acl政策
# file: acltest/test1
user:user1:rwx
mask::rwx
default:user:user1:rwx
# file: acltest/test2
# owner: user2
# group: user2
# file: acltest/file1.txt
user::rw-
user:user1:rwx #effective:rw-
group::r-x #effective:r--
mask::rw-
other::r--
# file: acltest/file2.txt
示例三:清除所有acl控制政策
清空剛才對/root目錄和/acltest目錄設定的acl政策
[root@localhost ~]# setfacl -b /acltest/ /root/
[root@localhost ~]# getfacl /acltest/ /root/ | grep user1
//注:這裡有一個提示,意思為從絕對路徑中移除根目錄
//也就是說,實際上是搜不到關于/root目錄和/acltest目錄關于acl政策的
//不适用該提示,直接cd到根目錄下使用相對路徑即可。
[root@localhost ~]# getfacl /acltest/ | grep user1
[root@localhost ~]# cd / //切換到根目錄下
[root@localhost /]# getfacl acltest/
[root@localhost /]# getfacl acltest/ root/ | grep user1
//可以看到,關于對user1的政策已經清空了,這裡無法檢視到。
三:補充:
1,關于注釋的第四條
我建立了一個分區,格式化之後,将其挂載的時候設定為啟用acl,這這時用
mount | grep 裝置名可以看到是有acl支援的,但是用tune2fs不能看到
安裝系統時的根目錄是啟用了acl的,使用tune2fs指令可以看到,但是用
mount | grep 裝置名不可以看到有acl支援
[root@localhost /]# mount -o acl /dev/sda6 /data/sda6/
/dev/sda6 on /data/sda6 type ext3 (rw,acl)
[root@localhost /]# tune2fs -l /dev/sda2 | grep acl
[root@localhost /]#df -h /
/dev/sda2 19G 2.4G 16G 13% /
[root@localhost /]# mount | grep sda2
/dev/sda2 on / type ext3 (rw)
2,關于對一個檔案設定了acl政策的效果
思路:
使用user2在/acltest(/acltest是沒有acl政策的)建立一個檔案file2.txt
将其權限設定為隻有所有者具有rw權限
使用setfacl指定一條政策為user1對file2.txt檔案有可讀寫的權限
測試,user1是否能夠修改該檔案
[root@localhost /]# ll -d /acltest/
drwxrwxrwx 4 root root 4096 02-20 23:53/acltest/
[root@localhost /]# getfacl acltest/
[root@localhost /]# su - user2
[user2@localhost ~]$ cd /acltest/
[user2@localhost acltest]$ touch file2.txt
[user2@localhost acltest]$ chmod 600 file2.txt
[user2@localhost acltest]$ ll file2.txt
-rw------- 1 user2 user2 12 02-21 00:51file2.txt
[user2@localhost acltest]$ echo user2 > file2.txt
[user2@localhost acltest]$ cat file2.txt
user2
[user2@localhost acltest]$ su - user1
密碼:
[user1@localhost ~]$ ls
[user1@localhost ~]$ cd /acltest/
[user1@localhost acltest]$ ls
file2.txt
[user1@localhost acltest]$ echo user1 >> file2.txt
-bash: file2.txt: 權限不夠
[user1@localhost acltest]$cat file2.txt
[user2@localhost acltest]$ setfacl -m u:user1:rw- file2.txt
[user2@localhost acltest]$ getfacl file2.txt
# file: file2.txt
user:user1:rw-
group::rw-
[user1@localhost ~]$ cd /acltest/
[user1@localhost acltest]$ echo user1>>file2.txt
[user1@localhost acltest]$ cat file2.txt //這時user1有權限寫入
user1
[user1@localhost acltest]$
3,關于删除一條acl規則:setfacl的-x參數
為擴充2裡面的user2建立的/acltest/file2.txt檔案添加條規則
setfacl -m u:user3:rw- file2.txt
使得user3具有讀寫權限,
然後将user3的權限删除
[user2@localhost acltest]$ setfacl -m u:user3:rw- file2.txt
user:user3:rw- //現在有兩條規則
group::---
[user2@localhost acltest]$ su user3
//使用su use3的時候可在目前目錄切換使用者
//使用su - user3的時候會直接切換到user3的家目錄
密碼: //輸入密碼後目前使用者将切換為user3,路徑不變
[user3@localhost acltest]$ ls
[user3@localhost acltest]$ cat file2.txt
[user3@localhost acltest]$ echo user3>> file2.txt
user3
[user3@localhost acltest]$ exit
exit
[user2@localhost acltest]$ setfacl -x u:user3 file2.txt
//注:在百度百科裡面搜尋到的那個答案是錯誤的!!!
//-x選項後面不能接權限,隻能寫成-x u:使用者名
[user2@localhost acltest]$ getfaclfile2.txt
user:user1:rw- //user3的那條規則删除掉了
[user2@localhost acltest]$
本文轉自Jx戰壕 51CTO部落格,原文連結:http://blog.51cto.com/xujpxm/1362091,如需轉載請自行聯系原作者