Linux文件的特殊属性chattr工具

本文详细介绍了Linux系统中chattr命令的使用方法,包括如何给文件添加a和i属性以增强安全性,以及如何查看和设置目录及其子文件的隐藏属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        linux系统下有许多文件权限设定的命令,本文主要介绍chattr工具给文件加隐藏属性,可以增加重要文件的安全性。

一、给文件加a属性,增加该属性后,该文件只能追加内容而不能覆盖删除,lsattr查看chattr权限

1.添加a属性

[root@www ~]# lsattr 1.txt    
-------------e- 1.txt

[root@www ~]# chattr +a 1.txt   
[root@www ~]# lsattr 1.txt 
-----a-------e- 1.txt

[root@www ~]# echo 111112222244444 > 1.txt       //无法覆盖
-bash: 1.txt: Operation not permitted
[root@www ~]# echo 111112222244444 >> 1.txt    //追加内容
[root@www ~]# cat 1.txt 
111112222244444
[root@www ~]# rm -r 1.txt 
rm: remove regular file `1.txt'? y
rm: cannot remove `1.txt': Operation not permitted   //无法删除

2、去除a属性

[root@www ~]# chattr -a 1.txt 

[root@www ~]# echo 55555 > 1.txt     //覆盖内容
[root@www ~]# cat 1.txt 
55555


二、给文件加i属性,增加该属性后,该文件无法修改、删除

1、添加i属性

[root@www ~]# chattr +i 2.txt 
[root@www ~]# echo 1111 > 2.txt 
-bash: 2.txt: Permission denied
[root@www ~]# echo 1111 >> 2.txt 
-bash: 2.txt: Permission denied
[root@www ~]# rm -rf 2.txt 
rm: cannot remove `2.txt': Operation not permitted

2、去除i属性

[root@www ~]# chattr -i 2.txt
[root@www ~]# echo 2222 > 2.txt
[root@www ~]# cat 2.txt
2222


三、查看目录及其下所有文件的chattr属性,加-R选项

[root@www ~]# lsattr -R 222
-------------e- 222/222
222/222:
-------------e- 222/222/111
222/222/111:
-------------e- 222/222/111/123.txt
-------------e- 222/123.txt
-------------e- 222/install.log


[root@www ~]# chattr +i 222/222/111/123.txt

[root@www ~]# rm -rf 222
rm: cannot remove `222/222/111/123.txt': Operation not permitted

[root@www ~]# lsattr -R 222
-------------e- 222/222
222/222:
-------------e- 222/222/111
222/222/111:
----i--------e- 222/222/111/123.txt
-------------e- 222/123.txt
-------------e- 222/install.log


四、给目录增加chattr权限,加-d选项

[root@www ~]# lsattr -d 222
-------------e- 222
[root@www ~]# chattr +a 444
[root@www ~]# lsattr -d 444
-----a-------e- 444

本文转自 HMLinux 51CTO博客,原文链接:http://blog.51cto.com/7424593/1719932



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值