How to prevent directory from being deleted by user?
Solution 1
What said Class Stacker in his answer is correct, but it didn't solved your problem. To prevent a directory from being deleted by the user which owns all rights to the parent directory (/home/pandya
in your case) you have to use the chattr
command.
Here is an example:
$ sudo mkdir dir1
$ sudo chattr +i dir1
$ rmdir dir1
rmdir: failed to remove ‘dir1’: Operation not permitted
$ rm -r dir1
rm: remove write-protected directory ‘dir1’? y
rm: cannot remove ‘dir1’: Operation not permitted
$ chattr -i dir1
chattr: Permission denied while setting flags on dir1
And in Nautilus:
Please read man chattr
for more info.
Solution 2
Actually, directories are special files.
In a directory /home/user
, the right of creating or removing entires (such as files or directories) is determined by the rights of /home/user
itself, not by the rights of the entries.
In your case, it would be best to provide a subdirectory, such as /home/user/fixed
, and set the rights of that to r-x
. Then, the user will be unable to create or delete files or directories in there. He may still be able to edit, descent, etc based on the rights of the individual files and directories inside /home/user/fixed
.
Related videos on Youtube
Pandya
Started using Linux and StackExchange since Ubuntu 12.04 LTS. Then Upgraded to 14.04 LTS. Now I am using Debian GNU/Linux on my Laptop and PureOS on old Desktop computer. I recommend visiting the Philosophy of GNU Project As I've replaced Ubuntu with Debian GNU/Linux, Now my question(s) are became off-topic on AskUbuntu. So, I continue to Unix & Linux. The second reason for my shifting to U & L is I found U & L more interesting than AU since AU is only Ubuntu specific whereas U & L is a broad concept and in my opinion U & L deserves generic questions. (I know why SE has AU & U & L both).
Updated on September 18, 2022Comments
-
Pandya over 1 year
Suppose a directory
dir1
is created bysudo
on Desktop.sudo mkdir dir1
Then I applied
chown
andchmod
as following:sudo chown root:root dir1 sudo chmod go-rwx dir1
Now
dir1
is only accessible with ownerroot
.$ ls -ld dir1 drwx------ 2 root root 4096 Jul 29 19:21 dir1
If user (
$USER
=pandya
) try to deletedir1
with GUI nautilus (withoutsudo
), then he can't which is ok.But if tried to remove with terminal then he can which is not ok:-
rm -r
(withoutsudo
):$ rm -r dir1 rm: descend into write-protected directory ‘dir1’? Y rm: remove write-protected directory ‘dir1’? Y $
And more easily with
rmdir
! (without sudo):$ rmdir dir1 $
Thus, How to prevent
dir1
to be delete with user than notsudo
?[optional]
My ultimate aim is: Only owner can delete directory, group and other only can read/execute.-
Admin almost 10 yearsYou need to fix this one directory higher.
-
Pandya almost 10 yearsI have only knowledge about
chown
andchmod
. Now Can you help me difference between mode (bychmod
) and attribute (bychattr
)? or it is more suitable that I ask another question for it which i am thinking? -
Radu Rădeanu almost 10 years@Pandya
chmod - change file mode bits
andchattr - change file attributes
. Just read the manual page of each command and probably you will understand what each one does. -
Frank Nocke over 7 yearsGood explanation on this
immutable flag
can be found here -
Serge Stroobandt almost 4 yearsHowever, by rendering a directory immutable one cannot create, edit or delete any subdirectories or files within this immutable directory.
-
João Pimentel Ferreira about 2 yearsWhen a directory is
immutable
can't we add files therein? -
João Pimentel Ferreira about 2 yearsyou have also
undeletable
flag