天天看點

linux檔案屬性管理,是不是隻學會chmod,ls?試試chattr

chattr指令簡介

在linux系統中,檔案的屬性在某種程度上決定了檔案的活動方式。一個屬性可以決定檔案是否可以被修改、删除、重命名,壓縮等等。chattr可以修改linux系統中檔案、檔案夾的屬性。可以設定檔案的屬性,讓其具有一定的安全性,保證除了管理者之外的使用者不能夠修改檔案中的内容。

檔案屬性模型

下面是檔案可以設定的部分屬性:

A: 當使用者通路檔案時,事件記錄不會被修改。
a: 檔案隻能以追加模式(append)寫入。
c: 磁盤上的檔案自動被核心壓縮。
D: 修改被同步寫入磁盤。
d: 當dump程式執行時,檔案不會被備份。
E: 表明壓縮檔案有一個壓縮錯誤。
e: 該檔案正在使用區段映射磁盤上的塊.
I: 檔案被htree代碼使用,即一個檔案夾通過hash來索引。
i: 一個檔案不可以被修改:不能被删除、重命名、不能建立link、不能寫入資料。
j: 具有'j'屬性的檔案在寫入檔案本身之前,将其所有資料寫入ext3日志。
s: 當一個帶有's'屬性集的檔案被删除時,它的塊被置零并寫回磁盤。
S: 當檔案被修改,變化将被同步寫入磁盤。
           

chattr 和 lsattr 指令的文法

chattr指令的文法:

$ sudo chattr [option] [mode] files
           

mode的格式為:+-=[acdeijstuADST]

“+” 被用來添加屬性。

“-” 用來删除選中的屬性。

“=” 表明選中的屬性為最終設定的屬性。

chattr一次可以設定多個屬性。

option 支援的選項:

  • -R :遞歸修改一個檔案夾及其所有子檔案、子檔案夾的屬性。
  • -V: 顯示指令詳細資訊和版本資訊。
  • -f: 不顯示錯誤消息

lsattr指令文法:

$ lsattr [option] file
           

option選項:

  • -R :遞歸修改一個檔案夾及其所有子檔案、子檔案夾的屬性。
  • -V: 顯示指令詳細資訊和版本資訊。
  • -a:顯示檔案夾下所有檔案。

示例

1 lsattr顯示檔案或檔案夾的屬性

lsattr指令的參數為一個檔案或者檔案夾,進而檢視他們的屬性。

$ lsattr file
           

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp$ lsattr awk_test
--------------e----- awk_test
yunzhong@DESKTOP-9VB7LN7:/tmp$ lsattr ./
--------------e----- ./header.awk
--------------e----- ./print.awk
--------------e----- ./python7036178842941535246
--------------e----- ./friutes.txt-new
--------------e----- ./friutes.txt
--------------e----- ./cat.temp.file
--------------e----- ./friutes.txt2
--------------e----- ./cmd.awk
--------------e----- ./test-dir
--------------e----- ./ls
--------------e----- ./hsperfdata_yunzhong
--------------e----- ./awk_test
           

如果沒有指定任何的檔案或者檔案夾,則lsattr列舉目前檔案夾下所有的檔案屬性。

yunzhong@DESKTOP-9VB7LN7:/tmp$ lsattr
--------------e----- ./header.awk
--------------e----- ./print.awk
--------------e----- ./python7036178842941535246
--------------e----- ./friutes.txt-new
--------------e----- ./friutes.txt
--------------e----- ./cat.temp.file
--------------e----- ./friutes.txt2
--------------e----- ./cmd.awk
--------------e----- ./test-dir
--------------e----- ./ls
--------------e----- ./hsperfdata_yunzhong
--------------e----- ./awk_test
           

2 chattr修改檔案屬性,隻能以append的模式打開

為檔案添加‘a’屬性,讓其隻能被append模式打開。

$ sudo chattr +a file
           

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp$ lsattr friutes.txt
--------------e----- friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp$ sudo chattr +a friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp$ lsattr friutes.txt
-----a--------e----- friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp$ echo 'hello world' > friutes.txt
-bash: friutes.txt: Operation not permitted
yunzhong@DESKTOP-9VB7LN7:/tmp$ echo 'hello world' >> friutes.txt
           

為檔案添加‘a’權限後,隻能追加内容到檔案,不能覆寫檔案。

3 使用chattr指令讓檔案不可變

可以為檔案添加屬性'i' ,讓檔案不可變。進而保證檔案不可以被删除,修改,重命名。

$ chattr +i  file
           

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp$ sudo chattr +i friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp$ mv friutes.txt friutes.txt.bak
mv: cannot move 'friutes.txt' to 'friutes.txt.bak': Operation not permitted
           

4 使用chattr指令删除檔案屬性

可以使用'-'來删除檔案屬性。

