setfacl命令的基本用法


1、setfacl的用途

setfacl命令可以用來細分linux下的文件權限。 
chmod命令可以把文件權限分為u,g,o三個組,而setfacl可以對每一個文件或目錄設置更精確的文件權限。 
換句話說,setfacl可以更精確的控制權限的分配。 
比如:讓某一個用戶對某一個文件具有某種權限。

這種獨立於傳統的u,g,o的rwx權限之外的具體權限設置叫ACL(Access Control List) 
ACL可以針對單一用戶、單一文件或目錄來進行r,w,x的權限控制,對於需要特殊權限的使用狀況有一定幫助。 
如,某一個文件,不讓單一的某個用戶訪問。

2、setfacl的用法

用法: setfacl [-bkndRLP] { -m|-M|-x|-X ... } file ...
-m,       --modify-acl 更改文件的訪問控制列表
-M,       --modify-file=file 從文件讀取訪問控制列表條目更改
-x,       --remove=acl 根據文件中訪問控制列表移除條目
-X,       --remove-file=file 從文件讀取訪問控制列表條目並刪除
-b,       --remove-all 刪除所有擴展訪問控制列表條目
-k,       --remove-default 移除默認訪問控制列表
          --set=acl 設定替換當前的文件訪問控制列表
          --set-file=file 從文件中讀取訪問控制列表條目設定
          --mask 重新計算有效權限掩碼
-n,       --no-mask 不重新計算有效權限掩碼
-d,       --default 應用到默認訪問控制列表的操作
-R,       --recursive 遞歸操作子目錄
-L,       --logical 依照系統邏輯,跟隨符號鏈接
-P,       --physical 依照自然邏輯,不跟隨符號鏈接
          --restore=file 恢復訪問控制列表,和“getfacl -R”作用相反
          --test 測試模式,並不真正修改訪問控制列表屬性
-v,       --version           顯示版本並退出
-h,       --help              顯示本幫助信息

3、幾個例子

查看一個test文件的acl

#查看acl
[root@localhost ~]# getfacl test
# file: test
# owner: root
# group: root
user::r-x
user:tank:rwx                   #effective:---
group::r-x                      #effective:---
mask::---
other::---

修改之后再查看:

[root@localhost ~]# setfacl -m u:zhangy:rw- test    #修改文件的acl權限,添加一個用戶權限
[root@localhost ~]# getfacl test
# file: test
# owner: root
# group: root
user::r-x
user:zhangy:rw-                       #多出來一個用戶
user:tank:rwx
group::r-x
mask::rwx
other::---
[root@localhost ~]# setfacl -m g:zhangying:r-w test      #添加一個組
[root@localhost ~]# getfacl test
# file: test
# owner: root
# group: root
user::r-x
user:zhangy:rw-
user:tank:rwx
group::r-x
group:zhangying:rw-
mask::rwx
other::---

 

再看一個例子:

[root@localhost ~]# getfacl test     #查看acl
# file: test
# owner: root
# group: root
user::rw-
group::r--
other::r--

[root@localhost ~]# setfacl -m u:tank:rx test   #給tank用戶向test文件增加讀和執行的acl規則
[root@localhost ~]# getfacl test    #查看acl
# file: test
# owner: root
# group: root
user::rw-
user:tank:r-x      #已加入
group::r--
mask::r-x
other::r--

[root@localhost ~]# setfacl -m u::rwx test   #設置默認用戶,讀,寫,可執行
[root@localhost ~]# getfacl test
# file: test
# owner: root
# group: root
user::rwx
user:tank:r-x
group::r--
mask::r-x
other::r--

[root@localhost ~]# setfacl -b test     #清除所有acl
[root@localhost ~]# getfacl test 
# file: test
# owner: root
# group: root
user::rwx
group::r--
other::r--

[root@localhost ~]# setfacl -m u:tank:rx test      #給tank用戶向test文件增加讀和執行的acl規則
[root@localhost ~]# setfacl -m u:testtank:rx test  #給testtank用戶向test文件增加讀和執行的acl規則
[root@localhost ~]# getfacl test
# file: test
# owner: root
# group: root
user::rwx
user:testtank:r-x
user:tank:r-x
group::r--
mask::r-x
other::r--


[root@localhost ~]# setfacl -x u:tank test    #清除tank用戶,對test文件acl規則
[root@localhost ~]# getfacl test
# file: test
# owner: root
# group: root
user::rwx
user:testtank:r-x
group::r--
mask::r-x
other::r--

 

設置組的話只需要把setfacl -m u::rwx 中的u改為g即可,大致差不多。

設置mask的話,setfacl -m u::rwx 中的u改為m,並且這個可不針對用戶和組哦,其他的大致差不多。

在使用-R時,記得放在-m前面,否則不可以地

使用-d的話,就會把默認的都加上去,針對目錄哦。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM