Linux – Keep same file owner for newly created files

chownfileslinuxpermissionsusers

This question is regarding samba file access.

I have created a folder A, and under folder A created two folders B and C. And also created three users A, B and C.

User A has access to all three folders but User B has only access to folder B and User C has only access to folder C.

Permission of B & C folders are:

drwxrwxr-x 3 a b 4096 May 10 16:22 b
drwxrwxr-x 3 a c 4096 May 10 16:43 c

Problem:

When user B creates any new file under folder B, it's permission becomes

drwxr-x--- 2 b b 4096 May 10 16:21 New Folder

whereas I want it to keep the owner, group and permission same as folder B for any newly created files.

Best Answer

Folder b and c are owned by user b and c. A file created by a user will belong to that user.

You can use the user permission for b and c, and the group permissions for a. If you set the SGID bit (g+s) on a folder, created files will get the group permission of that folder.

mkdir a
chown a:a a
chmod g+s a

mkdir b
chown b:a b

mkdir c
chown c:a c

(assuming all users are in a group of the same name.)

Related Question