linux chattr和lsattr命令使用详解
2019-04-30 12:41:10 阿炯

文件属性决定了文件的可见性、可读性、可写性等特性。chattr和lsattr是两个用于管理文件系统属性的重要工具,它们可以帮助用户保护重要的文件和目录,防止未授权的修改或删除;chattr命令用于改变文件属性,lsattr用于展示前者设定过后的文件属性。

语法格式如下:
lsattr [选项]... [文件]...

lsattr命令的常用选项或参数说明:
选项:说明
-a:列出所有文件和目录,包括以.开头的隐藏文件
-d:只列出目录的属性,而不是其下的文件
-R:递归处理,将指定目录下的所有文件及子目录一并处理
-v:显示文件的版本/生成号


chattr [ -RVf ] [ -v version ] [ mode ] files…
最关键的是在[mode]部分,[mode]部分是由+-=和[ASacDdIijsTtu]这些字符组合的,这部分是用来控制文件的属性。

这项指令可改变存放在ext2文件系统上的文件或目录属性,这些属性共有以下8种模式:
+-= :分别为 [+ 增加] [- 减少] [= 设定并清除其他所有] 属性
a:让文件或目录仅供附加用途。只能追加内容,不能删除或修改。
b:不更新文件或目录的最后存取时间。不更新文件或目录的最后存取时间。
c:将文件或目录压缩后存放。自动压缩文件,读取时解压缩,写入时压缩。
e:文件会被完全从磁盘上删除。
d:将文件或目录排除在倾倒操作之外。文件或目录被删除时,不会放入回收站,而是直接删除。
i:不得任意更动文件或目录。文件不能被删除、重命名、修改或链接。
s:保密性删除文件或目录。文件被删除时,其内容会被完全覆盖,以提高安全性。
S:即时更新文件或目录。文件写入时,数据会同步写入磁盘,以确保数据完整性。
u:预防意外删除。删除文件时,文件内容会被保存,便于恢复。

全部选项

A:即Atime,告诉系统不要修改对这个文件的最后访问时间。
S:即Sync,一旦应用程序对这个文件执行了写操作,使系统立刻把修改的结果写到磁盘。
a:即Append Only,系统只允许在这个文件之后追加数据,不允许任何进程覆盖或截断这个文件。如果目录具有这个属性,系统将只允许在这个目录下建立和修改文件,而不允许删除任何文件,只有 root 才能设定这个属性。
b:不更新文件或目录的最后存取时间。
c:将文件或目录压缩后存放。在读取的时候将会自动解压缩出来,但是在储存的时候,将会先进行压缩之后再储存。
d:当dump程序执行时,该文件或目录不会被dump备份。
D:检查压缩文件中的错误。
i:即Immutable,系统不允许对这个文件进行任何的修改。如果目录具有这个属性,那么任何的进程只能修改目录之下的文件,不允许建立和删除文件。
s:彻底删除文件,不可恢复,因为是从磁盘上删除,然后用0填充文件所在区域。
j:当使用 ext3 这个档案系统格式时,设定 j 属性将会使档案在写入时先记录在 journal 中,但是当 filesystem 设定参数为 data=journalled 时,由于已经设定了日志了,所以这个属性无效。
u:当一个应用程序请求删除这个文件,系统会保留其数据块以便以后能够恢复删除这个文件,可以使用来 undeletion,用来防止意外删除文件或目录。与 s 功能相反的。
t:文件系统支持尾部合并(tail-merging)。
X:可以直接访问压缩文件的内容。

语法
chattr [-RV][-v<版本编号>][+/-/=<属性>][文件或目录...]

参数
-R 递归处理,将指定目录下的所有文件及子目录一并处理。
-v<版本编号> 设置文件或目录版本。
-V 显示指令执行过程。
+<属性> 开启文件或目录的该项属性。
-<属性> 关闭文件或目录的该项属性。
=<属性> 指定文件或目录的该项属性。

原文参考

Below are the list of the operators which can be used with the selected attributes.

+ : The operator '+' causes the selected attributes to be added to the existing attributes of the files.
- : The operator '-' causes the selected attributes to be removed from the file.
= : The operator '=' makes the selected attributes to be the only attributes that the files have.

Below are the list of attributes that can be set/unset on a file using chattr command.

a: to append only
A: no atime updates
c: compressed
C: no copy on write
d: no dump
D: synchronous directory updates
e: extent format
F: case insensitive directory lookups
i: immutable
j: Data journalling
P: Project Hierarchy
s: Secure Deletion
S: Synchronous updates
t: No tail-merging
T: Top of Directory hierarchy
u: undeletable


