ACL 是 Access Control List 的縮寫,主要的目的是在提供傳統的 owner,group,others 的read,write,execute 權限之外的詳細權限訓定。ACL 可以針對單一使用者,單一檔案或目錄來進行r,w,x 的權限設定,對於需要特殊權限的使用狀況非常有幫助
下面來具體的設置文件ACL權限。
如果想擁有文件ACL的權限,就必須爲文件系統添加一個ACL屬性。
[root@localhost ~]#
[root@localhost ~]# mount -o remount,acl /dev/sda9
[root@localhost ~]#
[root@localhost ~]# mount | grep /dev/sda9
/dev/sda9 on /mnt type ext3 (rw,acl)
[root@localhost ~]#
[root@localhost ~]#
可以看到,/dev/sda9已經有了ACL屬性。
現在我們新建一個redhat目錄。
[root@localhost ~]#
[root@localhost ~]# cd /mnt/
[root@localhost mnt]#
[root@localhost mnt]# mkdir redhat
[root@localhost mnt]#
[root@localhost mnt]# ll -ld /redhat/
drwxr-xr-x 2 root root 4096 Feb 26 20:56 /redhat/
[root@localhost mnt]#
可以看到,這個redhat目錄的權限爲755,這個目錄的擁有人和擁有組都是root,也就是說其他人對這個目錄是沒有寫入的權限的。
[root@localhost ~]# su – user1
[user1@localhost ~]$
[user1@localhost ~]$ cd /mnt/
[user1@localhost mnt]$ ls
lost+found redhat
[user1@localhost mnt]$ cd redhat/
[user1@localhost redhat]$ touch file
touch: cannot touch `file’: Permission denied
[user1@localhost redhat]$ ls
[user1@localhost redhat]$
可以看到,user1是other,user1對redhat這個目錄果然沒有寫入的權限。
如果我們想要給user1對這個目錄有一個rwx的權限。除了開放other的權限,我們還可以使用文件的ACL功能。
現在我們就給user1對該目錄設置ACL權限。
#setfacl -m u:user1:rwx /mnt/redhat/
-m 修改
u 用戶
user1 指定用戶
rwx 指定權限
/redhat 指定目錄
[root@localhost ~]#
[root@localhost ~]# cd /mnt/
[root@localhost mnt]#
[root@localhost mnt]# setfacl -m u:user1:rwx /mnt/redhat/
[root@localhost mnt]#
[root@localhost mnt]# ll
total 14
drwx—— 2 root root 12288 Feb 26 21:53 lost+found
drwxrwxr-x+ 2 root root 1024 Feb 26 22:11 redhat
[root@localhost mnt]#
我們已經給redhat目錄設置了ACL權限,通過ll可以看到,redhat目錄的權限位後面多了一個加號,這個加號就代表不要看表面的權限,說明還有更深的權限隱藏在裏面。
如何去查看這個文件的具體權限呢
#Getfacl /mnt/redhat
[root@localhost mnt]#
[root@localhost mnt]# getfacl /mnt/redhat
getfacl: Removing leading ‘/’ from absolute path names
# file: mnt/redhat
# owner: root
# group: root
user::rwx
user:user1:rwx
group::r-x
mask::rwx
other::r-x
[root@localhost mnt]#
我們可以看到文件的擁有人和擁有組都是root,user的權限是rwx,group的權限也r-x,other的權限是r-x。而且最重要的是user:user1:rwx,也就是user1這個用戶對該目錄也有rwx的權限。Mask代表用戶的實際權限。
現在我們利用user1到redhat目錄裏面嘗試去寫入。
[root@localhost ~]#
[root@localhost ~]# su – user1
[user1@localhost ~]$
[user1@localhost ~]$ cd /mnt/redhat/
[user1@localhost redhat]$
[user1@localhost redhat]$ mkdir file
[user1@localhost redhat]$ ls
file
[user1@localhost redhat]$
可以看到,剛纔我們是不可以往裏面寫入的,但是現在我們就寫入成功了。
剛纔我們對user1這個用戶做了文件的ACL權限。
現在我們對組來做文件的ACL權限。
#setfacl -m g:user1:rwx /mnt/redhat/
[root@localhost ~]#
[root@localhost ~]# setfacl -m g:user1:rwx /mnt/redhat/
[root@localhost ~]#
[root@localhost ~]# getfacl /mnt/redhat/
getfacl: Removing leading ‘/’ from absolute path names
# file: mnt/redhat
# owner: root
# group: root
user::rwx
user:user1:rwx
group::r-x
group:user1:rwx
mask::rwx
other::r-x
[root@localhost ~]#
現在user1這個組裏面的成員對/mnt/redhat目錄也有rwx權限了。
下面還有個關於文件ACL功能的問題
剛纔我們用user1在/mnt/redhat目錄下面創建了一個file的文件夾,說明我們對redhat這個目錄有rwx的權限,但是如果在redhat目錄下面還有一個文件夾,我們是否對它有rwx的權限呢。試下,
[root@localhost ~]#
[root@localhost ~]# cd /mnt/redhat/
[root@localhost redhat]# ls
file
[root@localhost redhat]# mkdir file1
[root@localhost redhat]# cd
[root@localhost ~]#
[root@localhost ~]# su – user1
[user1@localhost ~]$ cd /mnt/redhat/
[user1@localhost redhat]$ cd file1
[user1@localhost file1]$ touch 1.txt
touch: cannot touch `1.txt’: Permission denied
[user1@localhost file1]$
很顯然,我們對redhat目錄下面的子目錄是沒有rwx的權限的。也就是說文件的ACL功能只對當前目錄生效,並不可以對子目錄生效。
看下這條命令的作用,
# setfacl -m d:u:user2:rwx /mnt/redhat/
[root@localhost ~]#
[root@localhost ~]# setfacl -m d:u:user2:rwx /mnt/redhat/
[root@localhost ~]#
這個時候,我們同樣給user2一個rwx的權限。
[root@localhost ~]#
[root@localhost ~]# su – user2
[user2@localhost ~]$
[user2@localhost ~]$ cd /mnt/redhat/
[user2@localhost redhat]$
[user2@localhost redhat]$ mkdir file100
mkdir: cannot create directory `file100′: Permission denied
[user2@localhost redhat]$
但是現在我們不可以往/mnt/redhat目錄裏面寫入數據,就是因爲多了一個d,這個d代表default。
我們現在來看看它的用處是什麼。
[root@localhost ~]#
[root@localhost ~]# cd /mnt/redhat/
[root@localhost redhat]# mkdir test
[root@localhost redhat]# ll
total 4
drwxrwxr-x 2 user1 user1 1024 Feb 26 22:44 file
drwxr-xr-x 2 root root 1024 Feb 26 22:46 file1
drwxrwxr-x+ 2 root root 1024 Feb 26 22:58 test
[root@localhost redhat]#
現在可以看到,test這個目錄權限位上面多了一個加號,看下它的具體權限。
[root@localhost redhat]#
[root@localhost redhat]# getfacl test/
# file: test
# owner: root
# group: root
user::rwx
user:user2:rwx
group::r-x
mask::rwx
other::r-x
default:user::rwx
default:user:user2:rwx
default:group::r-x
default:mask::rwx
default:other::r-x
[root@localhost redhat]#
可以看到,user2這個用戶對該目錄有rwx的權限。嘗試往test目錄裏面寫入數據。
[root@localhost ~]#
[root@localhost ~]# su – user2
[user2@localhost ~]$
[user2@localhost ~]$ cd /mnt/redhat/
[user2@localhost redhat]$
[user2@localhost redhat]$ cd test/
[user2@localhost test]$
[user2@localhost test]$ touch 1.txt
[user2@localhost test]$ ls
1.txt
[user2@localhost test]$
寫入成功了,剛纔我們只是對/redhat目錄做了文件的ACL功能,爲什麼現在user2對其子目錄會有rwx的權限呢,這個就是因爲剛纔d參數的含義,就是遞歸的含義,對當前目錄無效果,對子目錄纔有效果。
不管是任何用戶向/mnt/redhat目錄建立的數據,user2都會繼承它們的權限。
這條命令只可以針對目錄來做ACL權限。不可以對文件做,因爲對文件做沒有意義。文件不可能有下一級目錄的。
如果文件的擁有人和擁有組和文件的ACL權限發生衝突的時候,以擁有人和擁有組爲準,其實文件的ACL功能只對other人有意義。
如何移除文件的ACL權限呢
#setfacl -k /mnt/redhat/
移除文件的默認ACL屬性
#setfacl –b /mnt/redhat
移除文件的所有ACL權限
#setfacl –x u:user1 /mnt/redhat
移除user1對文件的ACL權限
當我們在複製文件的時候會,如果使用-p的參數,也一樣可以將文件的ACL權限給複製過去。