13

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

1
  • 9
    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)?
    – fschmitt
    Commented Sep 27, 2010 at 12:45

5 Answers 5

34

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.

5
  • 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.
    – Kumar
    Commented Sep 27, 2010 at 13:05
  • 12
    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)
    – msw
    Commented Sep 27, 2010 at 13:23
  • 5
    @msw: One could argue that it's a terrible joke even if you are familiar with the field. Commented Sep 27, 2010 at 13:48
  • @matthew crumley: guilty as charged :)
    – msw
    Commented Sep 27, 2010 at 15:45
  • @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 :) Commented Sep 27, 2010 at 17:08
34

Put it on a CD-rom! ;)

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

3
  • There is a sense behind the reputation limit for comments ;-) Just a small thought-provoking impulse. no flame intended ;-)
    – echox
    Commented Sep 27, 2010 at 16:37
  • 1
    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.
    – Lie Ryan
    Commented Sep 28, 2010 at 8:19
  • Well, you can now comment because you posted what you thought should be a comment, as an answer! So this actually helped you XD.
    – 0xc0de
    Commented Dec 20, 2021 at 5:22
15

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.

2

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 *".

2
  • long ago and far away. How did you actually do it (commands) ? Commented Sep 15, 2015 at 10:43
  • Wow, I like that idea!
    – 0xc0de
    Commented Dec 20, 2021 at 5:20
1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .