1. 程式人生 > >linux系統 使用者身份與檔案許可權(三)

linux系統 使用者身份與檔案許可權(三)

0.檔案的隱藏屬性

    Linux系統中的檔案除了具備一般許可權和特殊許可權之外,還有一種隱藏許可權,即被隱藏起來的許可權,預設情況下不能直接被使用者發覺。有使用者曾經在生產環境和RHEL考試題目中碰到過明明許可權充足但卻無法刪除某個檔案的情況,或者僅能在日誌檔案中追加內容而不能修改或刪除內容,這在一定程度上阻止了黑客篡改系統日誌的圖謀,因此這種“奇怪”的檔案也保障了Linux系統的安全性。

1.  chattr命令

    chattr命令用於設定檔案的隱藏許可權,格式為“chattr [引數] 檔案”。如果想要把某個隱藏功能新增到檔案上,則需要在命令後面追加“+引數”,如果想要把某個隱藏功能移出檔案,則需要追加“-引數”。chattr命令中可供選擇的隱藏許可權引數非常豐富,具體如下表所示。

引數 作用
i 無法對檔案進行修改;若對目錄設定了該引數,則僅能修改其中的子檔案內容而不能新建或刪除檔案
a 僅允許補充(追加)內容,無法覆蓋/刪除內容(Append Only)
S 檔案內容在變更後立即同步到硬碟(sync)
s 徹底從硬碟中刪除,不可恢復(用0填充原檔案所在硬碟區域)
A 不再修改這個檔案或目錄的最後訪問時間(atime)
b 不再修改檔案或目錄的存取時間
D 檢查壓縮檔案中的錯誤
d 使用dump命令備份時忽略本檔案/目錄
c 預設將檔案或目錄進行壓縮
u 當刪除該檔案後依然保留其在硬碟中的資料,方便日後恢復
t 讓檔案系統支援尾部合併(tail-merging)
x 可以直接訪問壓縮檔案中的內容

    先來建立一個普通檔案,然後立即嘗試刪除(這個操作肯定會成功):

[[email protected]
testdir]# echo "hello world" > test1 [[email protected] testdir]# rm -r test1 rm:是否刪除普通檔案 "test1"?y [[email protected] testdir]#

    接下來我們再次新建一個普通檔案,併為其設定不允許刪除與覆蓋(+a引數)許可權,然後再嘗試將這個檔案刪除:

[[email protected] testdir]# echo "hello world" > test2
[[email protected] testdir]# chattr +a test2 
[[email protected] testdir]# rm -r test2               
rm:是否刪除普通檔案 "test2"?y
rm: 無法刪除"test2": 不允許的操作
[[email protected] testdir]# 

2.  lsattr命令

    lsattr命令用於顯示檔案的隱藏許可權,格式為“lsattr [引數] 檔案”。在Linux系統中,檔案的隱藏許可權必須使用lsattr命令來檢視,平時使用的ls之類的命令則看不出端倪:

[[email protected] testdir]# ll -a test2 
-rw-r--r--. 1 root root 12 8月   9 21:33 test2
[[email protected] testdir]# lsattr test2 
-----a---------- test2
[[email protected] testdir]# chattr -a test2 
[[email protected] testdir]# lsattr test2    
---------------- test2
[[email protected] testdir]# rm -r test2 
rm:是否刪除普通檔案 "test2"?y
[[email protected] testdir]# 

3.檔案訪問控制列表

    不知道大家是否發現,前文講解的一般許可權、特殊許可權、隱藏許可權其實有一個共性—許可權是針對某一類使用者設定的。如果希望對某個指定的使用者進行單獨的許可權控制,就需要用到檔案的訪問控制列表(ACL)了。通俗來講,基於普通檔案或目錄設定ACL其實就是針對指定的使用者或使用者組設定檔案或目錄的操作許可權。另外,如果針對某個目錄設定了ACL,則目錄中的檔案會繼承其ACL;若針對檔案設定了ACL,則檔案不再繼承其所在目錄的ACL。

    為了更直觀地看到ACL對檔案許可權控制的強大效果,我們先切換到普通使用者,然後嘗試進入root管理員的家目錄中。在沒有針對普通使用者對root管理員的家目錄設定ACL之前,其執行結果如下所示:

[[email protected] testdir]# su - user1
上一次登入:四 8月  9 15:14:33 CST 2018pts/0 上
[[email protected] ~]$ cd /root
-bash: cd: /root: 許可權不夠
[[email protected] ~]$ exit
登出
[[email protected] testdir]# 

3.1  setfacl命令

    setfacl命令用於管理檔案的ACL規則,格式為“setfacl [引數] 檔名稱”。檔案的ACL提供的是在所有者、所屬組、其他人的讀/寫/執行許可權之外的特殊許可權控制,使用setfacl命令可以針對單一使用者或使用者組、單一檔案或目錄來進行讀/寫/執行許可權的控制。其中,針對目錄檔案需要使用-R遞迴引數;針對普通檔案則使用-m引數;如果想要刪除某個檔案的ACL,則可以使用-b引數。下面來設定使用者在/root目錄上的許可權:

[[email protected] testdir]# setfacl -Rm u:user1:rwx /root
[[email protected] testdir]# su - user1
上一次登入:四 8月  9 21:40:11 CST 2018pts/0 上
[[email protected] ~]$ cd /root    
[[email protected] root]$ ll
總用量 22708
-rw-rwx---+ 1 root root     1702 7月  23 22:30 anaconda-ks.cfg
-rwxrwxr-x+ 1 root root    10486 8月   1 15:03 sys_check.sh
drwxrwxr-t+ 2 root root        6 8月   9 21:36 testdir
-rw-rwxr--+ 1 root root  3118130 8月   6 2012 wget-1.14.tar.gz
[[email protected] root]$ exit
登出
[[email protected] testdir]# 

    怎麼去檢視檔案上有那些ACL呢?常用的ls命令是看不到ACL表資訊的,但是卻可以看到檔案的許可權最後一個點(.)變成了加號(+),這就意味著該檔案已經設定了ACL了。

[[email protected] ~]# ll -d /root
dr-xrwx---+ 8 root root 4096 8月   9 15:20 /root
[[email protected] ~]# 

3.2  getfacl命令

    getfacl命令用於顯示檔案上設定的ACL資訊,格式為“getfacl 檔名稱”。想要設定ACL,用的是setfacl命令;要想檢視ACL,則用的是getfacl命令。下面使用getfacl命令顯示在root管理員家目錄上設定的所有ACL資訊。

[[email protected] ~]# getfacl /root
getfacl: Removing leading '/' from absolute path names
# file: root
# owner: root
# group: root
user::r-x
user:user1:rwx
group::r-x
mask::rwx
other::---

[[email protected] ~]# 

詳細學習,請見)