Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

migrated from Jan 3 '13 at 21:59

This question came from our site for professional and enthusiast programmers.

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
@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
up vote 3 down vote accepted

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.

share|improve this answer
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:~/
share|improve this answer

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
share|improve this answer

You must log in to answer this question.

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