linux系統下有許多文件權限設定的命令,本文主要介紹chattr工具給文件加隱藏屬性,可以增加重要文件的安全性。
一、給文件加a屬性,增加該屬性後,該文件只能追加內容而不能覆蓋刪除,lsattr查看chattr權限
1.添加a屬性
[root@www ~]# lsattr 1.txt
-------------e- 1.txt
[root@www ~]# chattr +a 1.txt
[root@www ~]# lsattr 1.txt
-----a-------e- 1.txt
[root@www ~]# echo 111112222244444 > 1.txt //無法覆蓋
-bash: 1.txt: Operation not permitted
[root@www ~]# echo 111112222244444 >> 1.txt //追加內容
[root@www ~]# cat 1.txt
111112222244444
[root@www ~]# rm -r 1.txt
rm: remove regular file `1.txt'? y
rm: cannot remove `1.txt': Operation not permitted //無法刪除
2、去除a屬性
[root@www ~]# chattr -a 1.txt
[root@www ~]# echo 55555 > 1.txt //覆蓋內容
[root@www ~]# cat 1.txt
55555
二、給文件加i屬性,增加該屬性後,該文件無法修改、刪除
1、添加i屬性
[root@www ~]# chattr +i 2.txt
[root@www ~]# echo 1111 > 2.txt
-bash: 2.txt: Permission denied
[root@www ~]# echo 1111 >> 2.txt
-bash: 2.txt: Permission denied
[root@www ~]# rm -rf 2.txt
rm: cannot remove `2.txt': Operation not permitted
2、去除i屬性
[root@www ~]# chattr -i 2.txt
[root@www ~]# echo 2222 > 2.txt
[root@www ~]# cat 2.txt
2222
三、查看目錄及其下所有文件的chattr屬性,加-R選項
[root@www ~]# lsattr -R 222
-------------e- 222/222
222/222:
-------------e- 222/222/111
222/222/111:
-------------e- 222/222/111/123.txt
-------------e- 222/123.txt
-------------e- 222/install.log
[root@www ~]# chattr +i 222/222/111/123.txt
[root@www ~]# rm -rf 222
rm: cannot remove `222/222/111/123.txt': Operation not permitted
[root@www ~]# lsattr -R 222
-------------e- 222/222
222/222:
-------------e- 222/222/111
222/222/111:
----i--------e- 222/222/111/123.txt
-------------e- 222/123.txt
-------------e- 222/install.log
四、給目錄增加chattr權限,加-d選項
[root@www ~]# lsattr -d 222
-------------e- 222
[root@www ~]# chattr +a 444
[root@www ~]# lsattr -d 444
-----a-------e- 444