chattr:改变文件的扩展属性

xiaohai 2021-05-05 16:59:16 1423人围观 标签: Linux 
简介chattr命令用于改变文件的扩展属性。更加底层的控制文件的属性。

说明

  chattr命令用于改变文件的扩展属性。更加底层的控制文件的属性。

格式

  chattr [option] [file..]

常用参数说明

  • -R:递归更改目录的属性
  • -V:显示命令的执行过程
  • +:添加参数
  • -:移除参数
  • =:更新为指定参数
  • A:不能修改文件的最后访问时间
  • a:只能添加数据到文件,不能删除,主要用于日志文件
  • i:设定文件不能删除、改名、写入或者新增内容
示例
[root@localhost test]# lsattr test2.txt 
---------------- test2.txt
[root@localhost test]# chattr +a test2.txt #a参数使用
[root@localhost test]# lsattr test2.txt 
-----a---------- test2.txt
[root@localhost test]# echo 1111 > test2.txt  #不能清空
-bash: test2.txt: 不允许的操作
[root@localhost test]# echo 1111 >> test2.txt  #可以写入
[root@localhost test]# cat test2.txt 
fdasfsafsafdasfsafsafsadfs
1111
[root@localhost test]# rm -f test2.txt #不能删除
rm: 无法删除"test2.txt": 不允许的操作
[root@localhost test]# chattr -a test2.txt #移除参数a
[root@localhost test]# lsattr test2.txt 
---------------- test2.txt
[root@localhost test]# echo 222 > test2.txt #可以清空

[root@localhost test]# chattr +i test2.txt  #添加i参数
[root@localhost test]# lsattr test2.txt 
----i----------- test2.txt
[root@localhost test]# echo 333 > test2.txt #不能清空
-bash: test2.txt: 权限不够
[root@localhost test]# echo 333 >> test2.txt  #不能追加内容
-bash: test2.txt: 权限不够
[root@localhost test]# rm test2.txt  #不能移除
rm:是否删除普通文件 "test2.txt"?y
rm: 无法删除"test2.txt": 不允许的操作
[root@localhost test]# chattr -i test2.txt #移除i参数
[root@localhost test]# lsattr test2.txt 
---------------- test2.txt