dbj44
dbj44

Reputation: 1998

Set default group for newly created files/folders in linux?

When I create a new folder in linux it always 'inherits' the same group. What controls the group ownership of newly created files/folders and how is it reconfigured?

I have been reading about suid and sgid, but I'm not sure this is the way to go.

Thanks.

Upvotes: 6

Views: 10967

Answers (2)

vikrantt
vikrantt

Reputation: 2665

A better approach is to change the group of the directory so that all new files there are created with a particular group:

sudo chgrp www-data /my/dir
sudo chmod g+s /my/dir

Upvotes: 13

hek2mgl
hek2mgl

Reputation: 157947

If you are using the mkdir command the ownership is set to the user who creates the folder and the group ownership will be set to the primary group of that user.

You can use the install command to create a folder with a different ownership:

sudo install -o www-data -g www-data -d test

The above command creates the folder test and sets ownership and group ownership to www-data (for example)


Of course you can also use

sudo -u www-data mkdir test

to create a folder owned by USER and group owned by it's primary group. It leads to the same results as the install command above.

Upvotes: 1

Related Questions