Change permissions upon uploading with scp

98,299

Solution 1

If you're copying from a windows machine, you can use WinSCP to copy, and it has an option to set the permissions on the copied files after the upload.

If not, I think your only choice is to execute a chmod on the server after the upload, which you could do remotely with an ssh command:

scp /path/to/file server:/server/path/to/file
ssh server chmod 644 /server/path/to/file

Solution 2

My preferred working solution would be to use rsync instead:

Replace:

scp /path/to/file server:/server/path/to/file

With:

rsync --perms --chmod=u+rwx,g+rwx,o+rwx /path/to/file server:/path/to/file

This prevents you from authenticating twice. There are also a lot of other options with rsync which would probably add value such as being able to preserve owner, group, etc.

Solution 3

I have made some experiments with scp. For new files uploaded to the target server, the files have the same permissions as on the source server. If existing files are overwritten on the target server, the permissions for those files don't change.

I have done these experiments with CentOS 4.6.

Solution 4

You could do it using tar, ssh, & umask like this:

on host 1:

[saml@host1 testdir]$ pwd
/tmp/testdir

[saml@host1 testdir]$ ls -l
total 12
-rw-r--r--  1 saml saml 21 May 19 00:21 file1
-rw-r--r--  1 saml saml 48 May 19 00:21 file2
-rw-r--r--  1 saml saml 28 May 19 00:21 file3

[saml@host1 testdir]$ tar cvf - . | (ssh host2 "umask 0277; cd /tmp/testdir;tar xvf -")
./
./file1
./file2
./file3
./
./file1
./file2
./file3

on host2:

[samr@host2 testdir]$ pwd
/tmp/testdir

[samr@host2 testdir]$ ls -l
total 12
-r-------- 1 samr web 21 May 19 00:21 file1
-r-------- 1 samr web 48 May 19 00:21 file2
-r-------- 1 samr web 28 May 19 00:21 file3

You can drop the -v switches to tar which I've included here merely so that you can see the files being tarred up on host1 and sent through STDOUT (aka. -) and then getting un-tarred on host2.

NOTE: Why this works? Tar's default behavior is to unpack files using a remote user's umask. In the above example I've included the command umask to explicitly set it to something different which demonstrates that the remote tar is changing the permissions on the remote side.

Solution 5

I wrote a small script for the task in Python. You can do python script.py -p o+r some files some/dir/on/the/server/

import subprocess
import sys
from optparse import OptionParser


DEFAULT_SERVER = 'your.server.com'

parser = OptionParser()

parser.add_option("-p", "--permissions", action="store", 
                     type="str", dest="perm", metavar="PERM",
                     help="chmod files to PERM", default=None)
parser.add_option("-s", "--server", action="store", 
                     type="str", dest="serv", metavar="SERVER",
                     help="scp to SERVER", default=DEFAULT_SERVER)

options, args = parser.parse_args()
files = args[:-1]
direct = args[-1]

proc = subprocess.Popen(['scp'] + files + ['%s:%s' % (options.serv, direct)],
                        stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if proc.wait() != 0:
    print >>sys.stderr, "Uploading failed!"
    sys.exit(1)

if options.perm is not None:
    arg_dict = dict(dir=direct, perm=options.perm, files=' '.join(files))
    proc = subprocess.Popen(['ssh', options.serv, 'cd %(dir)s;'
                             'chmod -R %(perm)s %(files)s' % arg_dict],
                            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Share:
98,299

Related videos on Youtube

Florian Mayer
Author by

Florian Mayer

Updated on September 17, 2022

Comments

  • Florian Mayer
    Florian Mayer almost 2 years

    I am uploading files to my shell account using scp. As I need different permissions on the server than on my computer, I'd like to have a way to easily change the permissions upon upload without needing to ssh to the account and change them manually.

  • Florian Mayer
    Florian Mayer over 15 years
    Nope. -p is not what I wanted because I need different permissions on the server(yes it's an UNIX) than on my local machine. I could of course chmod them, use -p and then chmod them back, though I'd need to store the permissions then.
  • Florian Mayer
    Florian Mayer over 15 years
    I thought about that too, what about uploading directories then?
  • tvanfosson
    tvanfosson over 15 years
    I wasn't suggesting that you use -p, just confirming that you were. I'll clarify my post.
  • Florian Mayer
    Florian Mayer over 15 years
    Hm, I could do chmod -R then. Not a bad idea I guess.
  • zigdon
    zigdon over 15 years
    Right. scp -r, then ssh chmod -R
  • tshepang
    tshepang almost 13 years
    Do you mind posting your code at Code Review. There could be nice suggestions there for how it can be improved.
  • Jaime Hablutzel
    Jaime Hablutzel almost 10 years
    As I can see with this command you can only apply fewer permissions to transferred files, as umask only substract permissions, e.g. for a local file with 700 you couldn't get the file with 755 in the destination server, or am I wrong?
  • Jaime Hablutzel
    Jaime Hablutzel almost 10 years
    Furthermore you need --no-same-permissions for the second tar usage if the destination user is root, see superuser.com/a/383801/89031
  • Jaime Hablutzel
    Jaime Hablutzel almost 10 years
    This should be a comment instead of an answer
  • slm
    slm almost 10 years
    @jaime - that is correct, this will only allow you to set the umask for the entire set of files as they're written to the remote server. There is no individual control for different files. I'll often use this since I want to strip relaxed permissions that were OK on my laptop, when copying to a remote deployment, for example.
  • Job AJ
    Job AJ almost 10 years
    Wow, I never heard of the new SO site, Code Review, ty @Tshepang!
  • tshepang
    tshepang almost 10 years
    @AnneTheAgile it's not that new; it's over 3 years old :)
  • soham
    soham over 9 years
    This is not working.
  • soham
    soham over 9 years
    Found out the cause. You have to use --perms as well. explainshell.com/…
  • Mawg says reinstate Monica
    Mawg says reinstate Monica over 7 years
    Could you show the full, valid, command, pelase? Either as comment, or by editting the answer
  • Mawg says reinstate Monica
    Mawg says reinstate Monica over 7 years
    Can someone explain how this would workl, please?
  • Mawg says reinstate Monica
    Mawg says reinstate Monica over 7 years
    Agred (but it did really help ;-)
  • zaTricky
    zaTricky almost 6 years
    This is what I'm trying to get around in one go via script. Rsync isn't always on the destination server and I'd rather that the script I give out doesn't take liberties with apt/yum etc to install rsync. Seems I might have to go the scp+chmod or rm+scp method to ensure permissions are correct. :-/
  • soMuchToLearnAndShare
    soMuchToLearnAndShare about 4 years
    it appears to me (in automated way, not interactive), the host key might change, and i t has failure like this: "Host key verification failed". in ssh one can avoid this by doing "-oStrictHostKeyChecking=no", but in rsync, i do not see one.
  • Ammad
    Ammad over 3 years
    Will this solution work for the directory options recursively as well?
  • Coroos
    Coroos over 3 years
    @soMuchToLean, you just have to use the -e option (rsync -e 'ssh -o StrictHostKeyChecking=no' ... I think).