- Nikhil Bhaskar
- May 11, 2021
Most of the time Linux based Operating system machine is used by different users. So there are high chances that the users will access a common set of files. This practice will cause of problems like accidental deletion or modification of important files.
There is an existing command – chattr – that’s developed to help you in specifically these kinds of scenarios.
In this blog, we will discuss this utility using some easy examples.
Lets understand the flag of the attributes.
The value of the
[OPERATOR] part can be one of the following symbols:
+– The plus operator tells the
chattrto add specified attributes to the existing ones.
-– The minus operator tells the
chattrto remove specified attributes from the existing ones.
=– The equal operator tells the
chattrto set specified attributes as the only attributes.
The operator is followed by one or more
[ATTRIBUTES] flags that you want to add or remove from the file attributes. Below is a list of a few common attributes and associated flags:
a– When this attribute is set, the file can only be opened in append mode for writing.
A– When a file with this attribute set is open, its atime record is not changed. atime (access time) is the last time the file was accessed/opened by some command or application.
e– This attribute denotes that the file is using extents for mapping the blocks on disk. The
eattribute cannot be modified with
i– This attribute indicates that the file is immutable, which means that the file cannot be deleted or renamed.
For a complete list of all file attributes and flags, type
man chattr in your terminal.
1- Make a file read only
Lets start if you want to make a file read-only. Then you have to do is to run the chattr command with +i option and the name of the file as input.
Use the following to make the files immutable.
Now see that or test file is only read only is working or not.
2- Remove the file read only permission
Run command to remove the permission of the text.txt file.
3- Make a file append-able
If you want to provide users append-only access to a file so that new info can be added without modifying the existing content. This is possible with chattr command with +a parameter.
Now we could append the file but it would not edit existing content in the file as well as delete the file. To reverse this behaviour just use the -a option.
4- Make a file un-append
lsattr command is used to see the attributes of files in a directory. Here it should be noted that the e flag in the file is previously set and it means that the file is using extents for mapping blocks on the disk. The extents are file-system dependant. They are seldom removable.
5- how to secure directories
To secure entire directory and its files, we use ‘-R‘ (recursively) switch with ‘+i‘ flag along with full path of the directories.
To remove these securities of the directories and files the use command.
In this blog we tried to elaborate how the files can immutable and secure the directories and also make the files appendable and vice-versa.