Tuesday, 18 November 2014

How to make new files and directoris inherit group and permissions in GNU/Linux

~$ mkdir directory
~$ chmod 770 directory  # gives full permissions for owner and group, none to others
~$ chmod g+s directory  # makes new filed and dirs inherit group ownership
~$ chgrp grp directory  # changes group ownership to "grp"
~$ cd directory/
~/directory$ ls -la
total 8
drwxrws--- 2 usr grp 4096 Nov 18 07:42 .
drwxr-xr-x 6 usr usr 4096 Nov 18 07:42 ..



Note the s permission. This tells you the group ownership will be inherited to files and subdirectories within this directory.
In order to make permissions inherit, we need to use ACL - Access Control Lists.
If the setfacl command below does not work, you can install it (on Debian/Ubuntu and compatible systems) using: apt-get install acl
To make it easy, make sure that the directory your're setting ACL on has the exact permissions you wish to be inherited prior to executing setfacl.

~/directory$ setfacl -Rdm g:grp:rwx .
~/directory$ ls -la
total 8
drwxrws---+ 2 usr grp 4096 Nov 18 07:42 .
drwxr-xr-x  6 usr usr 4096 Nov 18 07:42 ..

The + after the permissions indicates that ACL is in use.

~/directory$ mkdir subdir
~/directory$ touch subdir/file
~/directory$ ls -la subdir/
total 8
drwxrws---+ 2 usr grp 4096 Nov 18 07:43 .
drwxrws---+ 3 usr grp 4096 Nov 18 07:43 ..
-rw-rw----+ 1 usr grp    0 Nov 18 07:43 file

Everything you create under directory and any directory below receives the group and permission you defined.
In order to see the permissions in use, you can use getfacl:

~/directory$ getfacl .
# file: .
# owner: usr
# group: grp
# flags: -s-
user::rwx
group::rwx
other::---
default:user::rwx
default:group::rwx
default:group:grp:rwx
default:mask::rwx
default:other::---

2 comments:

  1. Your content seems quite good, but your presentation leaves much to be desired. Too much noise with the background. I would recommend a plain background, perhaps straight black. The flames were somewhat of a deterrent to reading your content.

    ReplyDelete
    Replies
    1. Mark John, you're absolutely right. The background is horrible, but way back when I made it I was playing around with css as I needed to figure out how to make text appear (sort of) readable against backgrounds of different brightness and colour, and it evolved into this, plus I thought the flames were cool (at the time).

      Again, totally agree with you. I will probably improve it some day, but as with the shoes of a shoemaker's son, my own stuff comes way back in line after client work.

      Delete