Tip: group-writable directory

Often there is a need for a "project" directory where all group members have write access.
In theory this is a bit risky in case more than one user opens the same file; if both do changes then you don't know what you get.
So either there is a good coordination in the group, or the files are opened with an application that tells you if it's in use.

How to make a group-writable directory in LUnix?
The best approach is chmod 2770 on directories, that is full read+write+execute rights for user(owner) and group, and no access for others.
The g+s bit automatically switches new files to the directory group (instead of the user's current group).

Unfortunately the user's umask applies:
if the user has a secure umask 022 then the new files are not group-writable.
This cannot be solved with the Unix standard permissions.
But one can solve it with an ACL setfacl -m d::rwx

The following script creates or modifies a directory(-tree) to become group-writable:

 cat mkdirg+w
#!/bin/sh
PATH=/bin:/usr/bin:/sbin:/usr/sbin
export PATH
case $1 in
( -f | --files ) files=1; shift;;
( -?* || "" ) echo "usage: $0 [-f] dir..."; exit 1;;
esac
mkdir -p "$@" || exit
# set g+w and g+s on directories
# set a default ACL to create new files with g+w
find "$@" -type d -exec chmod 2770 {} \; -exec setfacl -m d::rwx {} \;
[ -n "$files" ] &&
find "$@" -type f -exec chmod ug+rw {} \;

The -f option will additionally roll the 660 permissions to the files.
You can recursively change the group at any time without loosing the Unix or ACL permissions:

chown -hR groupname directory

Regarding ACLs:
The filesystem must support it.
On LUnix you might need a special option during file system creation (warning: a mkfs destroys all data!). Or you might need an additional mount option.
It makes sense to have a group-writable directory on NFS.
Then the NFS server must have ACLs enabled in its filesystem AND in its NFS export.

(update)

4 Likes

This is useful stuff. Thanks.

We have a category for 'Tips and Tutorials' so expect one of the team to move this topic there.

Thanks again.

1 Like

Thanks for posting this tip.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.