$ sudo chattr -attribute file
           

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp$ lsattr friutes.txt
----ia--------e----- friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp$ sudo chattr -i friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp$ lsattr friutes.txt
-----a--------e----- friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp$ echo 'hello again' > friutes.txt
-bash: friutes.txt: Operation not permitted
yunzhong@DESKTOP-9VB7LN7:/tmp$ echo 'hello again' >> friutes.txt
           

5 chattr指定檔案的屬性

使用 '='來設定檔案的屬性。

指令格式:

$ sudo chattr =attr file
           

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp$ sudo chattr =e friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp$ lsattr friutes.txt
--------------e----- friutes.txt
           

6 lsattr指令遞歸的查詢檔案夾下所有檔案的屬性

可以使用-R選項,遞歸展示檔案夾下所有檔案。

$ lsattr -R dir
           

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp$ lsattr
--------------e----- ./header.awk
--------------e----- ./print.awk
--------------e----- ./python7036178842941535246
--------------e----- ./friutes.txt-new
-----a--------e----- ./friutes.txt
--------------e----- ./cat.temp.file
--------------e----- ./friutes.txt2
--------------e----- ./cmd.awk
--------------e----- ./test-dir
--------------e----- ./zeppelin-index
--------------e----- ./python340083847531222206
--------------e----- ./ls
--------------e----- ./hsperfdata_yunzhong
--------------e----- ./awk_test
yunzhong@DESKTOP-9VB7LN7:/tmp$ lsattr -R
--------------e----- ./header.awk
--------------e----- ./print.awk
--------------e----- ./python7036178842941535246

./python7036178842941535246:
--------------e----- ./python7036178842941535246/mpl_config.py
--------------e----- ./python7036178842941535246/py4j-src-0.10.7.zip
--------------e----- ./python7036178842941535246/backend_zinline.py
--------------e----- ./python7036178842941535246/zeppelin_context.py
--------------e----- ./python7036178842941535246/zeppelin_python.py

--------------e----- ./friutes.txt-new
-----a--------e----- ./friutes.txt
--------------e----- ./cat.temp.file
--------------e----- ./friutes.txt2
--------------e----- ./cmd.awk
--------------e----- ./test-dir
...
           

7 chattr指令遞歸修改檔案夾下所有的檔案屬性

與lsattr相似,可以使用-R參數遞歸修改檔案夾下所有的檔案屬性。

$ sudo chattr +a -R dir
           

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp$ ls test-dir/
testfile  testfile2
yunzhong@DESKTOP-9VB7LN7:/tmp$ sudo chattr +a -R test-dir/
yunzhong@DESKTOP-9VB7LN7:/tmp$ lsattr test-dir/
-----a--------e----- test-dir/testfile2
-----a--------e----- test-dir/testfile
           

8 使用lsattr指令檢視檔案夾下所有的檔案

-a參數可以輸出檔案夾下所有的檔案,包括’.‘開頭的檔案。

$ lsattr -a [dir]
           

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp$ lsattr -a ./
--------------e----- ./header.awk
--------------e----- ./print.awk
--------------e----- ./python7036178842941535246
--------------e----- ./friutes.txt-new
--------------e----- ./friutes.txt
--------------e----- ./cat.temp.file
--------------e----- ./friutes.txt2
--------------e----- ./cmd.awk
-----a--------e----- ./test-dir
--------------e----- ./zeppelin-index
--------------e----- ./python340083847531222206
--------------e----- ./ls
--------------e----- ./hsperfdata_yunzhong
--------------e----- ./..
--------------e----- ./awk_test
--------------e----- ./.
           

9. lsattr隻輸出檔案夾屬性

一般情況下,lsattr會輸出指定檔案夾内的檔案屬性。可以通過-d屬性,隻輸出檔案夾的屬性。指令格式。

$ lsattr -d dir
           

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp$ lsattr -d test-dir/
-----a--------e----- test-dir/
           

10 打開或消除錯誤提示資訊

當執行chattr指令時,會在終端提示錯誤資訊。使用-f參數可以關閉錯誤提示資訊,但并不是所有的錯誤資訊都會被關閉。

$ chattr -f +a file
           

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp$ chattr +a friutes.txt
chattr: Operation not permitted while setting flags on friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp$ chattr -f +a friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp$
           

11 chattr輸出版本資訊

預設情況下,chattr沒有任何輸出。可以使用-V參數輸出資訊,同時也輸出了chattr指令的版本資訊。

$ sudo chattr -V +a file
           

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp$ sudo chattr -V +a friutes.txt
[sudo] password for yunzhong:
chattr 1.45.5 (07-Jan-2020)
Flags of friutes.txt set as -----a--------e-----
           

12 lsattr 輸出檔案的版本、生成号等資訊

$ lsattr -v file
           

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp$ lsattr -v friutes.txt
3866499331 -----a--------e----- friutes.txt           

繼續閱讀