getfacl 取得 ACL 的狀態。
setfacl 設定 ACL 的狀態。
[root@demo ~]# mount -o remount,acl /
[root@demo ~]# mount
/dev/mapper/vgsrv-root on / type ext4 (rw,acl)
[root@demo ~]# vim /etc/fstab
/dev/mapper/vgsrv-root / ext4 defaults,acl 1 1
[root@demo ~]# setfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ...
選項:
-m :設定後續的 acl 參數給檔案使用,不可與 -x 合用;
-x :刪除後續的 acl 參數,不可與 -m 合用;
-b :移除所有的 ACL 設定參數;
-k :移除預設的 ACL 參數;
-R :遞迴設定 acl,即包括次目錄都會被設定;
-d :設定『預設 acl 參數』,只對目錄有效,在該目錄新建的資料會引用此預設值
acl_spec:
[d[efault]:] [u[ser]:]uid [:perms]
[d[efault]:] g[roup]:gid [:perms]
[d[efault]:] m[ask][:] [:perms]
[d[efault]:] o[ther][:] [:perms]
# d 選項,表示該目錄預設有設定的 acl 規範,類似 umask 的功能。
[root@demo ~]# cd /tmp
[root@demo tmp]# touch acl_test
[root@demo tmp]# ll acl_test
-rw-r--r--. 1 root root 0 Jul 18 17:35 acl_test
## 針對使用者 aclan
[root@demo tmp]# setfacl -m u:aclan:rwx acl_test
[root@demo tmp]# ll acl_test
-rw-rwxr--+ 1 root root 0 Jul 18 17:35 acl_test
# 權限部分多了個 +,且權限不是原本的“644”?
[root@demo ~]# getfacl filename
選項與參數:
getfacl 的選項幾乎與 setfacl 相同
[root@demo tmp]# getfacl acl_test
# file: acl_test
# owner: root
# group: root
user::rw-
user:aclan:rwx <== 多出使用者 aclan 獨立權限
group::r--
mask::rwx
other::r--
[root@demo tmp]# setfacl -m u::rwx acl_test
[root@demo tmp]# ll acl_test
-rwxrwxr--+ 1 root root 0 Jul 18 17:35 acl_test
[root@demo tmp]# getfacl acl_test
# file: acl_test
# owner: root
# group: root
user::rwx <== 無使用者列表,代表設定該檔案擁有者
user:aclan:rwx
group::r--
mask::rwx
other::r--
[root@demo tmp]# setfacl -m g:aclan:rx acl_test
[root@demo tmp]# getfacl acl_test
# file: acl_test
# owner: root
# group: root
user::rwx
user:aclan:rwx
group::r--
group:aclan:r-x <== 針對群組 aclan 設定
mask::rwx
other::r--
#使用者或群組所設定的權限必須存在於 mask 的權限設定範圍內才會生效
[root@demo tmp]# setfacl -m m:r acl_test
[root@demo tmp]# getfacl acl_test
# file: acl_test
# owner: root
# group: root
user::rwx
user:aclan:rwx #effective:r--
group::r--
group:aclan:r-x #effective:r--
mask::r--
other::r--
##effective:r--: 有效權限只有r,則user alcan與group aclan的有效權限會縮減為r。
[root@demo tmp]# setfacl -m m:rx acl_test
[root@demo tmp]# man setfacl
[root@demo tmp]# getfacl acl_test
# file: acl_test
# owner: root
# group: root
user::rwx
user:aclan:rwx #effective:r-x
group::r--
group:aclan:r-x
mask::r-x
other::r--
##effective:r-x: 有效權限只有r-x,則user alcan的有效權限縮減為r-x。
[root@demo tmp]# setfacl -x u:aclan: acl_test
[root@demo tmp]# getfacl acl_test
# file: acl_test
# owner: root
# group: root
user::rwx
group::r--
group:aclan:r-x
mask::r-x
other::r--
/tmp/acl_dir
,設定為讓 aclan 可以進入查閱,但不具有修改的權力。
# 1. 先產生測試目錄
[root@demo tmp]# mkdir acl_dir
[root@demo tmp]# ll -d acl_*
drwxr-xr-x. 2 root root 4096 Jul 18 18:10 acl_dir
-rwxr-xr--+ 1 root root 0 Jul 18 17:35 acl_test
[root@demo tmp]# chmod 700 acl_dir/
[root@demo tmp]# ll -d acl_*
drwx------. 2 root root 4096 Jul 18 18:10 acl_dir
-rwxr-xr--+ 1 root root 0 Jul 18 17:35 acl_test
# 2. 切換使用者 aclan 測試
[root@demo tmp]# su - aclan
[aclan@demo ~]$ cd /tmp/acl_dir/
-bash: cd: /tmp/acl_dir/: Permission denied
# 3. 用 root 設定 aclan 對目錄 acl_dir 的權限
[root@demo tmp]# setfacl -m u:aclan:rx acl_dir
[root@demo tmp]# getfacl acl_dir
# file: acl_dir
# owner: root
# group: root
user::rwx
user:aclan:r-x
group::---
mask::r-x
other::---
#4. 再切換使用者 aclan 測試
[root@demo tmp]# su - aclan
[aclan@demo ~]$ cd /tmp/acl_dir/
[aclan@demo acl_dir]$ touch aaa
touch: cannot touch `aaa': Permission denied
#1. 產生測試目錄及檔案並觀察
[root@demo tmp]# cd acl_dir/
[root@demo acl_dir]# touch file1
[root@demo acl_dir]# mkdir dir1
[root@demo acl_dir]# ll
total 4
drwxr-xr-x. 2 root root 4096 Jul 18 18:28 dir1
-rw-r--r--. 1 root root 0 Jul 18 18:28 file1
#權限後面都沒有 + ,代表這個 acl 屬性並沒有繼承
#2. 讓 aclan 在 /tmp/acl_dir 底下一直具有 rx 的預設權限
[root@demo acl_dir]# setfacl -m d:u:aclan:rx /tmp/acl_dir
[root@demo acl_dir]# getfacl /tmp/acl_dir
getfacl: Removing leading '/' from absolute path names
# file: tmp/acl_dir
# owner: root
# group: root
user::rwx
user:aclan:r-x
group::---
mask::r-x
other::---
default:user::rwx
default:user:aclan:r-x
default:group::---
default:mask::r-x
default:other::---
[root@demo acl_dir]# touch file2
[root@demo acl_dir]# mkdir dir2
[root@demo acl_dir]# ll
total 16
drwxr-xr-x. 2 root root 4096 Jul 18 18:28 dir1
drwxr-x---+ 2 root root 4096 Jul 18 18:48 dir2
-rw-r--r--. 1 root root 0 Jul 18 18:28 file1
-rw-r-----+ 1 root root 0 Jul 18 18:48 file2
# 權限後面有 +,表示確實有繼承。
[root@demo acl_dir]# getfacl file2
# file: file2
# owner: root
# group: root
user::rw-
user:aclan:r-x #effective:r--
group::---
mask::r--
other::---
[root@demo acl_dir]# getfacl file2 | setfacl --set-file=- file1
[root@demo acl_dir]# getfacl file1
# file: file1
# owner: root
# group: root
user::rw-
user:aclan:r-x #effective:r--
group::---
mask::r--
other::---
大前提:請建立一個新的 partition,且此 partition 掛載到 /opt/ 這個目錄,使用預設掛載不要加入參數!
Next: 檔案的搜尋
Up: Linux 檔案權限與管理
Previous: 改變檔案權限
Contents
2015-04-13