Search code examples
linuxsshfile-permissionsscpumask

scp file not setting correct owner


Does SCP have a problem setting file permissions or have I misconfiguration my server?

Use case:

There is a file on a server that I want to edit called "importantFile.txt". The file has owner and group of "master":

ls -l importantFile.txt:
-rw-rw-r--  1 master master     7 Mar 18 08:11 importantFile.txt

I am called "slave" but luckily, I am in group "master" so I can edit the file as I see fit. However, I'm a lazy slave and can't be bothered to edit the file on the server, I'd prefer to edit the file on my local machine and SCP it to the server:

echo "bored slave info" > importantFile.txt
scp importantFile.txt slave@theServerAddress:/pathToFile/importantFile.txt

If I do this, the contents of the file on the server are uploaded fine and the timestamp of the file is updated but the permissions of the file don't change, the file is still owned by "master". This is a problem because if "slave" uploaded bad content, no one would know it was "slave" who caused the problem, "master" would look guilty.

Perhaps I have to set a umask? if so where? I tried .bash_profile without success and haven't found anything on Google about umask in /etc/ssh/sshd_config.


Solution

  • 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