1. 程式人生 > >Linux 訪問控制acl

Linux 訪問控制acl

linux acl設置

Linux:ACL設置


目錄:

1. acl設置

2. mask值

3. acl備份

4. 實例演示


1.acl設置

Acl是關聯用戶和文件或目錄的訪問控制,因此對用戶而言,可以具體分為兩類,用戶和組;對文件而言可以分為文件和目錄;對繼承而言,可以分為默認繼承權限和非繼承權限,從語法上講d表示在某目錄下新建文件或目錄繼續父目錄的default權限。具體如下常用格式:

setfacl -m u:wang:rwx file|directory

setfacl -Rm g:sales:rwX directory

setfacl -m g:salesgroup:rw file| directory

setfacl -m d:u:wang:rx directory

setfacl –R –b directory|file 遞歸刪除目錄或文件下的acl權限

setfacl –x u:wang:rwxfile|directory 刪除本條acl

其中最為關鍵和常用的,在實際項目實施和方案中,將規劃好的acl復制給多個目錄或文件,此時需要復制acl的語法格式,最為常見的語法格式如下:

Getfacl file1 | setfacl --set-file=-file2 將file1的acl權限復制給file2,其中=-中的“-”代表上file1,此種寫法優勢避免重定向給另外一個空文件,等價於getfacl file1> f1 setfacl –set-file=f1 file2。

2.mask值

文件設置acl後,文件的基本權限含義更新,文件的9個字符之前分別表示所屬主的rwx權限、所屬組的rwx權限和其他用戶的rwx權限。在設置acl後文件權限9個字符分別表示所屬主的rwx權限、mask的rwx權限和其他用戶的rwx權限。而mask值則是acl中的限高線,因此在文件訪問控制過程中起到至高的作用,如下所示:

[[email protected] ~]# getfacl /testdir/dir #查看mask值為5,表示讀和執行權限

getfacl: Removing leading ‘/‘ from absolute path names

# file: testdir/dir

# owner: root

# group: root

# flags: -s-

user::rwx

group::r-x

group:g2:r-x

group:g3:r-x

mask::r-x

other::---

default:user::rwx

default:group::r-x

default:group:g2:rwx

default:group:g3:r-x

default:mask::rwx

default:other::---

[[email protected] ~]# ll -d /testdir/dir #驗證說明mask值為r和s權限

drwxr-s---+ 2 root root 21 Jul 25 12:52 /testdir/dir

[[email protected] ~]# chmod 2740 /testdir/dir #修改mask值為4,表示有r的權限

[[email protected] ~]# getfacl /testdir/dir #重新查看並驗證mask值

getfacl: Removing leading ‘/‘ from absolute path names

# file: testdir/dir

# owner: root

# group: root

# flags: -s-

user::rwx

group::r-x #effective:r—[mask限高線,只有r權限]

group:g2:r-x #effective:r—[mask限高線,只有r權限]

group:g3:r-x #effective:r—[mask限高線,只有r權限]

mask::r--

other::---

default:user::rwx

default:group::r-x

default:group:g2:rwx

default:group:g3:r-x

default:mask::rwx

default:other::---

3.acl 備份

Acl備份是通常采用備份還原方式,具體思路如下

第一:將之前文件復制一份重定向到文本中,通過—set-file=重定向文本名,然後將文本名進行還原;

第二:通過專用的的恢復策略參數,setfacl –restore文本名,恢復acl。

[[email protected] /]# getfacl -R /app/test > acl.txt #備份策略到acl.txt

getfacl: Removing leading ‘/‘ from absolute path names #進入/,確定根目錄

[[email protected] /]# cat acl.txt 不會報錯

# file: app/test

# owner: root

# group: root

user::rwx

group::r-x

other::r-x

[[email protected] /]# setfacl --set-file=acl.txt app/test #備份恢復方法1:acl.txt

[[email protected] /]# setfacl --restore acl.txt #備份恢復方法2,通過acl.txt

4.實例演示:

在/testdir/dir裏創建的新文件自動屬於g1組,組g2的成員如:alice能對這些新文件有讀寫權限,組g3的成員如:tom只能對新文件有讀權限,其它用戶(不屬於g1,g2,g3)不能訪問這個文件夾。備份/testdir/dir裏所有文件的ACL權限到/root/acl.txt中,清除/testdir/dir中所有ACL權限,最後還原ACL權限

