How to create a file even root user can't delete it

12,713

Solution 1

Simple answer: You can't, root can do everything.

You can set the "i" attribute with chattr (at least if you are on ext{2,3,4}) which makes a file unchangeable but root can just unset the attribute and delete the file anyways.

More complex (and ugly hackish workaround): Put the directory you want unchangeable for root on remote server and mount it via NFS or SMB. If the server does not offer write permissions that locks out the local root account. Of course the local root account could just copy the files over locally, unmount the remote stuff, put the copy in place and change that.

You cannot lock out root from deleting your files. If you cannot trust your root to keep files intact, you are having a social problem, not a technical one.

Solution 2

Put it on a CD-rom! ;)

(this should be a comment but I can't do comments...)

Solution 3

Put your file on a readonly filesystem. This may be a remote FS where root is no longer root, a CD-ROM or a removable device that can be write-protected, e.g., a SD card.

Solution 4

Long ago I wrote a kernel patch (to 2.2.ancient) where attempting to delete a file called SELFDESTRUCT killed the calling process. This guarded against accidental "rm -rf *".

Solution 5

You can use chattr +i to make a file immutable.

Of course a savvy user with root access could still use chattr -i to remove the immutable attribute and then delete the file, but they could undo any of the other attempts to block access listed here as well.

I would argue that chattr is the correct Unix way to do this.

Share:
12,713

Related videos on Youtube

Kumar
Author by

Kumar

Working as System Administrator at Coimbatore based Company

Updated on September 17, 2022

Comments

  • Kumar
    Kumar almost 2 years

    How to create a file even root user can't delete it ?

    • Uri Cohen
      Uri Cohen over 13 years
      While tante's answer should cover your question, one meta-comment to it: It would be much easier to answer if you added your rationale to your question. Maybe there is a work around to your problem not consisting of preventing deletion of the file(s)?
  • Kumar
    Kumar over 13 years
    This question is asked by an interviewer to me. I don't know the answer. I asked to him for answer. He said it possible and find solution by yourself.
  • msw
    msw over 13 years
    someday you too will be an interviewer and be able to ask nonsensical questions of your own: "can root create a file so large that he can't mv it?" (which is a lame joke based on Christian theosophy, and a terrible joke if you you aren't familiar with field)
  • Matthew Crumley
    Matthew Crumley over 13 years
    @msw: One could argue that it's a terrible joke even if you are familiar with the field.
  • msw
    msw over 13 years
    @matthew crumley: guilty as charged :)
  • Cole
    Cole over 13 years
    There is a sense behind the reputation limit for comments ;-) Just a small thought-provoking impulse. no flame intended ;-)
  • Matthew Crumley
    Matthew Crumley over 13 years
    @msw: I just noticed that you even admitted it was a lame joke. And FWIW, I like (some) lame jokes so I actually liked it. This is getting pretty far off topic though :)
  • Ankur Gupta
    Ankur Gupta over 13 years
    +1 But that would work.
  • Lie Ryan
    Lie Ryan over 13 years
    This and the answer by @mouviciel is the only correct solution. Even the almighty @root is still bound by the rules set by the hardware.
  • Michael Durrant
    Michael Durrant almost 9 years
    long ago and far away. How did you actually do it (commands) ?
  • 0xc0de
    0xc0de over 2 years
    Wow, I like that idea!
  • 0xc0de
    0xc0de over 2 years
    Well, you can now comment because you posted what you thought should be a comment, as an answer! So this actually helped you XD.