chattr +a file1 只允许以追加方式读写文件
chattr +c file1 允许这个文件能被内核自动压缩/解压
chattr +d file1 在进行文件系统备份时,dump程序将忽略这个文件
chattr +i file1 设置成不可变的文件,不能被删除、修改、重命名或者链接
chattr +s file1 允许一个文件被安全地删除
chattr +S file1 一旦应用程序对这个文件执行了写操作,使系统立刻把修改的结果写到磁盘
chattr +u file1 若文件被删除,系统会允许你在以后恢复这个被删除的文件
lsattr 显示特殊的属性
=====================
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