1. 程式人生 > >Linux基礎知識之檔案隱藏屬性

Linux基礎知識之檔案隱藏屬性

這幾天溫習下 Linux 基礎知識,基礎裡面較為薄弱的知識點我會在這裡記錄一下,以便回頭檢視

Linux 下的檔案除了許可權屬性,還有一些隱藏屬性,必須使用lsattr來顯示,如下所示:

Shell
12 [root@localhost~]# lsattr test   -------------test

結果中第一列是 13 個短橫杆,其中每一個橫杆都是一個屬性,如果當前位置上設定了該屬性就會顯示相對應的字元。

如果要設定檔案的隱藏屬性,需要使用chattr命令。這裡介紹幾個常用的隱藏屬性,第一種是a屬性。擁有這種屬性的檔案只能在尾部增加資料而不能被刪除。個人覺得用於重要的日誌類檔案非常不錯,即可以繼續追加內容,又可以避免被惡意刪除。

下面例項使用chattr來給該檔案新增a屬性並測試:

Shell
12345678 [root@localhost~]# ll test   -rwxrwxrwx1root root187Dec 507:55test[root@localhost~]# chattr +a test   [root@localhost~]# lsattr test   -----a-------test[root@localhost~]# rm -rf test   rm:cannot remove`test':Operation notpermitted[root@localhost
~]#

如上所示,設定了a屬性的檔案(夾),即便許可權是 777,即便是 root 使用者都不能刪除它,但是實際上可以以尾部新增(append)的方式繼續像該檔案中寫入內容:

Shell
1234567891011 [root@localhost~]# lsattr test   -----a-------test[root@localhost~]# cat test   first line[root@localhost~]# >test   -bash:test:Operation notpermitted[root@localhost~]# echo second line >>test   [root@localhost~]# cat test   first line   second line[root@localhost~]#

還有一種比較常見的屬性是i屬性。設定了這種屬性的檔案將無法寫入、改名、刪除,即使是 root 使用者也不行。這種屬性常用於設定在系統或者關鍵服務中的配置檔案,這對提升系統安全性有很大幫助。

更多隱藏屬性請使用man chattr檢視。