LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Automatically change permissions/ownership of files in a directory (https://www.linuxquestions.org/questions/linux-newbie-8/automatically-change-permissions-ownership-of-files-in-a-directory-938777/)

Shimdidly 04-08-2012 03:07 PM

Automatically change permissions/ownership of files in a directory
 
Hi, my friend and I created an FTP server to drop files into so we can share them anytime. His user is 'david' and mine, 'shimdidly'. We are using vsftpd. We created a special folder called 'share' where we put all of our files. When one of us uploads a file through FTP, the file is placed in the folder with the user and group set to whoever uploaded it. If 'david' uploaded the file, it would read
Code:

-rwxrwx--- 1 david    david
I don't want 'everyone' to have any permissions. I only want users part of the group 'share' to be able to r/w/x any of the files in the share folder.
So I want to set it up so that any files/folders within the share folder automatically fall under the group 'share'. Sure I can SSH into the server run 'sudo chgrp -R share share' after every file upload. But that is cumbersome.

Dark_Helmet 04-08-2012 03:16 PM

You want to enable the setgid bit on the directory containing the files.

See: Wikipedia: Filesystem Permissions

In practice:
1. Navigate to the parent of the directory containing the files (e.g. if /var/ftp/userfiles contains the files, navigate to /var/ftp)
2. Change the ownership of the directory containing the files to group 'share.'
Code:

root@localhost# chown :share userfiles
3. Apply the setgid bit:
Code:

root@localhost# chmod g+s userfiles
From that point on, any files created in userfiles will automatically belong to the share group. Further, any subdirectories created will also belong to group share and will also inherit the setgid bit.

Note: you will need to manually correct any pre-existing files or directories at the time you set the setgid bit.

Note2: you will also, of course, need to make sure that you and your friend belong to the share group.

Shimdidly 04-08-2012 08:17 PM

Thanks very much!

Dark_Helmet 04-08-2012 08:22 PM

Glad I could help.

One other thing I wanted to mention:
The setgid bit on the directory takes care of the initial group assignment. It does not enforce group ownership after that point. In other words, the setgid bit will not prevent a user with appropriate permissions from manually changing the group ownership at a later date.


All times are GMT -5. The time now is 03:35 PM.