4

Problem

I know that a file that is created in a directory with the GID-bit set it will inherit the diretory's group ownership. This however doesn't work for files that are moved to that directory as stated in https://en.wikipedia.org/wiki/Setuid#setuid_and_setgid_on_directories

It happens that users move files to a shared folder instead of copying them or creating new files, there are now files in the shared folder that are not accessible to everybody.

Example:

There is a shread folder

john:/home/common$ ls -l
drwxrws--- 15 john users 4096 Feb 11 09:14 shared

and a file in a private folder.

john:/home/john$ ls -l
-rw-rw---- 1 john john 512 Feb 11 09:14 test.txt

That file is moved to the shared folder. It keeps the group ownership.

john:/home/common/shared$ mv /home/john/test.txt .
john:/home/common/shared$ ls -l
-rw-rw---- 1 john john 512 Feb 11 09:14 test.txt

When the file is moved to shared, the group stays john and is not changed to users. If the file is copied, the group changed to users.

My Question

Even though this behaviour is logical from a system point of view, it is not what is expected from a user point of view.

Is there a way to configure the shared directory so that the group is also changed automatically for files that are moved? What is the best practice to achieve this goal?

Jdz
  • 43
  • 3

1 Answers1

3

The setgid does affect the newly created files (using touch, cp), and it does not affect the already created files (using mv).

There are several solutions like the ones posted here. Also, you can add a cron job to periodically chgrp all files / folders under your shared folder.

Another possible solution is to use incrontab -e to fix group ownership when a file is moved to your shared folder. Be careful, this triggers events only for shared folder itself not any other subfolder.

/path/to/shared/dir IN_MOVED_TO /bin/chgrp users $@/$#
Khaled
  • 36,533
  • 8
  • 72
  • 99