How to make directory writable by non-root users

To make folder writable by all the selected non-root users, group can be used in which specific non-root users will be added in the group.

The following steps requires root user privileges and can be followed in Ubuntu, CentOS, Fedora and RedHat

1) Create a group:

sudo groupadd <groupname>

2) Add user to the group:

sudo useradd -g <username> <groupname>

If user is already present then use following command

sudo usermod -a -G <groupname> <username>

3) Change group of the folder recursively:

sudo chown -R :<groupname> <folderpath>

4) Make the folder writable for the group:

sudo chmod -R g+rwX <folderpath>

5) Set group id:

sudo chmod g+s <folderpath>

if directories are present under the folder, then use following command

find <folderpath>/var/www -type d -exec chmod g+s '{}' \;

when a new file is created in the directory it will inherit the group of the directory instead of the user who created the file.

Reboot the machine to check by creating a file/folder using GUI mode.

Example:

To make folder /var/www/ and all it subfolders writable,

sudo groupadd www

sudo useradd -g testadmin www

or

sudo usermod -a -G www testadmin

sudo chown -R :www /var/www
sudo chmod -R g+rwX /var/www

sudo chmod g+s /var/www

or

find /var/www -type d -exec chmod g+s '{}' \;

That's all !


About Abhijit Sandhan

Abhijit Sandhan
Loves Linux, Blogging, Traveling, Hiking and sharing the knowledge!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

CAPTCHA Image

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>