I have 2 virtual machines. The 1st virtual machine is where the source files are stored. The 2nd virtual machine is where my bash script is and it should copy files from source to my 2nd virtual machine.

How can I do this in Bash?

Is it possible to SCP files from 1 virtual machine to a 2nd virtual machine in Bash, when my script is running on the 3rd machine? If so, please show me how to do this.

  • A quick read of the scp man page would tell you that only one of the locations can be remote. There are several obvious workarounds. Where's your research? – Phil Frost Jan 3 '13 at 21:31
  • 1
    @Phil: scp [-12346BCEpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-S program] [[user@]host1:]file1 ... [[user@]host2:]file2 I beg to differ. – Johnsyweb Jan 3 '13 at 22:04

It is possible (as long as both hosts are listening on SSH and the first remote host can see the second)! Why don't you give it a try?

scp remote1:/path/to/file remote2:/path/to

You may need to supply usernames and or passwords for both of these hosts (if your username varies from machine to machine).

If you use agent-based authentication with agent-forwarding enabled, you shouldn't need to provide any passwords (this is my prefered method of working).

Note that this has nothing to do with Bash (or even virtual machines) and should work in any shell.

  • 1
    Good explanation -- wish I could +1 for agent forwarding as well – MattPark Jan 3 '13 at 22:48

Usual way to do this is to ssh into one of the remote machines and issue the scp from there.

#3rd-vm> ssh 2nd-vm
#2nd-vm> scp file 1st-vm:~/

If you need to bridge it through the 3rd machine, you could do it with ssh like this:

ssh remote1 cat file | ssh remote2 'cat > file'

Or with tar:

ssh remote1 tar cf - path/to/copy | ssh remote2 tar xf - -C path/to/dest

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.