8

I have a project with a number of files that I created in Linux.

And I needed to give another user write permission on them. So I created a "dev" group of which he and I are members and changed the ownership of the files to this group.

A typical file now has these permissions :

-rw-rw-r--  1 phil dev   5617 Jul 14 15:45 profile.html

However, when I edit the file profile.html it reverts to me like this, and my colleague loses the ability to edit.

-rw-rw-r--  1 phil phil  5617 Jul 14 15:45 profile.html

How do I avoid it reverting like this? Was changing the ownership the wrong thing to do? Or was it done with the wrong options? Or is this to do with my editor (emacs) configuration?

interstar
  • 1,281
  • 4
  • 18
  • 23

4 Answers4

17

On the containing folder you'll want to change the group to be dev and then use mark it set-gid.

chgrp dev <containing-folder>
chmod g+ws <containing-folder>

The set gid bit makes files created in that folder to inherit the group of the folder as well as marking the setgid bit on any new folders. You'll want to be careful when moving files into the directory as that will preserve their existing permissions.

Frenchie
  • 1,282
  • 9
  • 14
8

You can use the setgid bit on a directory to preserve group ownerships by children.

chown :dev directory/
chmod g+s directory/
Dan Carley
  • 25,617
  • 5
  • 53
  • 70
7

You could also mount the filesystem ( assuming ext2/3 ) with the grpid mount option which will make it so whenever you create a new file in a directory, it will make the group owner the same as the parent directory. So then you would just make it so the directory that these files exist in are owned by the group 'dev'.

To remount it if it is the root partition (example):

sudo mount -o remount,grpid,rw,relatime,errors=remount-ro /

From 'man mount 8':

grpid or bsdgroups / nogrpid or sysvgroups
These options define what group id a newly created file gets.

When grpid is set, it takes the group id of the directory in which it is created; otherwise (the default) it takes the fsgid of the current process, unless the directory has the setgid bit set, in which case it takes the gid from the parent directory, and also gets the setgid bit set if it is a directory itself.

Kyle Brandt
  • 83,619
  • 74
  • 305
  • 448
1

Default behavior for emacs is to create the backup file by renaming. From the emacs manual:

Emacs can rename the original file so that it becomes a backup file, and then write the buffer being saved into a new file. After this procedure, any other names (i.e., hard links) of the original file now refer to the backup file. The new file is owned by the user doing the editing, and its group is the default for new files written by the user in that directory.by the user in that directory.

There are several ways to change this.

  • Set the group and sticky bit, as others describe.
  • Run 'newgrp dev' prior to editing the file, so that your default group is dev.

Or emacs specific:

  • Set file-precious-flag in emacs, which changes the behavior to preserve group, but has other side effects.
  • Set backup-by-copying-when-mismatch in emacs, which uses copying rather than renaming when that would cause the owner or group to change.

So, add to your .emacs:

(setq backup-by-copying-when-mismatch 't)

My preference is actually 'newgrp dev', as it is an explicit switch from "personal" mode (the files I edit are just mine), to group dev mode (the files I now edit are shared amongst the group).

jmanning2k
  • 302
  • 2
  • 9