+1  A: 

You have to delete the file to overwrite it. Wether you are able to do that depends on the directory's permissions and ownership. Hijacking ownership of an already existing file is not possible. The write permission you have is only applied on the contents of the file.

ypnos
+1  A: 

That's nothing special about scp - try logging on to the server as slave, and editing the file using your favourite text editor... You'll find the same behaviour occurs... Writing to a file does not make you the owner of the file.


Example:

as root

#cd /tmp
#mkdir fubar
#chgrp vboxusers fubar
#cd fubar/
#touch testfile
#chgrp vboxusers testfile 
#chmod g+w . testfile
#ls -al
total 16
drwxrwxr-x  2 root vboxusers  4096 2009-03-19 10:30 .
drwxrwxrwt 15 root root      12288 2009-03-19 10:29 ..
-rw-rw-r--  1 root vboxusers     0 2009-03-19 10:30 testfile
#echo foo > testfile 
#ls -al
total 20
drwxrwxr-x  2 root vboxusers  4096 2009-03-19 10:30 .
drwxrwxrwt 15 root root      12288 2009-03-19 10:29 ..
-rw-rw-r--  1 root vboxusers     4 2009-03-19 10:30 testfile

as user (in vboxusers group)

>cd /tmp/fubar
>ls -al
total 20
drwxrwxr-x  2 root vboxusers  4096 2009-03-19 10:30 .
drwxrwxrwt 15 root root      12288 2009-03-19 10:29 ..
-rw-rw-r--  1 root vboxusers     4 2009-03-19 10:30 testfile
>echo bar >> testfile 
>ls -al
total 20
drwxrwxr-x  2 root vboxusers  4096 2009-03-19 10:30 .
drwxrwxrwt 15 root root      12288 2009-03-19 10:29 ..
-rw-rw-r--  1 root vboxusers     8 2009-03-19 10:31 testfile
>vim testfile
>ls -al
total 20
drwxrwxr-x  2 root vboxusers  4096 2009-03-19 10:31 .
drwxrwxrwt 15 root root      12288 2009-03-19 10:31 ..
-rw-rw-r--  1 root vboxusers    12 2009-03-19 10:31 testfile
>cat testfile 
foo
bar
baz
Stobor
I did try this and I found writing to the file does make me the owner (and group) of the file. In fact, the only way to keep the group of the original file the same was to change the "set-groupID" flag in of the parent directory (`find . -type d -exec chmod g+s {} \;`)
Tom
Writing to a new file? or Writing to an existing file?
Stobor
I worked though your helpful example and found the same results as you. I realised my original mistake, instead of editing the contents of the file using "echo blah > testfile" I was using emacs and it seems emacs does change the file ownership. My mistake - and much appreciation for your answer.
Tom
You did say "using your favourite text editor"! :-) - Perhaps Emacs shouldn't be my favourite! lol.
Tom
A: 

I had misunderstood the way files work, modifying file contents do not change ownership or group.

Why the confusion? EMACS - Whenever I was editing a file I was using Emacs and Emacs does change the owner and group to the current user. It does this because it makes a backup file at save time by moving the "filename" to "filename~" and creating a new file called "filename" - because it's a new file, it has the current users file permissions. I guess this is 1up to VI fans?

Tom
+1  A: 

It seems you can configure how Emacs deals with this through the backup-by-copying-when-mismatch variable (see the Emacs Manual or type C-h-v backup-by-copying-when-mismatch in Emacs).

I actually filed a bug report about this, because I thought it was a bug in Tramp.

jozilla