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


系统运维技巧(一)——chattr和lsattr命令
 
        我们有这样的需求:一个文件不允许别人修改、删除或者只允许添加,我们就可以使用chattr命令。
 
[plain] 
[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  
 

相关内容

    暂无相关文章