chattr 与 lsattr 命令
2014-03-25 by dongnan
chattr
拒绝删除文件
chattr +a test/
# 创建一个文件
touch test/1
# 删除文件
rm -rf test/1
rm: 无法删除"test/1": 不允许的操作
用户权限
# 拥有管理员权限
uid=0(root) gid=0(root) 组=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel)
命令帮助
NAME
   chattr - change file attributes on a Linux file system
    Usage: chattr [-RVf] [-+=AacDdeijsSu] [-v version] files...
参数:
- -R递归处理,将指定目录下的所有文件及子目录一并处理。
- -v<版本编号> 设置文件或目录版本。
- -V显示指令执行过程。
- +<属性>开启文件或目录的该项属性。
- -<属性>关闭文件或目录的该项属性。
- =<属性>指定文件或目录的该项属性。
属性:
- A:即- Atime,告诉系统不要修改对这个文件的最后访问时间。
- S:即- Sync,一旦应用程序对这个文件执行了写操作,使系统立刻把修改的结果写到磁盘。
- a:即- Append Only,系统只允许在这个文件之后追加数据,不允许任何进程覆盖或截断这个文件。如果目录具有这个属性,系统将只允许在这个目录下建立和修改文件,而不允许删除任何文件。
- b:不更新文件或目录的最后存取时间。
- c:将文件或目录压缩后存放。
- d:当- dump程序执行时,该文件或目录不会被- dump备份。
- D:检查压缩文件中的错误。
- i:即- Immutable,系统不允许对这个文件进行任何的修改。如果目录具有这个属性,那么任何的进程只能修改目录之下的文件,不允许建立和删除文件。
- s:彻底删除文件,不可恢复,因为是从磁盘上删除,然后用- 0填充文件所在区域。
- u:当一个应用程序请求删除这个文件,系统会保留其数据块以便以后能够恢复删除这个文件,用来防止意外删除文件或目录。
- t:文件系统支持尾部合并(- tail-merging)。
- X:可以直接访问压缩文件的内容。
lsattr
查看文件额外属性
# 默认情况 
lsattr cron.log
-------------e- cron.log
# 设置额外属性
chattr +a cron.log
lsattr cron.log
-----a-------e- cron.log
注意: 多了一个a。
命令帮助
NAME
       lsattr - list file attributes on a Linux second extended file system
SYNOPSIS
       lsattr [ -RVadv ] [ files...  ]
DESCRIPTION
       lsattr  lists the file attributes on a second extended file system.  
        See chattr(1) for a description of the attributes and what they mean.
OPTIONS
       -R     Recursively list attributes of directories and their contents.
       -V     Display the program version.
       -a     List all files in directories, including files that start with `.'.
       -d     List directories like other files, rather than listing their contents.
       -v     List the file's version/generation number.