Change file attributes
You can use the chattr command to change file attributes in Linux. You can secure your files by modifying their attributes. For example, you can protect important system files by making them undeletable.
Here is a list of some of the attributes you can modify:
- immutable – the “i” attribute makes a file immutable, which means that the file can’t be modifed, renamed, or deleted and no link to it can be created.
- append only – the “a” attribute sets append mode, which means that data can only be appended to the file.
- compressed – a file with the “c” attribute is automatically compressed by the kernel. The file is uncompressed when read.
- no dump – a file with the “d” attribute will not be backed up by the dump utility.
- secure deletition – when the file with the “s” attribute set is deleted, the kernel zeros its data blocks. This means that the file will be securely wiped when someone deletes it. This makes it much harder for unauthorized persons to recover and view the file, which can be useful with files that contain sensitive data, such as passwords.
- no atime updates – Linux won’t update the access time stamp when you access a file with the “A” attribute. This reduces a certain amount of disk I/O, which is helpful for saving battery life on laptops.
The attributes are set using the + operator, which causes the attributes to be added to the file, the – operator, which causes the attributes to be removed, and the = operator, which causes the attributes to be the only attributes of the file.
Here is an example. We can add the immutable attribute to a file by using the “i” attribute:
We can then remove the “i” attribute: