ACL權限詳解

1.ACL簡介
2.前期準備
3.ACL的基本操作:添加和修改
4.ACL的其他功能:刪除和覆蓋
5.目錄的默認ACL
6.備份和恢復ACL
7.結束語


1.ACL簡介

用戶權限管理始終是Linux系統管理中最重要的環節。大家對Linux/Unix的UGO權限管理方式一定不陌生,還有最常用的chmod命令。為了實現一些比較復雜的權限管理,往往不得不創建很多的組,并加以詳細的記錄和區分。有一種方法可以實現靈活的權限管理(文件的額外賦權機制)除了文件的所有者,所屬組和其他人,可以對更多的用戶設置權限,這就是訪問控制列表(Access Control List)。


2.前期準備

支持ACL需要內核和文件系統的支持?,F在2.6內核以上版本配合EXT3/EXT4, JFS, XFS, ReiserFS等文件系統都是可以支持ACL。下面我們使用CentOS6.8和Ext4文件系統開始對Linux的ACL的體驗。


首先創建一個1M的空白文件:
[root@anonymous mnt]#dd if=/dev/zero of=testacl bs=1024 count=1000
1000+0 records in
1000+0 records out
1024000 bytes (1.0 MB) copied, 0.00832275 s, 123 MB/s
[root@anonymous mnt]#du -sh testacl
1000K testacl

和一個loop設備聯系在一起:
[root@anonymous mnt]#losetup /dev/loop1 testacl

創建一個ext4文件系統:
[root@anonymous mnt]#mkfs.ext4 /dev/loop1
mke2fs 1.41.12 (17-May-2010)
Discarding device blocks: done                           
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
128 inodes, 1000 blocks
50 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=1048576
1 block group
8192 blocks per group, 8192 fragments per group
128 inodes per group
Writing inode tables: done                           
Filesystem too small for a journal
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 37 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

掛載新建的文件系統:
[root@anonymous mnt]#mount -o rw,acl /dev/loop1 /mnt/testdir
[root@anonymous mnt]#cd testdir/
[root@anonymous testdir]#ls
lost+found

現在我已經得到了一個小型的文件系統。而且是支持ACL的。并且即使徹底損壞也不會影響硬盤上其他有價值的數據。因此可以開始我們的ACL體驗了。


3.ACL的基本操作:添加和修改

首先新建一個文件作為實施ACL的對象:
[root@anonymous testdir]#touch file1
[root@anonymous testdir]#ll
total 13
-rw-r–r–. 1 root root     0 Mar 11 15:48 file1
drwx——. 2 root root 12288 Mar 11 15:42 lost+found

然后看一下這個文件缺省的ACL,這時這個文件除了通常的UGO的權限之外,并沒有ACL:
[root@anonymous testdir]#getfacl file1
# file: file1
# owner: root
# group: root
user::rw-
group::r–
other::r–

下面添加幾個用戶和組,待會兒我將使用ACL賦予它們不同的權限:
[root@anonymous testdir]#groupadd grp1
[root@anonymous testdir]#useradd user1
[root@anonymous testdir]#useradd user2
[root@anonymous testdir]#usermod -G grp1 user1

我們將用戶切換到user1,發現并不能在此目錄下創建文件:
[root@anonymous testdir]#su user1
[user1@anonymous testdir]$echo “test” >> file1
bash: file1: Permission denied

失敗了,由于file1并不允許除了root以外的用戶寫,現在我們通過修改file1的ACL賦予user1足夠的權限:
[root@anonymous testdir]#setfacl -m u:user1:rw file1
[root@anonymous testdir]#su user1
[user1@anonymous testdir]$echo “test” >> file1
[user1@anonymous testdir]$cat file1
test

修改成功了,用戶user1可以對file1做讀寫操作了。再次查看file1的ACL:
[user1@anonymous testdir]$getfacl file1
# file: file1
# owner: root
# group: root
user::rw-
user:user1:rw-
group::r–
mask::rw-
other::r–

ls一下:
[root@anonymous testdir]#ls -l file1
-rw-rw-r–+ 1 root root 5 Mar 11 15:59 file1

