Amblem
Furkan Baytekin

Linux File Attributes: Create an Undeletable File

Create undeleteable files and secure logs with Linux file attributes

Linux File Attributes: Create an Undeletable File
57
5 minutes

Linux is renowned for its robust permission and file management system. Beyond standard file permissions, Linux provides extended file attributes that allow users to fine-tune the behavior and security of files. These attributes can make files immutable (undeletable and unmodifiable), append-only, and more. In this blog post, we’ll dive into these attributes, their uses, and how to make a file undeletable and unmodifiable using the chattr command and verify it with lsattr.


What Are File Attributes?

File attributes in Linux are metadata settings that control how the kernel interacts with files. Unlike standard permissions (read, write, execute), these attributes apply special constraints or behaviors to files and directories.

Key Attributes

Here’s a breakdown of all available attributes, explained in detail:

Each attribute provides unique functionality to enhance security, performance, or usability. Understanding these attributes empowers users to optimize their system’s file management effectively.


Viewing File Attributes

To view a file’s attributes, use the lsattr command:

bash
lsattr filename

For example, if you have a file named example.txt, running lsattr example.txt might output:

-------------e---- example.txt

The e indicates the file is extent-based.


Modifying File Attributes

To modify file attributes, use the chattr command. This tool allows you to add (+), remove (-), or set (=) attributes.

Syntax

bash
chattr [options] [operator][attribute] filename

Example: Making a File Undeletable and Unmodifiable

  1. Create a test file:
bash
echo "This is a test file" > testfile.txt
  1. Add the immutable attribute:
bash
chattr +i testfile.txt
  1. Verify the attribute:
bash
lsattr testfile.txt

Output:

----i--------e---- testfile.txt
  1. Try to delete or modify the file:
bash
rm testfile.txt

You’ll see an error:

rm: cannot remove 'testfile.txt': Operation not permitted

Even if you run this command with root user, you will get the same error. This is because the file is immutable.

To make the file modifiable and deletable again, remove the immutable attribute:

bash
chattr -i testfile.txt

Now you can delete the file:

bash
rm testfile.txt

A Practical Example: Logging

You can use the append-only attribute to create a log file that is immutable and append-only. This is useful for creating a log file that is immutable and append-only.

bash
chattr +a logfile.txt

Now you can append to the file:

bash
echo "This is a test log" >> logfile.txt

If you try to modify the file, you will get an error:

bash
echo "This is a test log" > logfile.txt

You will get an error:

bash
echo: write error: Operation not permitted

In this way, you can safely create a log file that you don’t worry about being modified or overwritten.


Conclusion

Linux file attributes provide powerful controls over file behavior and security. Using chattr, you can make files immutable, append-only, or restrict their behavior in other ways. The lsattr command is your go-to tool for verifying these attributes. By understanding and leveraging these tools, you can better secure sensitive files and fine-tune your system.

Try experimenting with these attributes, but remember: with great power comes great responsibility! Misusing attributes can lock you out of your own files.

Suggested Blog Posts