[[email protected] ~]# useradd alice #添加用戶alice

[[email protected] ~]# useradd tom #添加用戶tom

[[email protected] ~]# groupadd g1 #創建組g1

[[email protected] ~]# groupadd g2 #創建組g2

[[email protected] ~]# groupadd g3 #創建組g3

[[email protected] ~]# groupmems -a alice -g g2 #alice加入g2

[[email protected] ~]# groupmems -l -g g2

alice

[[email protected] ~]# groupmems -a tom -g g3 #tom加入g3

[[email protected] ~]# groupmems -l -g g3

tom

[[email protected] ~]# mkdir -p /testdir/dir

[[email protected] ~]# chmod 2750 /testdir/dir #給目錄sgid和其他用戶0權限

[[email protected] ~]# chgrp g1 /testdir/dir #給目錄所屬組為g1

[[email protected] ~]# ll -d /testdir/dir

drwxr-s---. 2 root root 6 Jul 25 12:40 /testdir/dir

[[email protected] ~]# setfacl -m g:g2:rx /testdir/dir #設置目錄本身的權限,允許g2

[[email protected] ~]# su – alice 進入目錄dir

[[email protected] ~]$ cd /testdir/dir/

[[email protected] dir]$

[[email protected] ~]# setfacl -m d:g:g2:rw /testdir/dir #設置新建文件的繼承權限

[[email protected] ~]$ getfacl /testdir/dir #驗證acl設置

getfacl: Removing leading ‘/‘ from absolute path names

# file: testdir/dir

# owner: root

# group: g1

# flags: -s-

user::rwx

group::r-x

group:g2:r-x

mask::r-x

other::---

default:user::rwx

default:group::r-x

default:group:g2:rw

default:mask::rwx

default:other::---

[[email protected] ~]# touch /testdir/dir/root.txt #創建文件

[[email protected] ~]# vim /testdir/dir/root.txt #編輯文件

[[email protected] ~]# ll /testdir/dir/ #驗證文件權限

total 8

-rw-rw----+ 1 root root 17 Jul 25 12:52 root.txt

[[email protected] ~]# su – alice #切換用戶alice

Last login: Tue Jul 25 12:51:32 HKT 2017 on pts/3

[[email protected] ~]$ cat /testdir/dir/root.txt #查看文件可讀

welcome to xi‘an

[[email protected] ~]$ vim /testdir/dir/root.txt #查看文件可寫

[[email protected] ~]$ cat /testdir/dir/root.txt

welcome to xi‘an

xi‘an is a old city

[[email protected] ~]# setfacl -m g:g3:rx /testdir/dir #設置g3對/testdir/dir權限

[[email protected] ~]# setfacl -m d:g:g3:rx /testdir/dir #設置文件繼續權限

[[email protected] ~]# getfacl /testdir/dir #查看驗證其acl屬性

getfacl: Removing leading ‘/‘ from absolute path names

# file: testdir/dir

# owner: root

# group: g1

# flags: -s-

user::rwx

group::r-x

group:g2:r-x

group:g3:r-x

mask::r-x

other::---

default:user::rwx

default:group::r-x

default:group:g2:rw

default:group:g3:r-

default:mask::rwx

default:other::---

[email protected] ~]# su - xuewb

Last login: Tue Jul 25 12:55:36 HKT 2017 on pts/3

[[email protected] ~]$

[[email protected] ~]$ cd /testdir/dir/ #驗證其他非g2和g3組用戶,無法進入dir目錄

bash: cd: /testdir/dir/: Permission denied

[[email protected] ~]# getfacl -R /testdir/dir/ > acl.txt #備份acl

[[email protected] ~]# setfacl -R -b /testdir/dir #清空acl

[[email protected] ~]# getfacl /testdir/dir #驗證是否清空acl

getfacl: Removing leading ‘/‘ from absolute path names

# file: testdir/dir

# owner: root

# group: g1

# flags: -s-

user::rwx

group::r--

other::---

[[email protected] ~]# setfacl --set-file=acl.txt /testdir/dir #恢復acl設置


本文出自 “11831715” 博客,請務必保留此出處http://11841715.blog.51cto.com/11831715/1960278

Linux 訪問控制acl