Make File Directory Undeletable Immutable in Linux
In this article
To make a file or directory undeletable and immutable in Linux, use the chattr +i command, which sets the immutable attribute. Once set, even the root user cannot modify, delete, or rename the file until the attribute is removed with chattr -i.
What Does the Immutable Attribute Do?
The immutable attribute (i) is a special Linux file flag that makes a file or directory completely read-only at the filesystem level. When set, the kernel refuses any operation that would change the file's contents, metadata, or link count. This includes:
- Deleting or renaming the file
- Creating, deleting, or renaming files inside an immutable directory
- Writing to or truncating the file
- Changing permissions or ownership
- Creating hard links to the file
Unlike standard Unix permissions (which can be bypassed by the superuser), the immutable attribute is enforced by the kernel even for root. This makes it a powerful tool for protecting critical system files, logs, or sensitive data from accidental or malicious modification.
How to Set the Immutable Attribute
The chattr command is part of the e2fsprogs package and is available on most Linux distributions. The basic syntax is:
sudo chattr +i /path/to/file_or_directoryTo verify the attribute is set, use lsattr:
lsattr /path/to/file_or_directoryYou will see an i in the output, for example: ----i---------e--. To remove the immutable attribute, replace the plus sign with a minus:
sudo chattr -i /path/to/file_or_directoryNote that only the root user (or a user with CAP_LINUX_IMMUTABLE capability) can set or remove this attribute. On some systems, you may need to use sudo even if you own the file.
Immutability vs. Other Protection Methods
Linux offers several ways to protect files from deletion or modification, and each has different strengths and weaknesses:
- Standard permissions (chmod): The owner can change permissions, and root can override them. This does not protect against root or accidental
rm -rf. - Immutable attribute (chattr +i): Protects against all users, including root, until explicitly removed. This is the strongest protection at the filesystem level.
- Append-only attribute (chattr +a): Allows adding data to a file but not deleting or modifying existing content. Useful for logs.
- Read-only mount: Mounting a filesystem with
romakes everything read-only, but the mount can be remounted by root. - SELinux or AppArmor: These provide mandatory access control but are more complex to configure and can be bypassed by root with proper policy.
For most users, chattr +i is the simplest and most effective way to make a file or directory truly undeletable.
Common Use Cases
The immutable attribute is used in various scenarios where file integrity is critical:
- Protecting system configuration files from accidental modification by administrators.
- Securing logs against tampering by an attacker who gains root access.
- Preventing accidental deletion of important data in shared directories.
- Creating a honeypot or decoy files that cannot be altered.
- Ensuring the integrity of files in a chroot jail or container.
However, be cautious: setting the immutable attribute on a directory does not make it impossible to delete the directory itself, but it prevents any changes to the directory's contents. To make the directory itself undeletable, you must set the attribute on the directory inode, which is what chattr +i /path/to/dir does.
Limitations and Caveats
While the immutable attribute is powerful, it has some limitations you should know:
- Filesystem support: Not all filesystems support
chattr. It works on ext2/3/4, XFS, Btrfs, and some others, but not on FAT, NTFS, or ZFS (without special configuration). - Root can still bypass it: Although the kernel enforces the attribute, a determined attacker with root access can remove the attribute (if they have the capability) or use raw disk access. It is not a security boundary against a fully compromised system.
- Backup and restore: Some backup tools may fail to read or restore immutable files. You may need to temporarily remove the attribute before backing up.
- System updates: If you make system files immutable, package managers (like
aptoryum) may fail to update them. You must remove the attribute temporarily during updates. - Immutable directories and the sticky bit: The sticky bit (
chmod +t) on a directory (like/tmp) prevents users from deleting files they do not own, but it does not protect against root. Immutable is stronger.
How to Make a Directory Truly Undeletable (Even Against Root)
If you need to protect a directory from deletion even by root, the immutable attribute is the standard method. However, if you want to prevent the attribute from being removed, you need to consider additional layers:
- Use a dedicated filesystem: Mount a separate partition for the directory and mount it read-only. Root can remount it, but it adds friction.
- Use a different user with no privileges: If the directory is owned by an unprivileged user and the filesystem is mounted with
nosuidandnoexec, root can still delete it, but it reduces accidental damage. - Use a kernel module or LSM: Advanced users can use a Linux Security Module (like SELinux) to enforce policies that even root cannot easily bypass.
For most practical purposes, chattr +i is sufficient to prevent accidental deletion and even malicious deletion by a non-root attacker.
Frequently Asked Questions
Can I make a file immutable on any filesystem?
No. The chattr command relies on filesystem support for extended attributes. It works on ext2/3/4, XFS, Btrfs, and ReiserFS, but not on FAT, NTFS, or exFAT. On those filesystems, you can only use standard permissions, which are less restrictive.
How do I remove the immutable attribute if I forgot the password?
You need root access to remove the attribute. If you have physical access to the machine, you can boot into a live CD, mount the filesystem, and run chattr -i from there. If you do not have root, you cannot remove it.
Does the immutable attribute protect against formatting?
No. Formatting the partition or deleting the filesystem will destroy the file regardless of attributes. The immutable attribute only protects against file-level operations, not low-level disk writes.
Can I set the immutable attribute on a symbolic link?
Yes, you can set it on a symlink, but it will protect the symlink itself, not the target. The target file remains mutable unless you also set the attribute on it.
Is there a way to make a file immutable without root?
No. The CAP_LINUX_IMMUTABLE capability is required, which is only available to root by default. This is a security feature to prevent users from making files undeletable and then causing problems for the administrator.