Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Prevent File Deletion Linux: 6 Ways

· 6 min read · Home Tech

In this article
  1. Why Files Get Deleted on Linux
  2. Method 1: Using chattr to Set the Immutable Attribute
  3. Method 2: Adjusting Directory Permissions
  4. Method 3: Using ACLs for Fine-Grained Control
  5. Method 4: Making Files Read-Only (and Its Limits)
  6. Method 5: Using a Separate Partition or Mount Options
  7. Method 6: Using Backup and Versioning (The Safety Net)
  8. How to Choose the Right Method
  9. Common Problems and Misconceptions
  10. Safety and Recovery
  11. Frequently Asked Questions

To prevent file deletion on Linux, you must change file permissions, use immutable attributes, or adjust directory permissions—depending on whether you need to protect against accidental deletion, other users, or even root. The most reliable method is the chattr +i command, which makes a file immutable so that no user, including root, can delete or modify it until the attribute is removed.

Why Files Get Deleted on Linux

Linux file deletion is controlled by the permissions of the directory that contains the file, not the file itself. To delete a file, you need write and execute permissions on the parent directory. This is a common source of confusion: a file can be read-only, yet still be deleted if the directory allows it. Other causes include accidental rm commands, script errors, malicious users, and software bugs. Understanding this distinction is the first step to preventing unwanted deletions.

Method 1: Using chattr to Set the Immutable Attribute

The most powerful way to protect a file is to mark it as immutable with the chattr command. This works on most Linux filesystems (ext4, xfs, btrfs) and prevents even the root user from deleting or modifying the file until the attribute is removed.

  1. Open a terminal.
  2. Run sudo chattr +i /path/to/file to make the file immutable.
  3. To remove the protection, use sudo chattr -i /path/to/file.
  4. Verify with lsattr /path/to/file—you’ll see an i flag.

This method is ideal for critical system files or sensitive documents. However, it also prevents any modification, including appending data. For logs or files that need to grow, use chattr +a (append-only) instead, which allows adding data but not deleting or overwriting.

Method 2: Adjusting Directory Permissions

Since deletion depends on directory permissions, you can protect a file by removing write permission on its parent directory for the relevant users. For example, to prevent others from deleting a file in a shared directory:

  • chmod 755 /path/to/directory – owner can write, others can only read and execute.
  • Use chown to change the owner if needed.

This approach is simple but affects all files in the directory. For granular control, use Access Control Lists (ACLs) to deny delete permission to specific users without affecting others.

Method 3: Using ACLs for Fine-Grained Control

ACLs allow you to set permissions for individual users or groups on a file or directory. To deny delete permission to a specific user on a file, you can set a default ACL on the parent directory that removes write permission for that user.

  1. Install ACL support if not present (sudo apt install acl on Debian/Ubuntu).
  2. Set a default ACL: setfacl -m d:u:username:rx /path/to/directory – this gives the user read and execute but no write, preventing deletion of files they don’t own.
  3. For existing files, apply setfacl -m u:username:r /path/to/file.

ACLs are more flexible than standard permissions and are essential for multi-user systems where you need to protect specific files without locking down the whole directory.

Method 4: Making Files Read-Only (and Its Limits)

Setting a file to read-only (chmod 444) prevents modification of the file’s content, but it does not prevent deletion if the parent directory allows it. This is a common misconception. Read-only protection is only effective when combined with directory permission changes. For example, if you set the file to read-only and remove write permission on the directory for other users, they cannot delete it. However, the owner can still delete it because the owner has write permission on the directory. To fully protect against the owner, use chattr +i.

Method 5: Using a Separate Partition or Mount Options

For directories that should never be modified, you can mount them as read-only. This is often used for system directories in embedded systems or live CDs. To do this:

  1. Edit /etc/fstab and add the ro option to the mount entry.
  2. Alternatively, remount a filesystem as read-only on the fly: mount -o remount,ro /path/to/mountpoint.

This method is heavy-handed but effective for entire directories. It also protects against accidental deletions by any user, including root, because the filesystem itself rejects writes.

Method 6: Using Backup and Versioning (The Safety Net)

No single method is foolproof. A robust strategy combines prevention with recovery. Regularly back up critical files using tools like rsync, tar, or a version control system like Git. For directories that change often, consider a file system that supports snapshots (e.g., btrfs, ZFS) so you can restore previous versions. Automate backups with cron jobs or systemd timers to ensure they happen without manual intervention.

How to Choose the Right Method

Select the method based on your threat model and usability needs:

  • Accidental deletion by yourself: Use chattr +i for critical files, or directory permissions for folders.
  • Protection from other users: Use ACLs or directory permissions.
  • Protection from root (e.g., malware): Use chattr +i (though a determined root can still remove it, it stops many attacks).
  • Log files that need appending: Use chattr +a.
  • Whole directories: Mount read-only.

Combining methods (e.g., immutable attribute plus backups) gives the best defense.

Common Problems and Misconceptions

  • “I set the file to read-only, but it still got deleted.” As explained, deletion depends on directory permissions, not file permissions.
  • “chattr doesn’t work on my filesystem.” Some filesystems like tmpfs and certain network filesystems do not support the immutable attribute. Check with lsattr.
  • “I can’t remove the immutable attribute.” You need root privileges and the CAP_LINUX_IMMUTABLE capability. If you get “Operation not permitted,” ensure you have sudo and the filesystem supports it.
  • “ACLs don’t seem to work.” Ensure the filesystem is mounted with the acl option (default on most modern distros).

Safety and Recovery

Before applying any protection, test on a non-critical file. Document the commands you use, so you can reverse them later. If you accidentally delete a file despite protections, stop using the filesystem immediately and use extundelete (for ext3/ext4) or testdisk to attempt recovery. However, prevention is always better—implement backups before you need them.

Frequently Asked Questions

Can I prevent a file from being deleted by root?

Yes, using chattr +i will prevent even root from deleting the file, as long as the filesystem supports the immutable attribute. However, root can still remove the attribute with chattr -i, so it’s not absolute security against a malicious root, but it stops accidental deletion and many attacks.

What is the difference between chmod and chattr?

chmod changes standard Unix permissions (read, write, execute) that apply to the file’s content. chattr changes filesystem attributes that affect the file’s metadata and behavior, such as immutability. chattr is more powerful for deletion prevention because it overrides even root.

Does making a directory read-only prevent file deletion?

If you remove write permission on a directory, no one (except root) can create or delete files in it. This will prevent deletion of any file in that directory, but it also prevents creating new files. For selective protection, use ACLs.

How do I protect a file from being deleted but still allow it to be read?

Set the file permissions to read-only for all (chmod 444) and ensure the parent directory does not grant write permission to the users you want to restrict. For stronger protection, use chattr +i which allows reading but prevents any modification or deletion.

Is there a way to prevent deletion of files in a shared folder for specific users?

Yes, use ACLs. For example, setfacl -m u:username:r /path/to/file gives that user read-only access, and they cannot delete the file if they don’t have write permission on the directory. You can also set default ACLs on the directory to apply to new files.