ACL權限是為了防止權限不夠用的情況,一般的權限有所有者、所屬組、其他人這三種,當這三種滿足不了我們的需求的時候就可以使用ACL權限;
比如:一個網絡老師,給一個班的學員上課,他在linux的根目錄下面建立了一個文件,只允許本班級的 學員對該目錄進行上傳下載,其他人都不行,這是該目錄的權限一般是770(一般我們設置權限都是所有者的權限大於所屬組的權限,所屬組的權限大於其他人的權限,依次往下),此時有個同學想試聽我們的課程,該怎么給他分配權限呢?此時的話所屬組、所有者、其他人都不滿足,這是就用到ACL權限;
ACL權限的原理類似windows給一個文件添加用戶(這個用戶對該文件具有的權限):
linux的ACL權限也是給一個文件或者目錄添加指定的用戶或者組,然后給對應的權限;
一、ACL權限的開啟
1.查看acl權限是否支持(文件系統是否支持ACL權限):
a.ext4文件系統:使用 dumpe2fs -h /dev/sda3 來查看超級塊中的信息,里面有 Default mount options: user_xattr acl 或者使用 mount 查詢
b.xfs文件系統 :xfs系統貌似已經強制開啟了ACL權限了
2.開啟acl權限
a.臨時開啟:mount -o remount,acl / (remount:重新掛載;[,acl]添加acl權限;[/]分區)
b.永久開啟:編輯我們開機掛載文件/etc/fstab
然后需要讓修改生效,因為是開機自動掛載的文件,所以要讓修改生效有兩種方式:
1.重啟系統
2.重新掛載:mount -o remount /
二、查看和設置ACL權限
1.查看ACL權限:getfacl 文件名 下面是我做的試驗之后的數據,我添加了一個st用戶和一個tgroup2組,這兩個就是alc權限做到的在
2.設定ALC權限:setfacl 命令設置(下面是我做的試驗)
[root@study tmp]# mkdir project [root@study tmp]# ls -l total 0 srwxrwxrwx 1 mysql mysql 0 Jul 20 09:56 mysql.sock srw-rw---- 1 root root 0 Jul 20 09:56 php-fcgi.sock drwxr-xr-x 2 root root 6 Jul 20 10:37 project [root@study tmp]# chmod 770 project/ [root@study tmp]# groupadd zhy [root@study tmp]# chown root:zhy project/ [root@study tmp]# ls -ld project/ drwxrwx--- 2 root zhy 6 Jul 20 10:37 project/ [root@study tmp]# useradd zgw1 useradd: user 'zgw1' already exists [root@study tmp]# useradd zgw2 [root@study tmp]# useradd zgw3 [root@study tmp]# gpasswd -a zgw2 zhy Adding user zgw2 to group zhy [root@study tmp]# gpasswd -a zgw3 zhy Adding user zgw3 to group zhy [root@study tmp]# ls -ld zhy ls: cannot access zhy: No such file or directory [root@study tmp]# ls -ld project/ drwxrwx--- 2 root zhy 6 Jul 20 10:37 project/ [root@study tmp]# su zgw3 Attempting to create directory /home/zgw3/perl5 [zgw3@study tmp]$ cd project/ [zgw3@study project]$ ls -l total 0 [zgw3@study project]$ vi a [zgw3@study project]$ ls -l total 4 -rw-rw-r-- 1 zgw3 zgw3 8 Jul 20 10:44 a [zgw3@study project]$ exit exit [root@study tmp]# su st [st@study tmp]$ cd project/ bash: cd: project/: Permission denied [st@study tmp]$ exit exit [root@study tmp]# setfacl -m u:st:rx project/ [root@study tmp]# getfacl project/ # file: project/ # owner: root # group: zhy user::rwx user:st:r-x group::rwx mask::rwx other::--- [root@study tmp]# su st [st@study tmp]$ cd project/ [st@study project]$ ls -l total 4 -rw-rw-r-- 1 zgw3 zgw3 8 Jul 20 10:44 a [st@study project]$ touch a touch: cannot touch ‘a’: Permission denied [st@study project]$
設置用戶:setfacl -m u:st:rx project [u指定用戶]
設置組:setfacl -m g:tgroup:rx project [g指定組]
[root@study tmp]# getfacl project/ # file: project/ # owner: root # group: zhy user::rwx user:st:r-x group::rwx mask::rwx other::--- [root@study tmp]# groupadd zhy2 [root@study tmp]# setfacl -m g:zhy2:rx project/ [root@study tmp]# getfacl project/ # file: project/ # owner: root # group: zhy user::rwx user:st:r-x group::rwx group:zhy2:r-x mask::rwx other::---
三、ACL權限的最大有效權限,和刪除acl權限
[root@study tmp]# getfacl project/ # file: project/ # owner: root # group: zhy user::rwx user:st:r-x group::rwx group:zhy2:r-x mask::rwx other::---
大家看上面的acl權限中有個"mask"的選項,它就是ACL權限的最大權限,現在是rwx,當你設置某個用戶或組的ACL權限時,要跟mask的權限“相與”之后產生的權限才是該用戶的最終權限,也就是加入mask的最大權限是rx,但是你給st用戶設置的是rwx權限,此時st用戶它的權限只有rx的權限,因為與最大權限“相與”得出的結果就是rx
命令:setfacl -m m:rx project
2.刪除ACL權限
刪除ACL用戶權限:setfacl -x u:st 文件名
刪除ACL組權限:setfacl -x g:tgroup 文件名
刪除整個ACL權限:setfacl -b 文件名
[root@study tmp]# getfacl project/ # file: project/ # owner: root # group: zhy user::rwx user:st:r-x group::rwx #effective:r-x group:zhy2:r-x mask::r-x other::--- [root@study tmp]# setfacl -x u:st project/ [root@study tmp]# getfacl project/ # file: project/ # owner: root # group: zhy user::rwx group::rwx group:zhy2:r-x mask::rwx other::--- [root@study tmp]# setfacl -x g:zhy2 project/ [root@study tmp]# getfacl project/ # file: project/ # owner: root # group: zhy user::rwx group::rwx mask::rwx other::--- [root@study tmp]# ls -ld project/ drwxrwx---+ 2 root zhy 14 Jul 20 10:44 project/ [root@study tmp]# setfacl -b project/ [root@study tmp]# ls -ld project/ drwxrwx--- 2 root zhy 14 Jul 20 10:44 project/
四、設置默認ACL權限和遞歸ACL權限
1.命令:setfacl -m u:st:rx -R project 遞歸只對該目錄下面現有的子文件或目錄有用,對於該目錄下面新添加的子文件或目錄沒用
[root@study tmp]# cd project/ [root@study project]# ls -l total 4 -rw-rw-r-- 1 zgw3 zgw3 8 Jul 20 10:44 a [root@study project]# cd .. [root@study tmp]# setfacl -m u:st:rx -R project/ [root@study tmp]# ls -l project/ total 4 -rw-rwxr--+ 1 zgw3 zgw3 8 Jul 20 10:44 a
[root@study tmp]# cd project/
[root@study project]# ls -l
total 4
-rw-rwxr--+ 1 zgw3 zgw3 8 Jul 20 10:44 a
[root@study project]# touch b
[root@study project]# ls -l
total 4
-rw-rwxr--+ 1 zgw3 zgw3 8 Jul 20 10:44 a
-rw-r--r-- 1 root root 0 Jul 20 11:34 b
[root@study project]#
2.默認權限:使用d ,格式:setfacl -m d:u:st:rx project 默認權限只對該目錄下面新建的文件或目錄有效,對已經存在的子文件無效
[root@study tmp]# setfacl -m d:u:st:rx project/ [root@study tmp]# ls -l project/ total 4 -rw-rwxr--+ 1 zgw3 zgw3 8 Jul 20 10:44 a -rw-r--r-- 1 root root 0 Jul 20 11:34 b [root@study tmp]# touch project/c [root@study tmp]# ls -l project/ total 4 -rw-rwxr--+ 1 zgw3 zgw3 8 Jul 20 10:44 a -rw-r--r-- 1 root root 0 Jul 20 11:34 b -rw-rw----+ 1 root root 0 Jul 20 11:38 c