CentOSFedoraLinuxOperating SystemsRedHatUbuntu

Grant non-root user write permission to the folder permanently

Today I am going to discuss a scenario where I have to make files and folders writable under a specific folder by selected non-root users. To achieve our goal I am going to use group concept in Linux for managing multiple users.

I have recently installed Apache Web server and want to make /var/www/ directory writable by certain non root user. Lets checkout the steps required to make folder /var/www/ and all it sub file & folders writable by user non-root user testadmin.

Prerequisite:

You must a be a root user or have sudo access to execute these commands. The commands can be executed on various Linux distributions like Ubuntu, CentOS, Fedora and RedHat

Step 1 : Create a group

Lets create a new group say www.

sudo groupadd www

Step 2 : Add user to the group

If the testadmin user does not belong to any primary group then add it to the www group using g flag.

sudo useradd -g testadmin www

If the testadmin user is has a primary group then add it to the www group using G flag. The a flag means append user testadmin to the supplementary group(s).

sudo usermod -a -G www testadmin

Step 3 : Change group of the folder recursively

Lets change the group of all files and folders recursively under /var/www folder to www 

sudo chown -R :www /var/www

Step 4 : Make the folder writable for the group

Make existing files and folder under /var/www folder writable recursively for the group.

sudo chmod -R g+rwX /var/www

Step 5 : Set group id to the folder

Set the group ID to the folder /var/www.

sudo chmod g+s /var/www

If directories are already present under the folder /var/www then set group ID to all the existing directories using following command

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

Note: For GUI mode, reboot the machine after performing all the above steps.

Conclusion:

From now onward, whenever a new file or directory is created in the /var/www/ directory it will inherit the group of the directory instead of the user testadmin who created the file.

That's all folks !

Related Articles

2 Comments

  1. Terrible article - does not explain anything clearly.

    What is the name of your user in this example. Why so spread out across two sections.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top button