Linux chattr command

Linux chattr command

Linux chattr Command – Change File Attributes

The chattr (change attribute) command in Linux is used to modify file attributes, which control the behavior of files in the filesystem. It is primarily used to protect files from being accidentally deleted or altered and can be particularly useful for system administrators and security-conscious users.

Syntax of chattr

chattr [OPTIONS] FILE
  • OPTIONS → Flags that define the attributes to be set or removed.
  • FILE → The file or directory whose attributes you want to modify.

Common File Attributes in Linux

Here are some of the most common attributes that can be set using chattr:

AttributeDescription
+iMakes the file immutable, preventing it from being modified or deleted.
-iRemoves the immutable attribute, allowing the file to be modified.
+aMakes the file append-only, meaning only append operations are allowed.
-aRemoves the append-only attribute, allowing regular file modifications.
+eMakes the file exclusive-use, preventing other processes from using it while open.
-eRemoves the exclusive-use attribute.
+sMakes the file secure-delete, so it is deleted securely and cannot be recovered.
-sRemoves the secure-delete attribute.
+uMakes the file undeletable by ordinary users, even with root privileges.
-uRemoves the undeletable attribute.
+jEnables journaled file updates, so the file is updated in the journal.
-jRemoves the journaled file updates attribute.

Basic Usage of chattr

1. Make a File Immutable (+i)

To prevent a file from being modified or deleted, you can set the immutable attribute:

sudo chattr +i filename

Example:

sudo chattr +i important_file.txt

After this, even the root cannot delete or modify important_file.txt unless the immutable attribute is removed.

2. Remove the Immutable Attribute (-i)

To allow modifications again, remove the immutable attribute:

sudo chattr -i filename

Example:

sudo chattr -i important_file.txt

3. Make a File Append-Only (+a)

To allow only appending data to a file, use the append-only attribute:

sudo chattr +a log.txt

This ensures that the file can only be modified by appending new data, and existing data cannot be altered or deleted.

4. Make a File Securely Deleted (+s)

To ensure a file is securely deleted and cannot be recovered, use the secure-delete attribute:

sudo chattr +s sensitive_file.txt

When deleted, the file will be overwritten before removal to prevent recovery.

5. Check the Attributes of a File (lsattr)

To check the current attributes of a file, use the lsattr command:

lsattr filename

Example:

lsattr important_file.txt

Output might look like:

----i--------e---- important_file.txt

This shows that the file is immutable and exclusive-use.

Examples

1. Set Multiple Attributes

To make a file immutable and append-only at the same time:

sudo chattr +i +a file.txt

2. Recursively Set Attributes for a Directory

To apply an attribute to all files in a directory:

sudo chattr -R +i /path/to/directory

This will make every file inside the directory immutable.

Important Notes on chattr

  1. Root Access: Most chattr commands require root (superuser) privileges to modify file attributes, especially for system files.
  2. Filesystem Support: The chattr command works only on filesystems that support extended attributes (such as ext2, ext3, and ext4). It will not work on other filesystems like NTFS or FAT32.
  3. Immutable Files: Files with the +i attribute cannot be moved, renamed, or deleted, even by root. This is a useful protection against accidental or malicious deletion.
  4. Append-Only Files: Files with the +a attribute can only be appended to, not modified or truncated, making it useful for logging purposes.

Conclusion

The chattr command is a powerful tool for setting file attributes that control access and modification of files, especially for system administrators who need to protect sensitive files or logs. By using attributes like +i (immutable) or +a (append-only), you can prevent unauthorized changes or accidental deletions.

Would you like additional details or SEO optimization? 🚀

Souy Soeng

Souy Soeng

Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

Github

Post a Comment

CAN FEEDBACK
close