zoukankan      html  css  js  c++  java
  • Linux文件属性命令chattr

    该命令只有root有权限使用,并且设置后对root用户有效
    chattr [+-=] 选项 文件或目录
    常见选项说明:
    A:文件或目录的 atime (access time)不可被修改(modified), 可以有效预防例如手提电脑磁盘I/O错误的发生。
    S:硬盘I/O同步选项,功能类似sync。
    a:即append,设定该参数后,只能向文件中添加数据,而不能删除,多用于服务器日志文件安全,只有root才能设定这个属性。
    c:即compresse,设定文件是否经压缩后再存储。读取时需要经过自动解压操作。
    d:即no dump,设定文件不能成为dump程序的备份目标。
    i:设定文件不能被删除、改名、设定链接关系,同时不能写入或新增内容。i参数对于文件 系统的安全设置有很大帮助。
    j:即journal,设定此参数使得当通过mount参数:data=ordered 或者 data=writeback 挂 载的文件系统,文件在写入时会先被记录(在journal中)。如果filesystem被设定参数为 data=journal,则该参数自动失效。
    s:保密性地删除文件或目录,即硬盘空间被全部收回。
    u:与s相反,当设定为u时,数据内容其实还存在磁盘中,可以用于undeletion。
    各参数选项中常用到的是a和i。a选项强制只可添加不可删除,多用于日志系统的安全设定。而i是更为严格的安全设定,只有superuser (root) 或具有CAP_LINUX_IMMUTABLE处理能力(标识)的进程能够施加该选项。

    以最常见的选项i和a举例说明
    i:如果对文件设置i属性,那么不允许对文件进行删除、改名、增加和修改数据。
    如果对目录设置i属性,那么只能修改目录下文件的数据,但是不能创建新文件和删除已有文件

     1 /**对文件设置i属性**/
     2 [root@LAMP tmp]# touch one.file
     3 [root@LAMP tmp]# echo 'just a test' >> one.file
     4 [root@LAMP tmp]# cat one.file
     5 just a test
     6 [root@LAMP tmp]# chattr +i one.file 
     7 [root@LAMP tmp]# echo 'hello world' >> one.file       //不能添加文件内容
     8 -bash: one.file: Permission denied
     9 [root@LAMP tmp]# vim one.file   //vim可以打开文件,但是也不能修改、增加、删除文件内容
    10 [root@LAMP tmp]# mv one.file another.file      //不能重命名文件
    11 mv: cannot move `one.file' to `another.file': Operation not permitted
    12 [root@LAMP tmp]# rm -rf one.file          //不能删除文件
    13 rm: cannot remove `one.file': Operation not permitted
    14 
    15 /**对目录设置i属性**/
    16 [root@LAMP tmp]# mkdir test
    17 [root@LAMP tmp]# ll
    18 total 20
    19 -rw-r--r-- 1 root root   12 May 22 21:36 one.file
    20 drwxr-xr-x 2 root root 4096 May 22 21:47 test
    21 [root@LAMP tmp]# touch test/one.file
    22 [root@LAMP tmp]# echo 'just a test file' >> test/one.file 
    23 [root@LAMP tmp]# chattr +i test
    24 [root@LAMP tmp]# echo 'hello world' >> test/one.file 
    25 [root@LAMP tmp]# head test/one.file 
    26 just a test file
    27 hello world
    28 [root@LAMP tmp]# rm -rf test/one.file 
    29 rm: cannot remove `test/one.file': Permission denied
    30 [root@LAMP tmp]# touch test/another.file
    31 touch: cannot touch `test/another.file': Permission denied

    a:如果对文件设置了a属性,那么只能在文件中增加数据,但是不能删除、修改数据,
    如果对目录设置a属性,只能在目录中建立和修改文件但不能删除文件

     1 [root@LAMP tmp]# touch two.file
     2 [root@LAMP tmp]# echo 'hello worle' >> two.file   
     3 [root@LAMP tmp]# chattr +a two.file
     4 [root@LAMP tmp]# echo 'just a test' >> two.file  //可以追加文件内容,仅限于使用命令增减,不能使用vim等编辑器增加
     5 [root@LAMP tmp]# vim two.file  //可以使用vim打开文件,但是不能删除、修改、增加文件内容
     6 [root@LAMP tmp]# mkdir twotest
     7 [root@LAMP tmp]# touch twotest/one.file   
     8 [root@LAMP tmp]# chattr +a twotest
     9 [root@LAMP tmp]# echo 'hello world' >> twotest/one.file   //可以修改文件内容
    10 [root@LAMP tmp]# touch twotest/two.file    //可以创建文件
    11 [root@LAMP tmp]# rm -rf twotest/one.file   //不能删除文件
    12 rm: cannot remove `twotest/one.file': Operation not permitted
    13 [root@LAMP tmp]# mv twotest/one.file  twotest/one.file.file  //不能重命名文件
    14 mv: cannot move `twotest/one.file' to `twotest/one.file.file': Operation not permitted

    查看文件属性
    lsattr -a 显示文件或目录属性
    [root@LAMP tmp]# lsattr -a one.file
    ----i--------e- one.file //one.file文件具有i属性,文件系统是ext(e)

    lsattr -d 显示目录属性
    [root@LAMP tmp]# lsattr -d twotest
    -----a-------e- twotest //目录twotest具有a属性,文件系统是ext(e)

  • 相关阅读:
    16. 3Sum Closest
    17. Letter Combinations of a Phone Number
    20. Valid Parentheses
    77. Combinations
    80. Remove Duplicates from Sorted Array II
    82. Remove Duplicates from Sorted List II
    88. Merge Sorted Array
    257. Binary Tree Paths
    225. Implement Stack using Queues
    113. Path Sum II
  • 原文地址:https://www.cnblogs.com/iaknehc/p/6891867.html
Copyright © 2011-2022 走看看