Skip to content

Instantly share code, notes, and snippets.

@dainis-boumber
Last active June 2, 2021 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dainis-boumber/a1615d65783c75732756553cf9a96efb to your computer and use it in GitHub Desktop.
Save dainis-boumber/a1615d65783c75732756553cf9a96efb to your computer and use it in GitHub Desktop.
Making directory that cannot be deleted but is otherwise normal (not immutable) and lets users having read, write, and execute permissions to contents

How to create an otherwise normal direcory that cannot be deleted

Author: Dainis Boumber

Overview

Follow the instructions in this gist to create a directory under / for which, depending on the user's permissions, it will be possible to:

  • read, copy, write, move, paste, and delete files and folders inside
  • run and execute programs and scripts within

yet it will NOT possible to:

  • Delete the directory itself
  • Move or rename it
  • Change it's permissions, then proceed to do anything (can often be done without sudo)

Motivation

This is very useful for protection from accidental or malicious deletion of a directory containing all work-related projects or anything else important

Instructions

First, you need to be a root user, so do sudo su. Next, you will essentially copy over the permissions and access rights of the /tmp directory, because it does what our directory will be doing. In this example, we are creating a work directory:

root@ubuntu:/# mkdir /work
root@ubuntu:/# chmod --reference=/tmp /work

Now, log out of su and test if it worked:

dainis@ubuntu:/$ mkdir /work/ner
dainis@ubuntu:/$ ls /work
ner
dainis@ubuntu:/$ rm -rf /work
rm: cannot remove `/work': Permission denied
dainis@ubuntu:/$ rm -rf /work/ner
dainis@ubuntu:/$ 

This it, you are done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment