LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-25-2019, 12:12 PM   #1
linux_nerd
LQ Newbie
 
Registered: Jun 2015
Posts: 26

Rep: Reputation: Disabled
Making directories immutable and being able to create files within the directory?


Hello, does anyone have any ideas as to how one could make a directory immutable, just the directory to protect it from removal but still be able to write files within the directory? This came up because I have had several people that have deleted their directories several times.
 
Old 02-25-2019, 12:54 PM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by linux_nerd View Post
Hello, does anyone have any ideas as to how one could make a directory immutable, just the directory to protect it from removal but still be able to write files within the directory? This came up because I have had several people that have deleted their directories several times.
Not sure that can be managed with permissions. I'd think a directory needs to be writeable to be written to.

One idea: Create a hidden file within the directory owned by root
Code:
touch /home/userdir/.donotremove
chown root.root /home/userdir/.donotremove
Now a rmdir should fail with
Code:
rmdir: failed to remove ‘/home/userdir/’: Directory not empty
 
1 members found this post helpful.
Old 02-25-2019, 01:41 PM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
I do not think you can do this with a single directory. You could, however, emded the directory in a "protective" parent directory and change the attributes of the parent directory to immutable:
Code:
# mkdir -p protect/workfolder
# chgrp <usergroup> protect/workfolder
# chmod g+w protect/workfolder
# chattr -V +i protect
You must issue the above commands as root. Setting the +i flag on protect will make 'protect' immutable, i.e., you will not be able to delete 'protect' or any of its contents. You can, however, create and delete files in 'workfolder'.
 
Old 02-25-2019, 02:21 PM   #4
linux_nerd
LQ Newbie
 
Registered: Jun 2015
Posts: 26

Original Poster
Rep: Reputation: Disabled
Cool, thanks for the ideas! I think this is how im going to do it.
create a .dontremove file as suggested above and then I will make the .file immutable which should cause rm to bail because the directory is not empty and the file cant be removed.
 
Old 02-25-2019, 04:15 PM   #5
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by linux_nerd View Post
Cool, thanks for the ideas! I think this is how im going to do it.
create a .dontremove file as suggested above and then I will make the .file immutable which should cause rm to bail because the directory is not empty and the file cant be removed.
You're welcome. That should do it. I learned about chattr in this thread...very interesting.

I note only root can set the immutable attribute. Of course, even without using chattr, were the file owned by root, no one else could remove it either.
 
Old 02-25-2019, 09:56 PM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by scasey View Post
Of course, even without using chattr, were the file owned by root, no one else could remove it either.
You've been around here long enough to know better than that. Absent the "immutable" attribute, all you need is write and execute permission on the directory in order to remove any file therein. That message from rm that a file is write protected is just a helpful suggestion, and you just need to respond "y" to proceed.
 
1 members found this post helpful.
Old 02-26-2019, 12:45 AM   #7
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by rknichols View Post
You've been around here long enough to know better than that. Absent the "immutable" attribute, all you need is write and execute permission on the directory in order to remove any file therein. That message from rm that a file is write protected is just a helpful suggestion, and you just need to respond "y" to proceed.
I did not know that! Yes, I should have. One of the challenges of working as root most of the time...besides being dangerous, it can make one ignorant
Thanks for the correction.
 
Old 02-26-2019, 01:00 AM   #8
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Sticky bit

Quote:
Originally Posted by rknichols View Post
You've been around here long enough to know better than that. Absent the "immutable" attribute, all you need is write and execute permission on the directory in order to remove any file therein. That message from rm that a file is write protected is just a helpful suggestion, and you just need to respond "y" to proceed.
You could utilize the sticky bit to avoid deleting files from other users:

Code:
# mkdir workfolder # all commands as root
# chgrp <usergroup> workfolder
# chmod 1775 workfolder
# touch workfolder/lockfile
# chmod g+w workfolder/lockfile # even the same group cannot delete the file, only the user
The sticky bit (at least in linux) lets you create files in workfolder but prohibits you from deleting files that you do not own; unless you are root. The sticky bit - in contrast to chattr - does not prohibit root from doing "damage".
If none of the users have root privileges then this might also be a suitable "lockfile" solution that does not require chattr. Depending on how the filesystem was created, chattr may not always be available.

