In the Linux hierarchy, the root user is a god. If you have root access (via sudo), you can delete any file, uninstall any software, and completely destroy the operating system with a single command. Standard file permissions (read, write, execute) mean absolutely nothing to the root user; they bypass them entirely.
However, there are certain critical files—like core system configurations or highly sensitive server logs—that you want to protect from accidental deletion or modification, even if the person logged in has full root privileges. To achieve this, you must step outside the standard permission system and alter the file’s extended filesystem attributes.
You can do this using the incredibly powerful chattr (Change Attribute) command to make a file completely “immutable.”
What is an Immutable File?
When a file is marked as immutable on an ext3/ext4 filesystem, it becomes perfectly locked in time. It cannot be deleted. It cannot be renamed. It cannot be moved. No data can be appended to it, and no data can be removed from it. Even a direct command from the root user (like sudo rm -rf) will fail with an “Operation not permitted” error.
Step 1: Make a File Immutable
Let’s assume you have a critical configuration file located at /etc/my_secure_config.conf.
- Open your terminal.
- Run the
chattrcommand with the+i(add immutable) flag. You must usesudoto apply this attribute:sudo chattr +i /etc/my_secure_config.conf - Press Enter. The command will execute silently.
Step 2: Test the Protection
Attempt to destroy the file using root privileges:
sudo rm /etc/my_secure_config.conf
The terminal will immediately reject the command, outputting:
rm: cannot remove '/etc/my_secure_config.conf': Operation not permitted
You can also try to append text to it using sudo echo "hacked" >> /etc/my_secure_config.conf. It will also fail with a permission denied error.
Step 3: Verify the Immutable Status
If you run a standard ls -l command, it will not show the immutable flag. The standard permissions might still say -rw-r--r--, which makes it look perfectly normal. To see extended attributes, you must use the lsattr (List Attributes) command.
lsattr /etc/my_secure_config.conf
The output will look something like this:
----i---------e---- /etc/my_secure_config.conf
The prominent i in that string confirms the file is actively locked.
How to Remove the Immutable Flag
The protection is designed against accidents, not determined intent. Since the root user applied the flag, the root user can simply take it away when it is time to legitimately edit or delete the file.
To unlock the file, run the exact same command, but swap the plus sign for a minus sign (-i):
sudo chattr -i /etc/my_secure_config.conf
The file instantly returns to its standard state, and standard root deletion commands will work once again.