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
下一篇 2016-08-04

相關推薦

欧美性久久久久