權限ACL Access Control List
- ACL可以更加靈活的設置權限,除了文件的所有者,所屬組和其它人,可以對更多的用戶設置權限
- CentOS7 默認創建的xfs 和ext4 文件系統具有ACL
- CentOS7 之前版本,默認手工創建的ext4 文件系統無ACL功
能, 需手動增加(後掛載的是沒有acl功能的,做系統掛載的是默認就有的) - tune2fs –o acl /dev/sdb1
- mount –o acl /dev/sdb1 /mnt/test
- tune2fs -l /dev/sdan(在centos6上可以查看分區詳細信息)
- ACL生效順序:所有者-自定義用戶-自定義組-其他人,當匹配後不會在往下匹配後面的權限也是沒有用的
命令setfacl
- 格式 setfacl [option] [u:username:rwx/g:group:rwx] [文件]
- 選項
- -m 是修改增加
- -x 是刪除
- -R 遞歸
- -X 批量刪除用文本
- -M 批量添加用文本
- d d:u:username:rwx dir 在目錄下創建的文件默認都會有acl
- –set 覆蓋原來的acl
- -b 清除所有的acl
查看文件的acl權限內容 getfacl
- 格式 getfacl file
備份和恢復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
- setfacl –restore acl.txt
- getfacl -R /tmp/dir1
操作
- 添加tom用戶對123.TXT文檔有rw權限
[root@centos6 app]# ll
total 4
-rw-r--r--. 1 root g1 68 Nov 18 11:13 123.txt
請注意權限後面有一個點- 現在我們開始把tom用戶有讀寫操作
[root@centos6 app]# setfacl -m u:tom:rw 123.txt
[root@centos6 app]# ll
total 8
-rw-rw-r--+ 1 root g1 68 Nov 18 11:13 123.txt
現在對比之前的詳細信息發現權限並沒有發生任何改變,但是後面的點變成了加號
- 這種有加號的就代表這文件裏有ACL權限,用getacl命令查看文件
[root@centos6 app]# ll
total 8
rw-rw-r--+ 1 root g1 68 Nov 18 11:13 123.txt[root@centos6 app]# getfacl 123.txt
# file: 123.txt
# owner: root
# group: g1
user::rw- 這是所有者的權限
user:tom:rw- 這是我們剛剛添加tom權限
group::r-- 這是所屬組的權限 (這和原來的權限不一樣)
mask::rw- 權限掩碼
other::r-- 這是其他人權限- su tom切換到tom用戶看看能寫和讀嗎
[tom@centos6 app]$ cat 123.txt
123
[tom@centos6 app]$ echo 'hahahah' >> 123.txt- 說明我們增加權限成功了
- 讓guo123用戶加入g1組
[root@centos6 app]# id guo123
uid=1209(guo123) gid=1209(guo123) groups=1209
(guo123),1241(g1)
[root@centos6 app]# su guo123
[guo123@centos6 app]$ cat 123.txt
123
123
123123123
[guo123@centos6 app]$ echo '123456' >> 123.txt
bash: 123.txt: Permission denied- 我們用ls -l 看看到是讀寫的,但是現在只有讀沒有寫的權限,說明用ls -l 看到到所屬組的權限不是準確的,但是在acl列表中group 是隻有r權限的說明所屬組是有group這個權限的控制的。
- 用chmod 改所屬組就是改變mask 掩碼值
改所屬組權限
- chmod g+rwx這是改mask值,並不是改group所屬組的權限
- setfacl -m group::rw file
[root@centos6 app]# setfacl -m group::rw 123.txt
[root@centos6 app]# getfacl 123.txt
# file: 123.txt
# owner: root
# group: g1
user::rw-
user:tom:rw-
group::rw-
mask::rw-
other:: ---
在添加某個組有讀寫權限
- setfacl -m g:groupname:rw /file
[root@centos6 app]# setfacl -m g:xiaowang:rw /app/123.txt
[root@centos6 app]# getfacl 123.txt
# file: 123.txt
# owner: root
# group: g1
user::rw-
user:tom:rw-
group::rw-
group:xiaowang:rw-
mask::rw-
other::---
- 組的權限是相加的如果一個用戶有多個組, 這幾個組都在acl有不同的權限,這個用戶的權限就是這幾個組的權限的累加之和。
mark 值詳解
- 我們之前說用chmod 命令改所屬組的時候並不是改真正的所屬組而是改mark值。那現在改一下mask值不能讀寫
[root@centos6 app]# chmod g-r,g-w 123.txt
[root@centos6 app]# ll
total 4
-rw-------+ 1 root g1 0 Nov 18 12:19 123.txt
[root@centos6 app]# getfacl 123.txt
# file: 123.txt
# owner: root
# group: g1
user::rw-
user:tom:rw- #effective:--- 有效的權限
group::rw- #effective:---
group:xiaowang:rw- #effective:---
mask::---
other::---- 當mark值沒有權限的時候我們發現所屬組和加入acl中有權限的用戶和組,的後面都顯示#effective — 這樣的備註 說明mark的權限設定了所屬組和加入acl用戶和組的最高權限,如果mask中沒有的權限但是三個選項有就給去掉,他們三是不能超過mask的權限的。mark的權限對所有者和其它人不會管。
更改mask的權限
- setfacl -m mask:rwx /file
- chmod g+rwx /file
遞歸增加目錄acl屬性
- setfacl -R -m u:username:rwx dir/
如何將文本中的ACL列表導入文件或者目錄中
- setfacl -M acl.txt /file
- setfacl -X acl.txt file 批量刪除
- 注意acl.txt這個名字可以隨便起,但是裏面的內容一定是這種格式
u:username:rwx
- g:groupname:rwx
如何設置在目錄裏創建的文件或者目錄都繼承父目錄的acl
- setfacl -m d:u:uname:rwx /file/
如何刪除單個acl條目
- setfacl -x u:name:rwx file
覆蓋原有的acl
- –set 選項會把原有的ACL 項都刪除,用新的替代,需要注
意的是一定要包含UGO 的設置,不能象-m 一樣只是添加
ACL 就可以 - setfacl –set u::rw,u:wang:rw,g::r,o::- file1
複製文件的acl權限給另一個文件添加
- getfacl file1 | setfacl –set-file=- file2