可以看到一個”+”,就在通常我們看到的權限位的旁邊。這說明了file1設置了ACL, 接下來我們修改一下grp1的權限,同時給tgrp1這個組以讀的權限:
[root@anonymous testdir]#setfacl -m u:user1:rwx,g:grp1:r file1
[root@anonymous testdir]#getfacl file1
# file: file1
# owner: root
# group: root
user::rw-
user:user1:rwx
group::r–
group:grp1:r–
mask::rwx
other::r–

這個時候user1已經有了執行的權限,而grp1這個組也獲得了讀取文件內容的權限。有人可能已經注意到了兩個問題:首先,file1的組權限從r–變成了rw-。其次,mask是什么?為什么也變化了呢?我們先從mask說起。如果說ACL的優先級高于UGO,那么mask就是最后一道防線。它決定了一個用戶或組能夠得到的最大的權限。這樣我們在不破壞已有ACL的定義的基礎上,可以臨時提高或是降低安全級別:
[root@anonymous testdir]#setfacl -m mask::r file1
[root@anonymous testdir]#getfacl file1
# file: file1
# owner: root
# group: root
user::rw-
user:user1:rwx   #effective:r–
group::r–
group:grp1:r–
mask::r–
other::r–

[root@anonymous testdir]#ls -l file1
-rw-r–r–+ 1 root root 5 Mar 11 15:59 file1

在user1對應的ACL項的后邊出現了effective的字樣,這是實際user1得到的權限。Mask只對其他用戶和組的權限有影響,對owner和other的權限是沒有任何影響的。執行ls的結果也顯示UGO的設置也有了對應的變化。因為在使用了ACL的情況下,group的權限顯示的就是當前的mask。


4.ACL的其他功能:刪除和覆蓋

如何刪除已有的ACL項呢?
[root@anonymous testdir]#setfacl -x g:grp1 file1
[root@anonymous testdir]#getfacl file1
# file: file1
# owner: root
# group: root
user::rw-
user:user1:rwx
group::r–
mask::rwx
other::r–

我們看到grp1的權限已經被去掉了。如果需要去掉所有的ACL可以用-b選項。所有的ACL項都會被去掉。
[root@anonymous testdir]#setfacl -b  file1
[root@anonymous testdir]#getfacl file1
# file: file1
# owner: root
# group: root
user::rw-
group::r–
other::r–

我們可以用–set 設置一些新的ACL項,并把原有的ACL項全部都覆蓋掉。和-m不同,-m選項只是修改已有的配置或是新增加一些。–set選項會把原有的ACL項都刪除,用新的替代,需要注意的是一定要包含UGO的設置,不能象-m一樣只是添加ACL就可以了。比如下邊這一段:
[root@anonymous testdir]#setfacl –set u::rw,u:user1:rw,g::r,o::- file1
[root@anonymous testdir]#getfacl file1
# file: file1
# owner: root
# group: root
user::rw-
user:user1:rw-
group::r–
mask::rw-
other::—

o::-是另一個需要注意的地方。其實完整的寫法是other::—,正如u::rw的完整寫法是user::rw-。通常我們可以把”-“省略,但是當權限位只包含”-“時,必須至少保留一個。如果寫成了o::,就會出現錯誤。


5.目錄的默認ACL

如果我們希望在一個目錄中新建的文件和目錄都使用同一個預定的ACL,那么我們可以使用默認(Default) ACL。在對一個目錄設置了默認的ACL以后,每個在目錄中創建的文件都會自動繼承目錄的默認ACL作為自己的ACL。用setfacl的-d選項就可以做到這一點:
[root@anonymous mnt]#setfacl -d –set g:grp1:rwx testdir
[root@anonymous mnt]#getfacl testdir
# file: testdir
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
default:user::rwx
default:group::r-x
default:group:grp1:rwx
default:mask::rwx
default:other::r-x

可以看到默認ACL已經被設置了。建立一個文件試試:
[root@anonymous mnt]#touch testdir/file2
[root@anonymous mnt]#getfacl testdir/file2
# file: testdir/file2
# owner: root
# group: root
user::rw-
group::r-x   #effective:r–
group:grp1:rwx   #effective:rw-
mask::rw-
other::r–
file2自動繼承了testdir對grp1設置的ACL。只是由于mask的存在使得grp1只能獲得rw-權限。


