修改文件系统属性chattr,查看文件系统属性lsattr

chattr

chattr +i 文件或目录 , chattr +a 文件或目录,chattr -i 文件或目录,chattr -a 文件或目录,chattr =i 文件或目录,chattr =a 文件或目录

-i 对文件的作用:对文件设置了i属性则文件只读,不能对文件内容进行修改,不可删文件,不可重命名文件,此权限限制对root也有效。

[root@localhost ~]# touch testfile
[root@localhost ~]# ll
总用量 0
-rw-r--r--. 1 root root 0 8月   6 09:44 testfile  注释:默认权限644
[root@localhost ~]# echo hello>testfile  注释:可写
[root@localhost ~]# cat testfile 
hello
[root@localhost ~]# chattr +i testfile   注释:增加i属性
[root@localhost ~]# lsattr -a testfile   注释:查看文件系统属性
----i----------- testfile
[root@localhost ~]# echo world>>testfile  注释:不可写
-bash: testfile: 权限不够  
[root@localhost ~]# rm testfile   注释:不可删
rm:是否删除普通文件 "testfile"?y
rm: 无法删除"testfile": 不允许的操作
[root@localhost ~]# mv testfile t  注释:不可重命名
mv: 无法将"testfile" 移动至"t": 不允许的操作
[root@localhost ~]# rm -rf testfile 
rm: 无法删除"testfile": 不允许的操作

 -i对目录的作用:只能修改目录内已存在文件内容,不能在目录内新建、删除文件。

[root@localhost ~]# mkdir testdir
[root@localhost ~]# ll -d testdir/
drwxr-xr-x. 2 root root 6 8月   6 09:57 testdir/
[root@localhost ~]# touch testdir/testfileA
[root@localhost ~]# chattr +i testdir/
[root@localhost ~]# lsattr -a testdir/
----i----------- testdir/.  注释:目录本身具有i属性
---------------- testdir/..
---------------- testdir/testfileA
[root@localhost ~]# echo "hello world" > testdir/testfileA   注释:可对目录内已存在的文件的内容进行修改
[root@localhost ~]# cat testdir/testfileA 
hello world
[root@localhost ~]# rm testdir/testfileA -rf  注释:不可在目录内删除文件
rm: 无法删除"testdir/testfileA": 权限不够
[root@localhost ~]# touch testdir/testfileB  注释:不可在目录内添加文件
touch: 无法创建"testdir/testfileB": 权限不够

 取消-i属性:chattr -i 文件或目录

-a属性对文件的作用:只能对文件查看、增加数据,不能修改、删除数据,不能重命名、删除文件。较i属性宽松点,可以对文件追加新内容。a可理解为add

[root@localhost ~]# touch testfileA
[root@localhost ~]# ll testfileA 
-rw-r--r--. 1 root root 0 8月   6 10:07 testfileA
[root@localhost ~]# chattr +a testfileA  注释:给文件添加a属性
[root@localhost ~]# lsattr -a testfileA  注释:查看文件系统属性
-----a---------- testfileA
[root@localhost ~]# echo "nihao">testfileA  注释:文件a属性不能修改文件内容
-bash: testfileA: 不允许的操作
[root@localhost ~]# echo "nihao">>testfileA  注释:文件a属性可以追加文件内容
[root@localhost ~]# cat testfileA  注释:文件a属性可能查看文件内容
nihao
[root@localhost ~]# mv testfileA testfileB  注释:文件a属性不能重命名文件
mv: 无法将"testfileA" 移动至"testfileB": 不允许的操作
[root@localhost ~]# rm -rf testfileA  注释:文件a属性不能删除文件
rm: 无法删除"testfileA": 不允许的操作
[root@localhost ~]# ls
testdir  testfile  testfileA

 -a属性对目录的作用:对目录内已存在文件不能重命名、删除,可修改目录内已存在文件内容,可在目录内新建文件。

[root@localhost ~]# mkdir testdirB
[root@localhost ~]# touch testdirB/testfileB
[root@localhost ~]# chattr +a testdirB
[root@localhost ~]# lsattr -a testdirB
-----a---------- testdirB/.
---------------- testdirB/..
---------------- testdirB/testfileB
[root@localhost ~]# mv testdirB/testfileB testdirB/testfileC  注释:不能对目录内已存在文件重命名
mv: 无法将"testdirB/testfileB" 移动至"testdirB/testfileC": 不允许的操作
[root@localhost ~]# rm -rf testdirB/testfileB   注释:不能删除目录内文件
rm: 无法删除"testdirB/testfileB": 不允许的操作
[root@localhost ~]# echo "welcome China">testdirB/testfileB  注释:可修改目录内文件内容
[root@localhost ~]# cat testdirB/testfileB 
welcome China
[root@localhost ~]# touch testdirB/testfileC  注释:可在目录内新建文件
[root@localhost ~]#

 总结:i属性为只读,a属性为追加。

原文地址:https://www.cnblogs.com/xiongjiawei/p/7293914.html