Linux文件的特殊屬性

Linux文件的特殊屬性

chattr

    對於一些特殊的文件,爲防止用戶誤操作,可以加特殊屬性,示例如下:
    +i:鎖定
            示例文件:
                    [root@centos76 data]#ll test.txt
                    -rw-r--r--. 1 root root 48 Mar 12 19:30 test.txt

            +i鎖定文件
                    [root@centos76 data]#chattr +i test.txt

            查看文件屬性,發現與之前一樣
                    [root@centos76 data]#ll test.txt
                    -rw-r--r--. 1 root root 48 Mar 12 19:30 test.txt

            進行驗證+i功能是否生效
            重定向內容:
                    [root@centos76 data]#echo 1111 > test.txt
                    ** -bash: test.txt: Permission denied **

            查看一下:
                    [root@centos76 data]#cat test.txt
                    abcdedf
                    abcdedf
                    abcdedf
                    fdedcba
                    fdedcba
                    fdedcba

            重命名:
                    [root@centos76 data]#mv test.txt haha.txt
                    **mv: cannot move ‘test.txt’ to ‘haha.txt’: Operation not permitted**

            刪除文件:
                    [root@centos76 data]#rm -f test.txt
                    **mv: cannot remove ‘test.txt’: Operation not permitted**

    +a:只能追加
            示例文件:仍然採用上次測試的test.txt文件
                    [root@centos76 data]#chattr +a test.txt

            重定向內容:   
                    [root@centos76 data]#echo 222 > test.txt
                    **-bash: test.txt: Operation not permitted**

            重命名:   
                    [root@centos76 data]#mv test.txt haha.txt
                    **mv: cannot move ‘test.txt’ to ‘haha.txt’: Operation not permitted**

            刪除:
                    [root@centos76 data]#rm -f test.txt
                    **mv: cannot remove ‘test.txt’: Operation not permitted**

            追加內容:
                    [root@centos76 data]#echo 222 >> test.txt
                    [root@centos76 data]#cat test.txt
                    abcdedf
                    abcdedf
                    abcdedf
                    fdedcba
                    fdedcba
                    fdedcba
                    222                

    +A:鎖定atime的時間,減少讀文件對磁盤IO的影響,以提高性能。
            [root@centos76 data]#chattr +a test.txt

            第一次讀取,記錄atime的時間
                    [root@centos76 data]#cat test.txt
                    333
                    444
                    555
                    [root@centos76 data]#stat test.txt
                      File: ‘test.txt’
                      Size: 12          Blocks: 8          IO Block: 4096   regular file
                    Device: 803h/2051d  Inode: 83          Links: 1
                    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
                    Context: unconfined_u:object_r:etc_runtime_t:s0
                    **Access: 2019-03-18 11:12:50.425400248 +0800**
                    Modify: 2019-03-18 11:03:18.770446437 +0800
                    Change: 2019-03-18 11:12:42.139400918 +0800
                    Birth: -
            第二次讀取,atime時間沒有變化,如下:
                    [root@centos76 data]#cat test.txt
                    333
                    444
                    555
                    [root@centos76 data]#stat test.txt
                    File: ‘test.txt’
                    Size: 12            Blocks: 8          IO Block: 4096   regular file
                    Device: 803h/2051d  Inode: 83          Links: 1
                    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
                    Context: unconfined_u:object_r:etc_runtime_t:s0
                    **Access: 2019-03-18 11:12:50.425400248 +0800**
                    Modify: 2019-03-18 11:03:18.770446437 +0800
                    Change: 2019-03-18 11:12:42.139400918 +0800

注意:不管是+i/a/A,都會鎖定文件的atime時間

提示:由於rm -f做了別名設置,在使用此命令時,會提示mv操作

lsattr 可以查看chattr對應的屬性

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章