溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Linux的ACL規(guī)則設(shè)置——setfacl及getfacl命令的使用詳解

發(fā)布時(shí)間:2020-06-14 20:23:35 來源:網(wǎng)絡(luò) 閱讀:624 作者:warrent 欄目:系統(tǒng)運(yùn)維

Linux系統(tǒng)中可以針對組、用戶、其他人設(shè)置不同的讀寫執(zhí)行權(quán)限,但是還不夠精準(zhǔn),若想給某個(gè)特定的用戶設(shè)置一個(gè)獨(dú)特的權(quán)限呢?而這個(gè)用戶又不是該文件或目錄的屬組或?qū)俳M,怎么破?

針對特定的某個(gè)用戶設(shè)置權(quán)限的方法如下:

[root@localhost ~]# dmesg | grep -i acl     #查看系統(tǒng)是否可以設(shè)置ACL,若可以查到下面標(biāo)紅的字樣,則代表沒問題。
[    1.173259] systemd[1]: systemd 219 running in system mode. (+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +'ACL' +XZ +LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN)
[    2.487295] SGI XFS with 'ACL's, security attributes, no debug enabled

1、setfacl——設(shè)置ACL規(guī)則(針對單一用戶或組設(shè)置)

setfacl的常用選項(xiàng)如下:

  • -m:設(shè)置后續(xù)的ACL參數(shù)給文件使用,不可與-x一起使用;
  • -x:刪除后續(xù)的ACL參數(shù),不可與-m一起使用;
  • -b:刪除所有的ACL設(shè)置參數(shù);
  • -k:刪除默認(rèn)的ACL參數(shù);
  • -R:遞歸設(shè)置ACL,就是包括子目錄都會被應(yīng)用;
  • -d:設(shè)置默認(rèn)ACL參數(shù),只對目錄有效,在該目錄新建的數(shù)據(jù)會引用此默認(rèn)值;

設(shè)置舉例:

[root@localhost ~]# touch acl_test1      #創(chuàng)建一個(gè)測試文件
[root@localhost ~]# setfacl -m u:lv:rw acl_test1      #單獨(dú)賦予用戶“l(fā)v”讀寫權(quán)限
[root@localhost ~]# ll acl_test1 #查看文件屬性,在權(quán)限欄最后多了一個(gè)“+”,這就是剛設(shè)置的ACL
-rw-rw-r--+ 1 root root 0 9月   3 08:45 acl_test1
[root@localhost ~]# setfacl -m u::rwx acl_test1     #若u后面不寫用戶,則表示設(shè)置該文件的屬主權(quán)限
[root@localhost ~]# ll acl_test1       #會發(fā)現(xiàn)屬主的權(quán)限變成了rwx。
-rwxrw-r--+ 1 root root 0 9月   3 08:45 acl_test1

2、getfacl——查看設(shè)置的ACL規(guī)則

選項(xiàng)和setfacl基本相同,可以通過man手冊來查看具體的使用選項(xiàng),最主要還是用來查看我們使用setfacl設(shè)置的ACL規(guī)則;

使用舉例:

[root@localhost ~]# getfacl acl_test1      #使用getfacl查看設(shè)置的ACL權(quán)限
# file: acl_test1       #文件名
# owner: root          #文件屬主
# group: root            #文件屬組
user::rwx              #屬主的權(quán)限
user:lv:rw-              #用戶“l(fā)v”的權(quán)限
group::r--               #文件屬組的權(quán)限
mask::rw-               #此文件默認(rèn)的有效權(quán)限
#用戶或群組所設(shè)置的權(quán)限必須要存在于mask的權(quán)限設(shè)置范圍內(nèi)才會生效
other::r--                 #其他任何人的權(quán)限

[root@localhost ~]# ll acl_test1     #使用“l(fā)l”命令查看后,進(jìn)行比較
-rwxrw-r--+ 1 root root 0 9月   3 08:45 acl_test1

3、setfacl和getfacl綜合使用舉例:

#1、針對有效權(quán)限mask的設(shè)置方式
[root@localhost ~]# setfacl -m m:r acl_test1     #設(shè)置該文件的權(quán)限僅有讀權(quán)限(r)。
[root@localhost ~]# getfacl acl_test1     #查看
# file: acl_test1
# owner: root
# group: root
user::rwx
user:lv:rw-         #effective:r--    #注釋為有效權(quán)限只有r(讀權(quán)限),哪怕用戶后面有w(寫權(quán)限),也不生效
group::r--
mask::r--   #是因?yàn)檫@里,這里有并且用戶也具有的權(quán)限,才是有效權(quán)限,如這里的“r”權(quán)限。
other::r--
#2、針對特定群組的方式設(shè)置權(quán)限(刪除了原本的文件,又新建了一個(gè)acl_test1):
[root@localhost ~]# setfacl -m g:mygroup:rx acl_test1     #設(shè)置mygroup組有rx權(quán)限
[root@localhost ~]# getfacl acl_test1     #查看
# file: acl_test1
# owner: root
# group: root
user::rw-
group::r--
group:mygroup:r-x                     #這里就是新設(shè)置的權(quán)限
mask::r-x
other::r--
#3、讓myuser1在/usr/local下面一直具有rx的默認(rèn)權(quán)限
[root@localhost ~]# setfacl -m d:u:myuser1:rx /usr/local/     #設(shè)置權(quán)限
[root@localhost ~]# getfacl /usr/local/       #查看
getfacl: Removing leading '/' from absolute path names
# file: usr/local/
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
default:user::rwx
default:user:myuser1:r-x               #就是這行咯
default:group::r-x
default:mask::r-x
default:other::r-x
#創(chuàng)建一個(gè)目錄,并查看是否繼承了默認(rèn)權(quán)限
[root@localhost ~]# cd /usr/local/
[root@localhost local]# mkdir test
[root@localhost local]# ls -ld test/      #有“+”的權(quán)限,說明繼承默認(rèn)權(quán)限了
drwxr-xr-x+ 2 root root 6 9月   3 09:51 test/
[root@localhost local]# getfacl test      #使用getfacl查看
# file: test
# owner: root
# group: root
user::rwx
user:myuser1:r-x
group::r-x
mask::r-x
other::r-x
default:user::rwx
default:user:myuser1:r-x              #就是這行咯
default:group::r-x
default:mask::r-x
default:other::r-x
#以下為取消用戶的某些ACL規(guī)則
[root@localhost local]# setfacl -x u:myuser1 /usr/local/     #取消myuser1用戶對該目錄的ACL規(guī)則
[root@localhost local]# setfacl -x d:u:myuser1 /usr/local/    #取消遞歸的ACL規(guī)則
[root@localhost local]# setfacl -m u:myuser1:- /usr/local    #使myuser1用戶無法使用該目錄,在權(quán)限字段使用“-”表示即可。

———————— 本文至此結(jié)束,感謝閱讀 ————————

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI