Is there a way to make a file readonly permanently in linux. Or can I put a password to a file to make it readonly, then apply that password again to change its permission, or before they can alter it.

  • Like it's pointed out by Sami-Laine, you can't. However, if you're the admin & the person to know the root password you can. And not to mention recovering root password when you've access to hardware. – SparKot Mar 1 '14 at 7:34
  • Not really, you could use chmod 600 to make the file only read/writeable by you (and root) or do chown root:root so that only root password can change permissions. Either that or encrypt the file. – Graeme Mar 1 '14 at 10:18
  • Who are you protecting against, or in other words, who would still be allowed to make the file read-write? You can't do anything that the system administrator or someone with physical access can't undo. – Gilles Mar 1 '14 at 15:43

Only way to do that would be to write the file in question on a media which is writable only once, e.g. a CD-ROM.

Any other solution is possible to circumvent by root. If the file you'd like to make non-writable is one of system files, e.g. /etc/passwd, you'd either have to to live with many files being read-only or create a symlink from the file you want to protect, e.g. /etc/passwd to a file residing on your read-only media, but that again would be easy to circumvent by root, simply delete the symlink and create new content in its place.

So simple answer would be: No and there must be a better way of achieve whatever it is that you want to accomplish by making one or more files permanently read-only.

  • Basically, what I wanted to do is to protect lookup files from being altered. Thus the option of putting password to it and making it read only. Is there any other way of accomplishing this? – user60216 Mar 1 '14 at 6:59
  • I read somewhere that SELinux is also able to enforce certain limitations that even root can't break. That will only work if root can't turn off SELinux or boot from other media. – Alois Mahdal Mar 1 '14 at 11:53
  • That is true, but it isn't exactly making a file read-only no matter what, only to make it better protected (root can still circumvent SELinux by turning it off, for example by booting a kernel which doesn't have SELinux compiled in or simply by using SELinux enforcement commands to turn it off). – Sami Laine Mar 1 '14 at 13:12

The only way you could have a file that is read only and still on the system is have an ISO file that then contains the read-only file. Then the file will be read-only as the ISO filesystem for CDRoms is read only. You could then mount the ISO file somewhere to allow access to the file. You don't need to write the ISO file to a CD.

However, if you ever need to modify the file, you need to create a new ISO file with the new file within the ISO file system as you cannot modify files within an ISO file system.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.