PS:
I only changed the group to the user's group for demonstration purposes only. If you want a "lockfile" solution then its group should probably stay root.

Last edited by crts; 02-26-2019 at 01:21 PM. Reason: Added title
 
Old 02-26-2019, 08:44 AM   #9
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by crts View Post
You could utilize the sticky bit to avoid deleting files from other users:

Code:
# mkdir workfolder # all commands as root
# chgrp <usergroup> workfolder
# chmod 1775 workfolder
# touch workfolder/lockfile
# chmod g+w workfolder/lockfile # even the same group cannot delete the file, only the user
The sticky bit (at least in linux) lets you create files in workfolder but prohibits you from deleting files that you do not own; unless you are root. The sticky bit - in contrast to chattr - does not prohibit root from doing "damage".
Being the owner of the directory is also sufficient to bypass the sticky bit, and the original post in this thread mentioned "people that have deleted their directories."
 
Old 02-26-2019, 08:53 AM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
what directories are they, the standard ones, Documents, Music, etc, or ones they have created themselves? You could perhaps create a script the takes a "snap shot" of what dirs they have, then upon logging in to there account check them the if not there re-created them. it could get a bit hairy if they delete a directory on purpose no longer wanting it though.

don't they have any idea ow to create a dir? A redundant question I m sure. Seeings how you're asking how to make it so they cannot delete the directories.

Yet, if they can delete them, then they have rights to create directories too. Maybe a short training session for the "employees" to teach them some basics they need to know. So they can fix what they screwed up.

Last edited by BW-userx; 02-26-2019 at 08:55 AM.
 
Old 02-26-2019, 09:49 AM   #11
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
I am not sure how this
Quote:
Originally Posted by rknichols View Post
Being the owner of the directory is also sufficient to bypass the sticky bit ...
relates to my "sticky-bit" solution. I explicitly posted that the commands need to be issued by root, thus the folder is created and owned by root and not the user. Notice, that I only change the group to the user's group but not ownership. If the folder exists already then, of course, the first step needs to be omitted and instead of chgrp
Code:
# chown 0:<usergroup> workfolder
needs to be issued. In any case, the resulting permissions/ownerships are:
Code:
# ls -ld workfolder
drwxrwxr-t 2 root <usergroup> 4.0K Feb 26 16:00 workfolder/
# ls -l workfolder
-rw-r--r-- 1 root root 0 Feb 26 16:00 lockfile
The user can neither change ownership nor remove the sticky-bit.

Since all solutions require root intervention and result in the user losing control over the directory, I see no reason why the folder should not be owned by root.

Please elaborate if I am missing something.

Last edited by crts; 02-26-2019 at 09:50 AM.
 
Old 12-31-2019, 08:24 PM   #12
MasterCATZ
LQ Newbie
 
Registered: Dec 2016
Posts: 2

Rep: Reputation: Disabled
also after something similar

I need to stop the time stamps of the folders being updated when files are changed inside the folder

if I do immutable "chattr -i" , this is also stopping files / folders inside from being created even if I "chmod 777 -R" the folder

The Folders contain symlinked files inside with another program adding other additional files / doing updates

I need the Folders timestamps to permanently stay the same as creation date and not change to last modified date

folders show they should allow contents to be changed
ls "drwxrwxrwx"
but permission is denied
lsattr ----i---------------

Last edited by MasterCATZ; 12-31-2019 at 08:31 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
question: 'onclick' within 'onmouseover' within 'form' within 'table' - how is it possible? rblampain Programming 4 04-25-2017 08:49 PM
Copying files and sub-directories of a directory except the directories named ".abc" sri1025 Linux - General 2 08-24-2010 08:53 AM
Finding files with immutable bit set SlowCoder Linux - General 2 02-24-2009 01:57 PM
Count the number of files in a directory and sub-directories within that directory soumyajit.haldar Linux - Software 4 03-20-2007 06:22 AM
Immutable files wasp Programming 3 11-15-2005 10:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration