现在的位置: 首页 > 综合 > 正文

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

2013年10月08日 ⁄ 综合 ⁄ 共 1707字 ⁄ 字号 评论关闭

        我们有这样的需求:一个文件不允许别人修改、删除或者只允许添加,我们就可以使用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
katoon Sina CSDN
@Wentasy 格物、致知、诚意、正心、修身、齐家、治国、平天下。Focus,Persist,Master. 成都信息工程学院 温国兵

抱歉!评论已关闭.