umask and groups

Hi

I see that I can use umask to set the default permissions on files created by a script like so -

umask u=rwx,g=rwx,o=

So that would grant rwx to the user and group.

My question is how do I control the group this applies to?

I want a script to create files under /tmp/script_dir that are rwx for the user running the script and members of one of their groups, (say groupx).

So if user A has 5 groups and groupx

and user B has 3 groups and groupx

How do I set rwx permissions for groupx only. Can this be done using umask?

So if user A runs the script and it creates files and directories under /tmp/script_dir, I want to have user A as the user and groupx as the group by default, without having to specifically chown them.

Any help appreciated as ever

Steady

Try setting the setgid bit on the directory's entry to the desired group;

$ chmod g+s tdir
$ chown :groupx tdir
$ ls -lad  tdir
drwxr-srwx 2 nobody groupx 4096 Aug 26 12:54 tdir/
$ touch tdir/xy
$ ls -la tdir/
-rw-rw-r-- 1 usera groupx 0 Aug 26 12:58 xy

Is this what you were out for?

1 Like

Thanks RudiC

Exactly what I needed :slight_smile:

This works on some systems on some file systems; it won't work on OS X nor on openBSD.

According to the standards, the group ID on a newly created file is either set to the group ID of the containing directory or to the effective group ID of the creating process. (Historically, BSD based systems used the group ID of the containing directory and System V based systems used the effective group ID of the creating process. Solaris systems used the set-GID bit on a directory to allow users to choose the behavior they wanted.) The Linux open(2) man page says that Linux systems sometimes mimic the Solaris behavior depending on the file system type and mount options used when the file system was mounted.