Lock Up Files And Prevent Changes With This Simple Command

Spread the love

[Linux] There are times when you want to lock up files and prevent other people, or the system from making changes to them. Here is one simple command that you can use to lock up either a single file or folder:

sudo chattr +i /path/to/file

To unlock:

sudo chattr -i /path/to/file

Explanation: chattr is a command that allows a user to set certain attributes on a file residing on a Linux filesystem. A “+i” flag adds an immutable attribute to the file. When this is enabled, even a root user cannot change the file. Similarly, a “-i” flag subtracts the specific attribute from the file.

If you have a folder of files that you want to lock up, you can also add the “+R” flag. For example:

sudo chattr +R +i /path/to/folder

Note: Only the superuser or a process pessessing the CAP_LINUX_IMMUTABLE capability can set or clear this attribute.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time. Subscribe


Damien Oh

Damien Oh started writing tech articles since 2007 and has over 10 years of experience in the tech industry. He is proficient in Windows, Linux, Mac, Android and iOS, and worked as a part time WordPress Developer. He is currently the owner and Editor-in-Chief of Make Tech Easier.

Comments are closed