close

ACL(Access Control List):主要的目的是在Linux系統提供傳統的檔案及目錄存取權限之外,另外開放更細部權限設定功能。

啟用ACL:
由於Linux POSIX ACL 預設是關閉,因此需要透過檔案系統的啟用才能用此功能

ubuntu@ubuntu:~$ vim /etc/fstab                                  #編輯/etc/fstab
LABEL=cloudimg-rootfs   /home        ext4   defaults,acl  0 0    #於defaults後加入acl 
ubuntu@ubuntu:~$ sudo mount -o remount /home                     #重新掛載
ubuntu@ubuntu:~$ cat /etc/mtab                                   #查看ACL是否已啟用
/dev/sda1 / ext4 rw,acl 0 0                                      #ACL已啟用

取得存取控制清單ACL資訊: getfacl

ubuntu@ubuntu:~$ touch acltest                        #建立個測試檔
ubuntu@ubuntu:~$ ls -l acltest                        #查看預設權限 
-rw-rw-r-- 1 ubuntu ubuntu 24 May 28 15:19 acltest
ubuntu@ubuntu:~$ getfacl acltest                      #查看ACL權限
# file: acltest                                       #檔案名稱
# owner: ubuntu                                       #檔案擁有者
# group: ubuntu                                       #檔案擁有群組
user::rw-                                             #擁有者權限
group::rw-                                            #擁有群組權限
other::r--                                            #其他使用者權限

設定存取控制清單ACL資訊: setfacl

setfacl參數
參數 說明
-m 新增或修改ACL規則。
-x 移除現有的ACL規則。
-d 設定預設的ACL規則,僅可針對目錄進行操作。
-b 移除所有ACL規則。

 

增加使用者權限:setfacl -m u:使用者:權限 檔名

ubuntu@ubuntu:~$ setfacl -m u:acltest:rw acltest       #讓acltest使用者可讀寫acltest檔案
ubuntu@ubuntu:~$ ls -l acltest                         #查看acltest檔案權限
-rw-rw-r--+ 1 ubuntu ubuntu 24 May 28 15:19 acltest    #在原本權限後增加了+號
ubuntu@ubuntu:~$ getfacl acltest
# file: acltest
# owner: ubuntu
# group: ubuntu
user::rw-
user:acltest:rw-                                       #增加了acltest使用者的權限
group::r--
mask::rw-
other::r--

設定ACL Mask:setfacl -m m:權限 檔案/目錄

ubuntu@ubuntu:~$ setfacl -m m:r acltest           #設定ACL Mask
ubuntu@ubuntu:~$ getfacl acltest                  #查看ACL權限
# file: acltest
# owner: ubuntu
# group: ubuntu
user::rw-
user:acltest:rw-                #effective:r--   #ACL Mask生效了
group::r--
mask::r--                                        #設定的ACL Mask權限
other::r--
ubuntu@ubuntu:~$ su acltest                      #切換為acltest
Password: 
acltest@ubuntu:/home/ubuntu$ echo test > acltest #嘗試寫入檔案
bash: acltest: Permission denied                 #因ACL Mask生效,所以無法寫入

設定目錄ACL權限

ubuntu@ubuntu:~$ mkdir acl_dir1                                 #建立acl_dir1目錄
ubuntu@ubuntu:~$ setfacl -m u:acltest:rwx acl_dir1              #建立ACL規則,讓acltest使用者可讀.寫.進入acl_dir1目錄
ubuntu@ubuntu:~$ getfacl acl_dir1                               #查看ACL權限
# file: acl_dir1
# owner: ubuntu
# group: ubuntu
user::rwx
user:acltest:rwx
group::rwx
mask::rwx
other::r-x
ubuntu@ubuntu:~$ su acltest                                     #切換為acltest
Password: 
acltest@ubuntu:/home/ubuntu$ touch acl_dir1/filetest            #以acltest使用者建立檔案
acltest@ubuntu:/home/ubuntu$ ls -l acl_dir/filetest
drwxrwxr-x  1 acltest    acltest    4096 May 28 15:26 filetest  #擁有者和擁有群組為acltest
acltest@ubuntu:/home/ubuntu$ getfacl acl_dir1/filetest          #查看ACL權限
# file: filetest
# owner: acltest
# group: acltest                                                #擁有者和擁有群組為acltest
user::rwx
group::rwx
other::r-x
acltest@ubuntu:/home/ubuntu$ mkdir acl_dir1/dirtest             #以acltest使用者建立目錄
acltest@ubuntu:/home/ubuntu$ ls -ld acl_dir/dirtest
drwxrwxr-x 2 acltest acltest 4096 May 28 15:26 dirtest
acltest@ubuntu:/home/ubuntu$ getfacl acl_dir1/dirtest
# file: dirtest
# owner: acltest
# group: acltest                                               #擁有者和擁有群組為acltest
user::rwx
group::rwx
other::r-x                                                     #目錄ACL權限設定完成
acltest@ubuntu:/home/ubuntu$ exit                              #切換使用者回ubuntu
ubuntu@ubuntu:~$ mkdir acl_dir/subtest/                        #在acl_dir目錄下再建立個subtest檔案
ubuntu@ubuntu:~$ su acltest                                    #切換為acltest
Password: 
acltest@ubuntu:/home/ubuntu$ ls -l acl_dir1/subtest            #查看檔案權限
-rw-rw-r--  1 ubuntu ubuntu    0 Aug 28 15:21 subtest          #擁有者和擁有群組為ubuntu
acltest@ubuntu:/home/ubuntu$ echo test > acl_dir1/subtest      #嘗試寫入檔案
bash: subtest: Permission denied                               #因未設定Default ACL所以無法寫入

設定Default ACL:setfacl -d -m 使用者/群組:權限 目錄
Default ACL主要針對目錄內所產生的檔案進行預設權限設定,因此只能套用在目錄中。

ubuntu@ubuntu:~$ setfacl -d -m u:acltest:rwx acl_dir1  #建立Default ACL規則,讓acltest使用者可讀.寫.進入acl_dir1目錄
ubuntu@ubuntu:~$ getfacl acl_dir1                      #查看ACL權限
# file: acl_dir1
# owner: ubuntu
# group: ubuntu
user::rwx
user:acltest:rwx
group::rwx
mask::rwx
other::r-x
default:user::rwx
default:user:acltest:rwx
default:group::rwx
default:mask::rwx
default:other::r-x                                       #Default ACL設定完成
ubuntu@ubuntu:~$ touch acl_dir1/dfacltest                #在acl_dir1目錄下建立檔案
ubuntu@ubuntu:~$ ls -l  acl_dir1/dfacltest               #查看在acl_dir1目錄下建立的檔案
-rw-rw-r--+ 1 ubuntu ubuntu    0 May 29 11:04 dfacltest  #建立的檔案已有ACL權限
ubuntu@ubuntu:~$ getfacl acl_dir1/dfacltest              #查看ACL權限
# file: dfacltest
# owner: ubuntu
# group: ubuntu
user::rw-
user:acltest:rwx                #effective:rw-
group::rwx                      #effective:rw-
mask::rw-
other::r--
ubuntu@ubuntu:~$ su acltest                              #切換為acltest
Password: 
acltest@ubuntu:/home/ubuntu$ echo "write dfacltest" > acl_dir1/dfacltest #嘗試寫入檔案
acltest@ubuntu:/home/ubuntu$ cat acl_dir1/dfacltest      #驗證是檔案是否寫入
write dfacltest                                          #因有目錄設定Default ACL,可寫入檔案

移除ACL權限:setfacl -x 使用者/群組 檔案/目錄

ubuntu@ubuntu:~$ getfacl acltest                   #查看ACL權限
# file: acltest
# owner: ubuntu
# group: ubuntu
user::rw-
user:acltest:rw-                #effective:r--
group::r--
mask::r--
other::r--
ubuntu@ubuntu:~$ setfacl -x u:acltest acltest      #移除ACL權限
ubuntu@ubuntu:~$ getfacl acltest                   #再次查看ACL權限
# file: acltest
# owner: ubuntu
# group: ubuntu
user::rw-
group::r--
mask::r--
other::r--                                         #acltest之ACL權限已被移除

移除所有ACL權限:setfacl -b 檔案/目錄

ubuntu@ubuntu:~$ getfacl acltest      #查看ACL權限
# file: acltest
# owner: acltest
# group: acltest
user::rw-
user:ubuntu:rw-
group::rw-
group:ubuntu:rw-
mask::rwx
other::r--
ubuntu@ubuntu:~$ setfacl -b acltest  #刪除所有ACL權限
ubuntu@ubuntu:~$ getfacl acltest     #再次查看ACL權限
# file: acltest
# owner: acltest
# group: acltest
user::rw-
group::rw-
other::r--                           #檔案已變為預設權限
arrow
arrow
    文章標籤
    Linux ACL Access control list
    全站熱搜

    小必 發表在 痞客邦 留言(0) 人氣()