系统运维技巧(一)——chattr和lsattr命令

        我们有这样的需求:一个文件不允许别人修改、删除或者只允许添加,我们就可以使用chattr命令。


[root@serv01 test]# cat note.txt 
1.不许迟到
2.不许早退
3.不许抽烟
[root@serv01 test]# chattr +i note.txt

#root用户都不能编辑
[root@serv01 test]# vim note.txt 
[root@serv01 test]# cat note.txt
1.不许迟到
2.不许早退
3.不许抽烟

#root用户不能删除 修改
[root@serv01 test]# rm -f note.txt
rm: cannot remove `note.txt': Operation not permitted
[root@serv01 test]# echo "this is test" >> note.txt
-bash: note.txt: Permission denied

#ll查看文件属性不能看到,需要使用lsattr查看
[root@serv01 test]# ll note.txt
-rw-r--r--. 1 root root 45 Sep 21 17:46 note.txt
[root@serv01 test]# lsattr note.txt
----i--------e- note.txt

#root用户不能修改只是个相对概念,root用户可以修改文件的属性,就可以修改了
[root@serv01 test]# chattr -i note.txt
[root@serv01 test]# lsattr note.txt
-------------e- note.txt
[root@serv01 test]# vim note.txt
[root@serv01 test]# cat note.txt
1.不许迟到
2.不许早退
3.不许抽烟
4.可以不来上班

#留言板:自己说的话不能删除
[root@serv01 test]# vim note.txt
[root@serv01 test]# cat note.txt
1.不许迟到
2.不许早退
3.不许抽烟
4.可以不来上班
hello
[root@serv01 test]# chattr +a note.txt
[root@serv01 test]# lsattr note.txt
-----a-------e- note.txt
[root@serv01 test]# vim note.txt
[root@serv01 test]# ls
note.txt  note.txt~  note.txy~ note.txz~  
#删除,发现没有权限 
[root@serv01 test]# rm -rf *
rm: remove regular file `note.txt'? y
rm: cannot remove `note.txt': Operation not permitted
[root@serv01 test]# ls
note.txt  upload
[root@serv01 test]# echo "hello world" > note.txt 
-bash: note.txt: Operation not permitted
#只能使用追加的方式添加内容
[root@serv01 test]# echo "hello world" >> note.txt 
[root@serv01 test]# cat note.txt 
1.不许迟到
2.不许早退
3.不许抽烟
4.可以不来上班
hello
hello world

[root@serv01 test]# chattr -a note.txt 
[root@serv01 test]# lsattr note.txt 
-------------e- note.txt


  我的邮箱wgbno27@163.com
  新浪微博@Wentasy27         
  微信公众平台:JustOracle(微信号:justoracle)
  数据库技术交流群:336882565(加群时验证 From CSDN XXX)
  Oracle交流讨论组https://groups.google.com/d/forum/justoracle
  By Larry Wen


katoonSina CSDN
@Wentasy 格物、致知、诚意、正心、修身、齐家、治国、平天下。Focus,Persist,Master. 成都信息工程学院 温国兵
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值