CharacterAttributeDescription
aappend onlyThe file may only be opened for writing in append mode: its existing data may not be overwritten. It cannot be deleted or renamed; hard links cannot be made to this file; most of its metadata cannot be changed. Modifying this attribute requires root privileges.
Ano atime updatesWhen the file is accessed, its atime record is not modified, which in some situations can reduce disk I/O.
ccompressedFiles with this attribute are automatically compressed by the kernel when written to disk. Its contents are uncompressed when read. Note: This attribute has no effect in the ext2, ext3, and ext4 filesystems.
Cno copy on writeFiles with this attribute are not subject to copy-on-write updates. If this attribute is set on a directory, new files created in that directory get this attribute set. Note: This attribute is only effective on filesystems which perform copy-on-write. On btrfs, this attribute should be set on new or empty files. If this attribute is set after a btrfs file already contains data, the time when its data will be stable is undefined.
dno dumpFiles with this attribute are bypassed in any backup initiated by dump, a legacy tool for ext2 filesystems.
Dsynchronous directory updatesChanges to a directory with this attribute are written synchronously to disk. That is, the system waits for write completion before doing something else. Equivalent to the dirsync option to the mount command, applied to a subset of files on a filesystem.
eblock extentsIndicates that a file should be stored using block extents. Data is stored contiguously between two blocks, and only those two blocks must be known to find the file’s data. Block extent mapping may potentially save disk space, because it reduces the number of blocks which must be listed in the file’s inode.
iimmutableFiles with this attribute cannot be deleted or renamed; hard links cannot be made to this file; most of its metadata cannot be changed; data cannot be written to the file. Modifying this attribute requires root, or a process with the CAP_LINUX_IMMUTABLE capability, as set with setcap.
jdata journallingA file with this attribute has all its data written to its journal before being written to the file itself. Only effective on ext3 and ext4 filesystems which have journalling enabled and the “data=ordered” or “data=writeback” options set. If journaling is enabled in those systems, but the “data=journal” option is set, this attribute has no effect. Only root or a process with CAP_SYS_RESOURCE capability as set with setcap can change this attribute.
Pproject hierarchyA directory with this attribute will enforce a hierarchical structure for project IDs. Files and directories created in the directory will inherit the project ID of the directory. Rename operations are constrained so when those files or directories are moved to another directory, the project IDs will match. Hard links to these files may only be created if the project ID of the target and destination match.
ssecure deletionIf a file with this attribute is deleted, its data is overwritten with zeroes, similar to a simple shred. This attribute is ignored by ext2, ext3, and ext4 filesystems.
Ssynchronous updatesWhen files with this attribute are modified, the changes are written synchronously to disk. Equivalent to the sync option of the mount command, for individual files.
tno tail mergingA file with this attribute will not have any partial block fragment at the end of the file shared with another file’s data. This attribute is necessary for software such as LILO, which reads the filesystem directly and is not aware of tail merging. Some filesystems do not support tail merging, in which case this attribute has no effect.
Ttop of directory hierarchyA directory with this attribute is deemed to be the top of directory hierarchies by the Orlov block allocator, used by ext2 and ext3. The attribute gives a hint to the allocator that the subdirectories are not related in how they are used, and their data should be separate when blocks are allocated. For example, the /home directory may have this attribute, indicating that /home/mary and /home/john should be placed in separate block groups.
uundeletableWhen a file with this attribute is deleted, its contents are saved, enabling their later undeletion. Undelete tools that can take advantage of this attribute include extundelete.


Different Options that can be used in chattr Command :
-R change attributes of directory and its sub-directories recursively
-V Verbose output of chattr command along with version.
-f Suppress most error messages.

Operators that are used in chattr command to set and unset attributes
The ‘+’ sign is used to set attribute on the files and directories,
The ‘-‘ sign is used to remove or unset the attribute
The ‘=’ sign causes them to be the only attributes that the files have.

Basic Syntax of chattr and lsattr command :
# chattr <options> <attributes> <file or Directory >

# lsattr <File or Directory>


示例
用chattr命令防止系统中某个关键文件被修改:
chattr +i /etc/resolv.conf
lsattr /etc/resolv.conf

会显示如下属性
----i-------- /etc/resolv.conf

让某个文件只能往里面追加数据,但不能删除,适用于各种日志文件:
chattr +a /var/log/messages

锁定文件,不能删除,不能更改
+a:只能给文件添加内容,但是删除不了。
-d:不可删除。
加锁:chattr +i  /etc/passwd 文件不能删除,不能更改,不能移动
查看加锁: lsattr /etc/passwd 文件加了一个参数 i 表示锁定
解锁:chattr -i /home/omd/h.txt  - 表示解除

lsattr -a #将当前目录的文件或目录下的文件所有属性(包括隐藏属性)列出

对整个freeoa目录下的文件递归加锁:chattr -R +i freeoa

在使用chattr命令时需要注意以下几点:
需要root权限或者CAP_LINUX_IMMUTABLE能力,普通用户不能使用它;
只能改变文件或目录的属性,不能改变它们的权限,如果想要改变权限,需要使用chmod命令;
只能在支持的文件系统上使用,如果文件系统不支持某些属性,chattr命令会报错或者无效,可以使用mount命令来查看当前文件系统类型;
属性有优先级,比如i属性会覆盖a属性,如果想要同时使用两个属性,需要先取消i属性,再添加a属性,比如:
chattr -i +a /var/log/messages
如果在使用chattr命令时,遇到了bash: chattr: command not found的错误,说明当前系统没有安装chattr命令;如果在某些Linux发行版中无法使用,通常是因为没有安装e2fsprogs包。