The file or folder on the server is the most important asset of any website or application; even a minute unintended change in a file or folder can result in making the website or application down. Every project contains some files or folder that needs even more attention, whether it is a configuration file in your home directory or a potentially vulnerable file or folder that needs additional care. The best way to prevent such files or folders is to make them immutable so that even the ‘root’ will have to make extra effort to edit/modify/delete them.
How to Make File or Folder Immutable or Uneditable on Ubuntu Server:
By changing the attributes you can easily make any file or folder immutable or uneditable, all you need is server SSH access. If you have SSH access then it can be done by running just a couple of commands. However, some files need to be updated and edited by the application or server tools to continue running the website or applications, even when you think that the specific file or folder is extremely critical you should not make them immutable unless you are sure about that it won’t affect the project. So make sure you test the website or application after adding or changing the attributes.
Follow these quick steps to make a file or folder immutable or uneditable on the Ubuntu server:
- Login to your server using valid SSL credentials.
- Navigate to the /directory where you have the file or folder you want to restrict.
- Run the following command to make the file/folder immutable. Change the filename or folder name or their path with your target file or folder.
$ chattr +i filename or foldername
- You can also use the direct path of the file and folder with the command as follows;
$ chattr +i /var/www/FolderName/MakeImmutable $ chattr +i /var/www/FolderName/MakeImmutable.php
- To check the attribute status, run the following command on your terminal;
$ lsattr filename or foldername $ lsattr /var/www/FolderName/MakeImmutable.php
- You should expect the output as follows, where the i tag represents it as immutable.
Once you run the command go back to the file using terminal or sFTP and try editing, renaming or deleting (be careful) the file or folder for which you just changed the attributes. It should show the permission denied error.
How to Remove the i Tag or Make File or Folder Editable Again?
Use the following command in the same way as the above process to remove the immutable attributes or i tag for a file or folder.
$ chattr -i filename or foldername
$ chattr -i /var/www/FolderName/MakeImmutable $ chattr -i /var/www/FolderName/MakeImmutable.php
Additionally, you should have the proper file permission and ownership on all files and folders for better security and accessibility or performance of the website and application. We offer different digital solutions including services for server management and maintenance, you can reach out to us for professional support.