8

Is there any way to force a file, created within a directory, to inherit ownership from the parent directory? I tried the sticky bit, but that doesn't seem to work.

Example of what I'm looking for:

drwxrwxr-x www-data somegroup parentdir

When parentdir/newfile.htm is created by someuser:

-rwxrwxr-x www-data somegroup newfile.htm

NOT

-rwxr-xr-x someuser somegroup newfile.htm

Any way this can be done? Thank you!

2 Answers2

6

Linux has something known as Access Control List (ACL). This is a way to extend standard unix permissions and fine tune them. One of the advantages is that it does have inheritance. What could be done, has been referenced by a related post on serverfault, and in your particular case:

sudo setfacl -Rdm g:somegroup:rwx /path/to/parent

As for forcing the files to be owned by the same user, it has been discussed in Getting new files to inherit group permissions on Linux, however forcing the same owner on each file is far more troublesome than having the file to be forced to have same group ownership as done via setfacl. If the group has exactly the same permissions as the owner, there's no point to force the same ownership. Of course, you could always use inotifywait and trigger chown upon file creation, but that's pointless since group ownership already gives you control over the file.

See also:

0

You problem (it's not really a problem) raises in two parts as I understand from you.

First you want to give ownership of files created by a user in that directory directly to the apache user www-data. This can't be done that way.

In real life You can't give something to your friend if he doesn't want it!!

Same thing here, you can't give the ownership to some user without his permission.

So how to solve this here:

you still need to make chown

sudo chown www-data newfile.htm

The other needed is to change the permission of a file to inherits permission of parent directory.

This is not a good habit since the directory normally have execute permission x to make cd available in. But x for a normal file make it executable, and as those files as you mentioned are owned by www-data, this also makes you in trouble with a huge security threat, so my advice don't do it

But anyway if you still want to try : take a look for those two questions

https://superuser.com/questions/264383/how-to-set-file-permissions-so-that-new-files-inherit-same-permissions

https://superuser.com/questions/151911/how-to-make-new-file-permission-inherit-from-the-parent-directory

Maythux
  • 87,123