Prevent Files And Folders From Accidental Deletion Or Modification In Linux
Data protection is one of the main job of a System administrator. There are numerous free and commercial data protection softwares are available on the market. But, as you might know, there is a simple, yet useful commandline utility called “chattr” (abbreviation of Change Attribute) which can be used to prevent files and folders from accidental deletion in Unix-like distributions. It applies certain attributes to a file or folder in your Linux system. So the users can’t delete or modify the files and folders either accidentally or intentionally, even as root user. Sounds useful, isn’t it?
By default, Chattr is available in most modern Linux operating systems. Let us see some examples.
The default syntax of chattr command is:
chattr has the following operators.
- The operator ‘+’ causes the selected attributes to be added to the
existing attributes of the files; - The operator ‘-‘ causes them to be removed;
- The operator ‘=’ causes them to be the only attributes that the files have.
Chattr has different attributes namely – aAcCdDeijsStTu. Each letter applies a particular attributes to a file.
- a – append only,
- A – no atime updates,
- c – compressed,
- C – no copy on write,
- d – no dump,
- D – synchronous directory updates,
- e – extent format,
- i – immutable,
- j – data journalling,
- P – project hierarchy,
- s – secure deletion,
- S – synchronous updates,
- t – no tail-merging,
- T – top of directory hierarchy,
- u – undeletable.
In this tutorial, we are going to discuss the usage of two attributes, namely a, i which are used to prevent the deletion of files and folders.
Prevent files from accidental deletion
I am going to create a file called file.txt in my current directory.
Now, I am going to apply “i” attribute which makes the file immutable. It means you can’t delete, modify the file, even if you’re the file owner and the root user.
You can check the file attributes using command:
Sample output:
Now, try to remove the file either as a normal user or with sudo privileges.
Sample output:
Let me try with sudo command:
Sample output:
Let us try to append some contents in the text file.
Sample output:
Try with sudo privilege:
Sample output:
As you noticed in the above outputs, We can’t delete or modify the file even as root user.
To revoke attributes, just use “-i” switch as shown below.
Now, the immutable attribute has been removed. You can now delete or modify the file.
Similarly, you can restrict the directories from accidental deletion or modification as described in the next section.
Prevent folders from accidental deletion and modification
Create a directory called dir1 and a file called file.txt inside this directory.
mkdir dir1 && touch dir1/file.txt
Now, make this directory and its contents (file.txt) immutable using command:
Where,
- -R – will make the dir1 and its contents immutable recursively.
- +i – makes the directory immutable.
Now, try to delete the directory either as normal user or using sudo user.
You will get the following output:
Try to append some contents in the file using “echo” command:
See? You can’t modify it either.
To revoke the attributes back, run:
Now, you can delete or modify the contents of this directory as usual.
Prevent files and folders from accidental deletion, but allow modification
We know now how to prevent files and folders from accidental deletion and modification. Next, we are going to prevent files and folders from deletion, but allow modification.
To do so, run the following command:
For files:
For directories:
The “+a” option will allow you to modify the file or folder, but you can’t delete it.
Add some contents to the file(s) to check whether it works or not.
Check the file contents using cat command:
Sample output:
You will see that you can now be able to append the contents. It means we can modify the files and folders.
Let us try to delete the file or folder now.
Output:
Let us try to delete the folder:
Sample output:
To remove the attributes, run the following commands:
For files:
For directories:
Now, you can delete or modify the files and folders as usual.
For more details, refer the man pages.
That’s all for now. You know now how to apply file attributes to files and folders using chattr command, and how to prevent them from accidental deletion or modification. This can be useful to protect the important system files and data in your Linux system.
Cheers!
No comments:
Post a Comment