Setting Default File Permission in Linux™

Posted on 2012/01/08 Comments

Here’s the situation; I want every newly created file/directory in /var/www to be owned by user kucrut and group apache, and they should have read/write persmission, while all other users/groups should only have read permission. We will use ACL for this, and it’s usually installed by default.

First, we need to make sure that the partition which the /var/www directory resides (usually /has ACL enabled. The /etc/fstab line should look like this:

# Partition  # Mount point   # Filesystem     # Options         # Dump/pass
/dev/sda5    /               ext4             noatime,acl       0 1

If acl isn’t there yet, add it, save the file, and remount the partition by running this command as root (or use sudo):

mount -o remount /

If you can’t remount the partition for whatever reason, just reboot :)

Now run this set of commands as root, and don’t forget to change kucrut to your own username:

# Set ownership
chown -R kucrut:apache /var/www

# Set permission
chmod -R g+ws /var/www

# Set default permission for newly created files/directories
setfacl -d -m g::rwx /var/www
setfacl -d -m o::rx /var/www

… and we’re done.

Try creating a new file and check its permission:

touch /var/www/xyz && ls -l /var/www/xyz
-rw-rw-r-- 1 kucrut apache 0 2012-01-08 /var/www/xyz

Source: LinuxQuestions.org

Leave a Reply

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

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>