How To make sure any file or folder created in /var/www/html gets automatically owned by www-data

Automatically Set Ownership to www-data Using incron

If you want to ensure that any file or folder created inside /var/www/html is automatically owned by www-data, you can use incron.

Incron works similarly to cron, but instead of running based on time schedules, it monitors filesystem events such as:

  • File or directory creation
  • File modifications
  • Attribute changes
  • File deletions

Step 1 — Install incron

Install incron using:

sudo apt-get install incron

Step 2 — Allow Root to Use incron

By default, not all users are allowed to use incron. You must explicitly grant permission.

Open the file:

sudo vim /etc/incron.allow

Add the following line:

root

Save and exit.


Step 3 — Configure incrontab

Edit the incrontab for root:

sudo incrontab -u root -e

Add this line:

/var/www/html IN_CREATE /bin/chown -R www-data:www-data /var/www/html/

Save and exit.


How It Works

With this configuration, whenever a new file is created inside /var/www/html, its ownership will automatically be changed to:

www-data:www-data

Explanation of the incron Rule

General format:

<directory> <event> <command>

Breakdown of the rule:

  • /var/www/html → the directory being monitored
  • IN_CREATE → triggers when a new file or directory is created
  • /bin/chown -R www-data:www-data /var/www/html/ → command executed automatically

Important Notes

  • The -R option applies ownership changes recursively to all contents inside the directory.
  • If you only want to change ownership for the newly created file (not recursively), you should modify the command accordingly.
  • Make sure the incron service is running:
sudo systemctl status incron

Summary

Using incron is a practical solution for:

  • Automatically managing file ownership
  • Monitoring filesystem events in real time
  • Eliminating repetitive manual permission changes

Hope this helps 👍


Komentar

Postingan populer dari blog ini

Cara restart / stop windows service (services.msc) dengan bat / cmd

Learning Haproxy Load Balancer with Podman and Go Backends

Konfigurasi ITNSA OpenVPN auth LDAP