Linux文件的特殊属性
chattr
对于一些特殊的文件,为防止用户误操作,可以加特殊属性,示例如下:
+i:锁定
示例文件:
[[email protected] data]#ll test.txt
-rw-r--r--. 1 root root 48 Mar 12 19:30 test.txt
+i锁定文件
[[email protected] data]#chattr +i test.txt
查看文件属性,发现与之前一样
[[email protected] data]#ll test.txt
-rw-r--r--. 1 root root 48 Mar 12 19:30 test.txt
进行验证+i功能是否生效
重定向内容:
[[email protected] data]#echo 1111 > test.txt
** -bash: test.txt: Permission denied **
查看一下:
[[email protected] data]#cat test.txt
abcdedf
abcdedf
abcdedf
fdedcba
fdedcba
fdedcba
重命名:
[[email protected] data]#mv test.txt haha.txt
**mv: cannot move ‘test.txt’ to ‘haha.txt’: Operation not permitted**
删除文件:
[[email protected] data]#rm -f test.txt
**mv: cannot remove ‘test.txt’: Operation not permitted**
+a:只能追加
示例文件:仍然采用上次测试的test.txt文件
[[email protected] data]#chattr +a test.txt
重定向内容:
[[email protected] data]#echo 222 > test.txt
**-bash: test.txt: Operation not permitted**
重命名:
[[email protected] data]#mv test.txt haha.txt
**mv: cannot move ‘test.txt’ to ‘haha.txt’: Operation not permitted**
删除:
[[email protected] data]#rm -f test.txt
**mv: cannot remove ‘test.txt’: Operation not permitted**
追加内容:
[[email protected] data]#echo 222 >> test.txt
[[email protected] data]#cat test.txt
abcdedf
abcdedf
abcdedf
fdedcba
fdedcba
fdedcba
222
+A:锁定atime的时间,减少读文件对磁盘IO的影响,以提高性能。
[[email protected] data]#chattr +a test.txt
第一次读取,记录atime的时间
[[email protected] data]#cat test.txt
333
444
555
[[email protected] 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时间没有变化,如下:
[[email protected] data]#cat test.txt
333
444
555
[[email protected] 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对应的属性
原文地址:https://blog.51cto.com/14436237/2419437
时间: 2024-11-03 13:50:07