6.備份和恢復ACL

主要的文件操作命令cp和mv都支持ACL,只是cp命令需要加上-p參數。但是tar等常見的備份工具是不會保留目錄和文件的ACL信息的。如果希望備份和恢復帶有ACL的文件和目錄,那么可以先把ACL備份到一個文件里。以后用–restore選項來回復這個文件中保存的ACL信息:

[root@anonymous mnt]#getfacl -R testdir > test.acl
[root@anonymous mnt]#ls -l test.acl
-rw-r–r–. 1 root root 649 Mar 11 16:29 test.acl

我們用-b選項刪除所有的ACL數據,來模擬從備份中回復的文件和目錄:
[root@anonymous mnt]#setfacl -R -b testdir
[root@anonymous mnt]#getfacl -R testdir
# file: testdir
# owner: root
# group: root
user::rwx
group::r-x
other::r-x

# file: testdir/file1
# owner: root
# group: root
user::rw-
group::r–
other::—

現在我們從test.acl中恢復被刪除的ACL信息:
[root@anonymous mnt]#setfacl –restore test.acl
[root@anonymous mnt]#getfacl -R testdir
# file: testdir
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
default:user::rwx
default:group::r-x
default:group:grp1:rwx
default:mask::rwx
default:other::r-x
# file: testdir/file2
# owner: root
# group: root
user::rw-
group::r-x    #effective:r–
group:grp1:rwx   #effective:rw-
mask::rw-
other::r–


7.結束語

剛開始學習ACL的時候感覺非常的痛苦,因為對原理及概念的理解有誤,造成了很大的困惑,好在從網上參考了很多資料以及下了很大功夫將其消化吸收。學習Linux唯有多練多記才能達到自己所預想的目標。


8.參考資料


http://www.bestbits.at/richacl/?cm_mc_uid=29552251466514872527098&cm_mc_sid_50200000=1489219374
http://www.cnblogs.com/dkblog/archive/2011/10/21/2219765.html

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

(0)
mrlapulgamrlapulga
上一篇 2017-03-11 16:27
下一篇 2017-03-11 18:15

相關推薦

  • 第二周練習作業

    第二周作業 1、2、4、5 題博客中已經總結 3、請使用命令行展開功能來完成以下練習: (1)、創建/tmp目錄下的:ac, ad, bc, bd (2)、創建/tmp/mylinux目錄下的: mylinux/     ├── bin     ├── boo…

    Linux干貨 2016-11-06
  • 請使用命令行展開功能來完成以下練習

    (1)、創建/tmp目錄下的:ac, ad, bc, bd ]# mkdir -p /tmp/{a,b}_{c,d} 或者: ]# touch /tmp/{a,b}_{c,d} (2)、創建/tmp/mylinux目錄下的: ]# mkdir -pv /tmp/mylinux/{bin,boot/gtub,dev,etc/{rc.d/init.d,sysco…

    Linux干貨 2016-11-06
  • 使用Openssl構建私有CA

    使用Openssl構建私有CA Openssl是SSL的開源實現,是一種安全機密程序,主要用于提高遠程登錄訪問的安全性。也是目前加密算法所使用的工具之一,功能很強大。     Openssl為網絡通信提供安全及數據完整性的一種安全協議,包括了主要的密碼算法、常用的密鑰和證書封裝管理功能(CA)以及SSL協議,并提供了豐…

    Linux干貨 2015-10-07
  • Homework Week-3 用戶管理

    1、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。  who | cut -f 1 -d \ | uniq “\”后跟一個空格字符 2、取出最后登錄到當前系統的用戶的相關信息。  who | tail…

    Linux干貨 2016-08-24
  • linux 進程解析

    博文介紹了進程信息以及部分進程管理命令

    2017-09-09
  • raid 0磁盤陣列

    raid0 1  先給sdb磁盤和sdc磁盤分區 [root@localhost ~]# fdisk /dev/sdb    Command (m for help): n Command action   e   extended   p   primary part…

    Linux干貨 2017-04-25

評論列表(1條)

  • 馬哥教育
    馬哥教育 2017-03-20 09:09

    咱,建議命令行換個顏色,看起來聽吃力的,太花了

欧美性久久久久