So, here's the problem. I have a file on a remote machine available only to root, and a private-key-only no-root SSH access(I do have sudo though). I need to copy that file to the local machine, and make it as easy to use as possible (an alias or a shell script), because this will be used multiple times by different people.

Here's what I was thinking: 1. SSH to the remote machine and copy the file to some location where I have permissions 2. From the local machine, do rsync --remove-source-files for the file I just copied.

But I don't see any way to do it with just one command. Is there?

  • It doesn't matter how many commands you need. If you can make it work with any number of commands, you write them in a new file that will become the script to run to do it. May 10, 2016 at 6:41
  • Well, yeah, but If the first command is "ssh whatever", and the the second is "cp whatever", will "cp" be executed on the remote server?
    – T. Spikes
    May 10, 2016 at 7:02
  • Then the solution lies on the remote machine. I don't understand why you need to automate such a weird intrusion into your own system but since you do it, why not simply change the permissions on the remote server or create a hard link with proper permissions? May 10, 2016 at 7:28

2 Answers 2


I think I can improve on the answer given by EightBitTony.

ssh -t user@target 'sudo cat /source/file' > output && chmod +x output

When more than one file is involved, I find pax much easier to use than rsync, not matter how often rsync is "simplified" for me.

ssh -t user@target 'cd /src && pax -w ./' | pax -r 



ssh -t user@target machine sudo cp /source/file /target/file

The -t creates a pseudo-tty, so that sudo can ask for a password. The ssh command takes an optional command (sudo cp .... in this case) which it executes on the target machine and then disconnects.

You will get prompted for the sudo password (i.e. user@target's password).

You can then run the rsync. You can script this on the starting machine with just the two lines in a single script.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .