Linux命令之:setfacl和getfacl

命令總結之:setfacl和getfacl

acl:access control list,實現靈活的權限管理

除了文件的所有者,所屬組合其他人,可以對更多的用戶設置權限

acl生效順序:所有者、自定義用戶、自定義組、其他人

1、首先我們查看man幫助文檔說明

[root@centos7 sixijie]# man setfacl

根據man文檔節選出來幾個我們會經常用到的功能和選項加以說明:

    setfacl - set file access control lists

    The --set and --set-file options set the ACL of a file or a directory. The previous ACL is replaced.   ACL  entries
    for this operation must include permissions

    The -m (--modify) and -M (--modify-file) options modify the ACL of a file or directory.  ACL entries for this oper‐
    ation must include permissions.

    The -x (--remove) and -X (--remove-file) options remove ACL entries. It is not an error to remove  an  entry  which
    does  not  exist.   Only  ACL entries without the perms field are accepted as parameters, unless POSIXLY_CORRECT is
    defined.

    -b, --remove-all
       Remove all extended ACL entries. The base ACL entries of the owner, group and others are retained.

    -k, --remove-default
       Remove the Default ACL. If no Default ACL exists, no warnings are issued.

    -R, --recursive
       Apply operations to all files and directories recursively. This option cannot be mixed with `--restore'.

    -   If the file name parameter is a single dash, setfacl reads a list of files from standard input.

2、-m和-x選項分別為modify(設定)和remove(移除)acl權限

[root@centos7 sixijie]# setfacl -m u:sixijie:rwx f1
[root@centos7 sixijie]# getfacl f1
[root@centos7 sixijie]# setfacl -x u:sixijie f1
[root@centos7 sixijie]# getfacl f1

Linux命令之:setfacl和getfacl Linux命令之:setfacl和getfacl

3、-M和-X選項可以通過文件批量設定acl和移除acl

acl.txt:
u:user1:rw
u:user2:r
u:user3:rwx

[root@centos7 sixijie]# setfacl -M acl.txt f1
[root@centos7 sixijie]# getfacl f1

Linux命令之:setfacl和getfacl

acl.del
u:user1
u:user2

[root@centos7 sixijie]# setfacl -X acl.del f1
[root@centos7 sixijie]# getfacl f1

Linux命令之:setfacl和getfacl

4、–set和–set-file選項:

注意:--set和--set-file會把原有的ACL表項都刪除    

--set-file

man手冊中有這樣一句話:

Copying the ACL of one file to another
          getfacl file1 | setfacl --set-file=- file2

因此
[root@centos7 sixijie]# getfacl f1 | setfacl --set-file=- f2
上述命令即復制f1的ACL給f2
[root@centos7 sixijie]# getfacl f2

Linux命令之:setfacl和getfacl

--set選項

注:一定要包含UGO的設置

[root@centos7 sixijie]# setfacl --set u::rw,u:sixijie:rwx,g::r,o::rw f1
[root@centos7 sixijie]# getfacl f1

Linux命令之:setfacl和getfacl

5、-b選項:清除所有ACL權限

[root@centos7 sixijie]# setfacl -b f1
[root@centos7 sixijie]# getfacl f1

Linux命令之:setfacl和getfacl

6、設置默認的ACL權限及其刪除

(1)設置默認ACL 一般只針對目錄
[root@centos7 sixijie]# setfacl -m d:u:sixijie:rw,d:g:hadoop:r dir1
[root@centos7 sixijie]# getfacl dir1

Linux命令之:setfacl和getfacl

(2)我們在dir1/目錄下創建一個文件和目錄
[root@centos7 sixijie]# cd dir1/
[root@centos7 dir1]# touch f3
[root@centos7 dir1]# mkdir d2
[root@centos7 dir1]# getfacl f3
[root@centos7 dir1]# getfacl d2

Linux命令之:setfacl和getfacl

我們可以看到dir1目錄下的文件及其子目錄繼承了父目錄dir1的ACL權限,這就叫做默認的ACL

刪除的方法很簡單:一個-k選項即可
[root@centos7 sixijie]# setfacl -k dir1/
[root@centos7 sixijie]# getfacl dir1/

Linux命令之:setfacl和getfacl

7、mask介紹

[root@centos7 tmp]# man acl
節選其中的一段話:acl_mask條目表示最大的訪問權限

ACL_MASK        The ACL_MASK entry denotes the maximum access rights 
                that can be granted by entries of type ACL_USER, ACL_GROUP_OBJ, or ACL_GROUP.

所以我們可以在getfacl中看到這樣的條目:effective:Mode

[root@centos7 ~]# setfacl -m u:sixijie:rx,g:user1:rwx f1
[root@centos7 ~]# getfacl f1

設置mask
[root@centos7 ~]# setfacl -m m:r f1
[root@centos7 ~]# getfacl f1

Linux命令之:setfacl和getfacl

可見自定義用戶,自定義組及其所屬組的權限不能大于mask設置的權限

一個小的知識點:setfacl和chmod設置的所屬組的權限可以相互覆蓋,當二者設置的權限不一致時,以使用getfacl看到的“#effective:”后的權限為準

8、備份和恢復ACL

主要的文件操作命令cp和mv都支持ACL,只是cp命令需要加上-p 參數。但是tar等常見的備份工具是不會保留目錄和文件的ACL信息

方法如下:

[root@centos7 ~]# getfacl -R /tmp/dir1 > acl.txt
[root@centos7 ~]# setfacl -R -b /tmp/dir1
[root@centos7 ~]# setfacl -R --set-file=acl.txt /tmp/dir1
[root@centos7 ~]# getfacl -R /tmp/dir1

原創文章,作者:sixijie,如若轉載,請注明出處:http://www.www58058.com/28688

(0)
sixijiesixijie
上一篇 2016-08-04 14:39
下一篇 2016-08-04 14:40

相關推薦

  • 第四周作業

    1、復制/etc/skel目錄為/home/tuser1,要求/home/tuser1及其內部文件的屬組和其他用戶均沒有任何訪問權限 答:     復制目錄:cp -R /etc/skel /home/tuser1     修改權限:chmod -R go=- /home/tuser1 2、編輯/etc/group文件…

    Linux干貨 2016-12-07
  • LVS集群類型

     lvs:Linux Virtual Server         l4:四層路由、四層交換          根據請求報文的目標IP和目標PORT將其調度轉發至后端的某主機;      IPTABLES:  …

    Linux干貨 2017-01-10
  • 【招聘】北京/互聯網/運維工程師/7-15K/雙休,五險一金

    崗位職責:   1、負責服務器的規劃、調試優化、日常監控、故障處理、數據備份、日志分析等工作;   2、參與運維流程制定,確保任何突發情況都能高效響應;  3、負責服務器部署,對服務器構架和網絡進行優化和改進;  4、負責運維相關數據的收集、分析和總結;  5、負責技術運維相關的文檔、手冊…

    Linux干貨 2016-04-16
  • ?Nginx 代理與緩存

    Nginx 代理與緩存 代理、緩存、集群概述 緩存控制(ngx_http_proxy_module) 配置nginx代理的URI資源類型 增加X-Forwarded-For值 啟用proxy緩存URL資源 封裝首部(ngx_http_headers_module) 響應報文首部添加代理信息 代理調度模塊(ngx_http_upstream_module) R…

    Linux干貨 2016-10-30
  • 搜索引擎-網絡爬蟲

     通用搜索引擎的處理對象是互聯網網頁,目前網頁數量以百億計,搜索引擎的網絡爬蟲能夠高效地將海量的網頁數據傳下載到本地,在本地 形成互聯網網頁的鏡像備份。它是搜索引擎系統中很關鍵也很基礎的構件。 1. 網絡爬蟲本質就是瀏覽器http請求。      瀏覽器和網絡爬蟲是兩種不同的網絡客戶端,都以相同的方式來獲取網…

    Linux干貨 2015-11-18
欧美性久久久久