linux系统中的文件隐藏权限、chattr命令及lsattr命令

linux系统中文件除了具备一般权限和特殊权限外,还有一种隐藏权限,即被隐藏起来的权限

chattr命令用于增减文件和目录的隐藏权限

1、 chattr -i 选项 不能删除文件、不能对文件进行修改

[root@linuxprobe home]# whoami
root
[root@linuxprobe home]# mkdir test
[root@linuxprobe home]# chmod 777 test/
[root@linuxprobe home]# touch a.txt
[root@linuxprobe home]# chmod 777 a.txt
[root@linuxprobe home]# rm -f a.txt  ## 没有 +i隐藏全选,可以直接删除
[root@linuxprobe home]# touch b.txt
[root@linuxprobe home]# chmod 777 b.txt
[root@linuxprobe home]# chattr +i b.txt  ## 加 +i 隐藏权限
[root@linuxprobe home]# rm -f b.txt  ## 不能删除
rm: cannot remove ‘b.txt’: Operation not permitted
[root@linuxprobe home]# lsattr b.txt  ## 查看隐藏权限
----i----------- b.txt
[root@linuxprobe home]# chattr -i b.txt  ## 减去隐藏权限
[root@linuxprobe home]# lsattr b.txt
---------------- b.txt
[root@linuxprobe home]# rm -f b.txt  ## 可以删除

chattr +i 文件,不能对文件进行修改

[root@linuxprobe test]# ls
[root@linuxprobe test]# seq 5 > a.txt ## 测试数据
[root@linuxprobe test]# lsattr a.txt
---------------- a.txt
[root@linuxprobe test]# cat a.txt
1
2
3
4
5
[root@linuxprobe test]# echo "xxxxx" >> a.txt  ## 可以正常修改
[root@linuxprobe test]# cat a.txt
1
2
3
4
5
xxxxx
[root@linuxprobe test]# chattr +i a.txt  ##  添加隐藏权限
[root@linuxprobe test]# lsattr a.txt
----i----------- a.txt
[root@linuxprobe test]# echo "yyyyy" >> a.txt  ## 不能修改文件内容
-bash: a.txt: Permission denied
[root@linuxprobe test]# chattr -i a.txt  ## 减去隐藏权限
[root@linuxprobe test]# lsattr a.txt
---------------- a.txt
[root@linuxprobe test]# echo "yyyyy" >> a.txt  ##可以对文件进行修改
[root@linuxprobe test]# cat a.txt
1
2
3
4
5
xxxxx
yyyyy

chattr +i 目录,可以修改其中的文件,不能新建文件、目录,不能删除文件目录

[root@linuxprobe test]# mkdir -p test01/dir01  
[root@linuxprobe test]# mkdir -p test02/dir02
[root@linuxprobe test]# touch test01/a.txt
[root@linuxprobe test]# touch test02/b.txt
[root@linuxprobe test]# chmod -R 777 *
[root@linuxprobe test]# tree  ## 测试目录、数据
.
├── test01
│   ├── a.txt
│   └── dir01
└── test02
    ├── b.txt
    └── dir02

4 directories, 2 files
[root@linuxprobe test]# echo "xxxxx" > test01/a.txt  ## 可以修改文件
[root@linuxprobe test]# touch test01/c.txt  ## 可以创建文件
[root@linuxprobe test]# rm -f test01/a.txt  ## 可以删除文件
[root@linuxprobe test]# rm -rf test01/dir01/ ## 可以删除目录
[root@linuxprobe test]# lsattr -d test01  ## 查看隐藏权限
---------------- test01
[root@linuxprobe test]# chattr +i test02  ## 增加隐藏权限
[root@linuxprobe test]# lsattr -d test02  ## 查看隐藏权限
----i----------- test02
[root@linuxprobe test]# echo "xxxxxx" > test02/b.txt  ## 可以修改文件内容
[root@linuxprobe test]# touch test02/d.txt  ## 不能创建新文件
touch: cannot touch ‘test02/d.txt’: Permission denied
[root@linuxprobe test]# rm -f test02/b.txt  ## 不能删除文件
rm: cannot remove ‘test02/b.txt’: Permission denied
[root@linuxprobe test]# rm -rf test02/dir02/  ## 不能删除目录
rm: cannot remove ‘test02/dir02/’: Permission denied

2、 chattr +a  仅能够追加内容,不能覆盖和删除内容(常用于日志文件)

[root@linuxprobe test]# ls
[root@linuxprobe test]# seq 4 > a.txt
[root@linuxprobe test]# chmod 777 a.txt
[root@linuxprobe test]# ll
total 4
-rwxrwxrwx. 1 root root 8 Oct 20 23:51 a.txt
[root@linuxprobe test]# chattr +a a.txt  ## 增加隐藏权限
[root@linuxprobe test]# lsattr a.txt
-----a---------- a.txt
[root@linuxprobe test]# echo "xxxxx" > a.txt  ## 不能够覆盖
-bash: a.txt: Operation not permitted
[root@linuxprobe test]# echo "xxxxx" >> a.txt  ## 可以追加
[root@linuxprobe test]# rm -f a.txt  ## 不能删除
rm: cannot remove ‘a.txt’: Operation not permitted
[root@linuxprobe test]# chattr -a a.txt
[root@linuxprobe test]# lsattr a.txt
---------------- a.txt
[root@linuxprobe test]# echo "xxxxx" > a.txt
[root@linuxprobe test]# echo "xxxxx" >> a.txt
[root@linuxprobe test]# rm -f a.txt
原文地址:https://www.cnblogs.com/liujiaxin2018/p/13849933.html