← 返回命令列表

Linux command

chattr 命令

安全

权限或系统影响较大,执行前请核对目标。

常用示例

Example

sudo chattr +i path/to/file

Example

sudo chattr -i path/to/file

Recursive

sudo chattr -R +i path/to/dir

Case-folding

chattr +F path/to/dir

Append-only

sudo chattr +a path/to/file

说明

chattr changes file attributes on Linux filesystems. These extended attributes provide security and administrative controls beyond standard permissions, such as making files immutable or append-only. The immutable attribute (+i) is particularly useful for protecting critical system files, as even root cannot modify, delete, or rename an immutable file until the attribute is removed. The append-only attribute (+a) is commonly used for log files to prevent tampering while still allowing new entries to be written.

参数

+attribute
Add attribute to file
-attribute
Remove attribute from file
=attribute
Set only the specified attributes
-R
Change attributes recursively
i
Immutable - cannot be modified, deleted, or renamed
a
Append-only - can only be appended to
s
Secure deletion - blocks overwritten with zeros
S
Synchronous updates
A
No atime updates
c
Compressed
e
Extent format (default on ext4)
F
Case-fold directory (case-insensitive)

FAQ

What is the chattr command used for?

chattr changes file attributes on Linux filesystems. These extended attributes provide security and administrative controls beyond standard permissions, such as making files immutable or append-only. The immutable attribute (+i) is particularly useful for protecting critical system files, as even root cannot modify, delete, or rename an immutable file until the attribute is removed. The append-only attribute (+a) is commonly used for log files to prevent tampering while still allowing new entries to be written.

How do I run a basic chattr example?

Run `sudo chattr +i path/to/file` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does +attribute do in chattr?

Add attribute to file