8.3-ACL權限詳解(命令篇)

前言

        我們都知道Linux有三種身份(owner,group,other)搭配三種權限(r,w,x)以及三種特殊權限(SUID,SGID,SBIT),

但是某些時候這些組合不能滿足復雜的權限需求。


例如

      目錄data的權限為:drwxr-x—,所有者與所屬組均為root,在不改變所有者和所屬組的前提下,要求用戶yufei對該

目錄有完全訪問權限(rwx),但又不能讓其他有用完全權限(rwx)。這個要求看似不能實現,這就看出來傳統的權限

管理設置有時候也會力不從心。為了解決這樣的問題,Linux開發出了一套新的文件系統權限管理方法,叫文件訪問

控制列表ACL(Access Control Lists)。這時候,我們就可能通過ACL來實現。


什么是ACL

        ACL是Access Control List的縮寫,主要的目的是在提供傳統的owner,group,others的read,write,execute權限之外的局

部權限設定。ACL可以針對單個用戶,單個文件或目錄來進行r,w,x的權限設定,特別適用于需要特殊權限的使用情況。

簡單來說,ACL就是可以設定特定用戶或用戶組對于一個文件或目錄的操作權限



1.查看當前分區是否支持ACL權限

[root@localhost ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        19G  3.6G   14G  21% /

[root@localhost ~]# dumpe2fs -l /dev/sda1
...
Default mount options:    user_xattr acl

若沒有acl選項,則用以下命令掛載
mount -o remount,acl /dev/sda1

2.查看acl權限

        getfacl file

[root@localhost ~]# getfacl /root/testdir/a 
getfacl: Removing leading '/' from absolute path names
# file: root/testdir/a
# owner: root
# group: root
user::rw-
group::r--
other::r--

3.設定facl權限

setfacl 選項 類型:用戶:權限 file
        -m 設定acl權限
        -x 刪除指定的acl權限
        -b 刪除所有的acl權限
        -d 設定默認acl權限
        -k 刪除默認acl權限
        -R 遞歸設定acl
eg:    setfacl -m u:tom:rw /testdir            #添加tom用戶對/testdir有讀寫權限 
       setfacl -m g:alice:rw /testdir          #添加alice組對/testdir有讀寫權限

4.刪除acl權限
    setfacl -x u:user 文件名    :刪除指定的acl權限
    setfacl -b 文件名               :刪除文件所有acl權限

[root@localhost ~]# setfacl -x u:tom /testdir/
[root@localhost ~]# setfacl -b /testdir/
[root@localhost ~]# getfacl /testdir/
getfacl: Removing leading '/' from absolute path names
# file: testdir/
# owner: root
# group: root
user::rwx
group::r-x
other::r-x

5.修改mask值

    setfacl -m m:rw /testdir

[root@localhost ~]# setfacl -m mask:w /root/acl/
[root@localhost ~]# getfacl /root/acl/
getfacl: Removing leading '/' from absolute path names
# file: root/acl/
# owner: root
# group: root
user::rwx
user:tom:rw-   #effective:-w-
group::r-x   #effective:---
mask::-w-
other::r-x

 

6.遞歸acl:針對目錄下已存在的文件
      setfacl -R -m u:用戶名:權限 目錄名

[root@localhost ~]# setfacl -R -m u:tom:rw acl/
[root@localhost ~]# getfacl acl/
# file: acl/
# owner: root
# group: root
user::rwx
user:tom:rw-
group::r-x
mask::rwx
other::r-x
[root@localhost ~]# getfacl acl/a 
# file: acl/a
# owner: root
# group: root
user::rw-
user:tom:rw-
group::r--
mask::rw-
other::r--

 

7.默認acl:針對加入到目錄的新文件
      setfacl -m d:u:用戶名:權限  目錄名

新文件沒有acl權限
[root@localhost ~]# touch /root/acl/test.txt
[root@localhost ~]# getfacl /root/acl/test.txt
getfacl: Removing leading '/' from absolute path names
# file: root/acl/test.txt
# owner: root
# group: root
user::rw-
group::r--
other::r--
對目錄使用默認acl權限
[root@localhost ~]# setfacl -m d:u:tom:rw /root/acl/
[root@localhost ~]# touch /root/acl/test2.txt
[root@localhost ~]# getfacl /root/acl/test2.txt
getfacl: Removing leading '/' from absolute path names
# file: root/acl/test2.txt
# owner: root
# group: root
user::rw-
user:tom:rw-
group::r-x   #effective:r--
mask::rw-
other::r--

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

#getfacl -R /tmp/dir1 > acl.txt
#setfacl -R -b /tmp/dir1
#setfacl -R --set-file=acl.txt /tmp/dir1
#getfacl -R /tmp/dir1

 附:用戶對于文件的最終權限要和mask值“相與”才是最后所得權限,“文件所有者”和“其他人”對文件權限不受mask限制。

 

 

 

原創文章,作者:M20-1--孔祥文,如若轉載,請注明出處:http://www.www58058.com/28408

(0)
M20-1--孔祥文M20-1--孔祥文
上一篇 2016-08-04 14:39
下一篇 2016-08-04 14:40

相關推薦

  • python快速入門之數據類型

        Python 是 90 年代初由 Guido Van Rossum 為了打發圣誕節而創建的語言。如今它已是當前最流行的程序語言之一.     Python的關鍵要素有以下幾點:1.基本數據類型;2.對象引用;3.組合數據類型;4.邏輯操作符;5.控制流語句;6.算數操作符…

    Python干貨 2015-12-10
  • CentOS7編譯安裝LAMP—php-fpm

    inux的環境是: [root@localhost ~]# lsb_release -a LSB Version:     :core-4.1-amd64:core-4.1-noarch Distributor ID: CentOS Description:     CentOS…

    Linux干貨 2016-12-21
  • N25-第五周博客作業

    1、顯示/boot/grub/grub.conf中以至少一個空白字符開頭的行; [root@localhost ~]# grep "^[[:space:]]\+" /boot/grub/grub.conf      root (hd0,0)…

    Linux干貨 2017-01-06
  • tab鍵 history 命令的相關巧用

    Tab鍵的妙用       1.command沒有完全輸入時: 點擊tab,如果能唯一匹配到命令,則直接補全,否則需要點擊兩下tab來顯示能夠匹配到的命令。                2./2tab : 顯…

    2017-02-18
  • Select、Case

    select循環與菜單 select循環主要用于創建菜單,按數字排序list指定的順序排序,并列出在標準輸出,利用PS3列出提示符進行輸入選擇 用法: select VARIABLE in list  do     循環體命令 done PS3提示語定義: 在腳本中腳本代碼的第一…

    Linux干貨 2016-08-21
  • 搭建個人博客&論壇(LAMP):wordpress、discuz、phpMyAdmin

    搭建個人博客&論壇(LAMP):wordpress、discuz、phpMyAdmin 一、快速部署LAMP架構平臺 1.CentOS 6系統部署 所需安裝包:httpd, php, mysql-server, php-mysql ]# yum install -y  httpd php&n…

    Linux干貨 2016-10-17
欧美性久久久久