Categories
Linux Tutorials

Setup a folder so that new files created in it inherit default permissions and group

A quick tutorial showing you how to ensure that new files created in your file structure will inherit the existing user and groups (no matter which user creates the new files). This tutorial is for Debian/Ubuntu and Mac:

Say you have a folder where you have different users accessing it and creating files and folders, such as a web directory, you don’t want each new file being assigned the different users permissions. For example, you may have a Git repository in a folder owned by Apache. If the Git is being run by a different user than the Apache user (which is more secure) then new files pulled into the Git repo will not be accessible by the Apache user. Which is not what you want.

Here is a quick command to run on the folder to set the group ownership of the folder (skip this step if the group ownership is already set)

chown -R user:group /path to folder

Then run the following command to ensure the new files and folders created inherit the default permissions (AKA the sticky bit)

chmod g+s /path/to/folder

By Jonathan Whiting

I enjoy sharing what I am learning and hopefully it's of interest and help to you. I live in Canada with my wife. Follow me on Twitter.

6 replies on “Setup a folder so that new files created in it inherit default permissions and group”

Hi Jonathan,
It works for stick the group, however not for the permissions. There is any command that allows to inherit the permissions of the parent folder for new files?
drwxrws–T parent_directory
-rw——- child_new_file (Stick the group but not the permissions)

Hi Jenny,

Great question. The command chmod g+s ensures that the group ownership is inherited by new files and folders created in it.

What you are looking for is a bit trickier as file permissions are not inherited, but are set when created. You can achieve this using POSIX ACLs

setfacl -d -m u::rwX,g::rwX,o::- /path/to/parent

Explained:
-m modifying the
-d default ACLs
rw give the files read/write permissions. You can change these to what you need.
X uppercase means only directories will receive the +x bit

Also for reference:
u is the user
g is the group
o means other

Thanks for your article, very useful. If there are already files and folders in /path/to/folder should you run `chmod -R g+s /path/to/folder` to ensure that sub-folders also have the sticky bit set?

Hi Jacob,

Thanks for your question. No, it shouldn’t be necessary unless you plan on updating those files too and want the permissions to stay.

Leave a Reply

Your email address will not be published.