How to SCP from host to host from a central machine

Tags: , , , , , , ,


Time and time again I run into an instance where I have a file on host 1 and a file on host 2 but host 1 can’t connect directly to host 2. Typically this requires me to copy the file locally to my desktop, then copy it again to the destination host. Thanks to the fact that ssh can tunnel standard input, however, there is a simple way to effectively scp directly from remote host to remote host using your machine as an intermediary. Here’s how.

SCP files using tar and ssh pipes

root@desktop ~ $ ssh host1 'tar -c ./foo' | ssh host2 'tar -x'

As you can see, we are simply redirecting the output of ‘tar -c ./foo’ on host1 to ‘tar -x’ on host2 through ssh. If you want to copy the file to a location other that your home directory on the remote server you can use something similar to the following.

root@desktop ~ $ ssh host1 'tar -c ./foo' | ssh host2 'cd /tmp; tar -x'

All we need to do is execute a cd command prior to our tar command to change the target of the copy.

I hope this helps! If you have a different way to do this leave a commend, I would love to hear it!

2 Responses to “How to SCP from host to host from a central machine”

  1. Matt Reid Says:

    Great post — exactly what I was looking for to save time. I turned it into a quick bash function for my rc file — maybe it will come in handy for you too:

    function scp_tunnel() {
    echo “scp_tunnel :: piped ssh file copy between two servers”
    echo -n “origin server: “; read ORIGINSRV
    echo -n “origin filename: “; read ORIGINFILE
    echo -n “dest server: “; read DESTSRV
    echo -n “dest dir [./]: “; read DESTDIR; if [ “$DESTDIR” = ” ]; then DESTDIR=’./’; fi
    echo “initiating copy, it will finish when it’s finished…”
    echo “ssh ${ORIGINSRV} ‘tar -c ${ORIGINFILE}’ | ssh ${DESTSRV} ‘cd ${DESTDIR}; tar -x'”
    ssh ${ORIGINSRV} “tar -c ${ORIGINFILE}” | ssh ${DESTSRV} “cd ${DESTDIR}; tar -x”
    echo “finished!”
    }

    [Reply]

  2. Cody Says:

    Why do you need to bother with the cd first ?

    Wouldn’t the -C option to tar suffice ?

    It changes to the directory first so e.g.:

    tar xvf file.tar -C /tmp

    (If the -C is formatted to be a double dash then I apologise; can’t recall the html code off hand to do it right).

    will extract the files found in file.tar after changing to /tmp (so whatever was in file.tar will – notwithstanding permissions or some other error – now be in /tmp ). And to those who might think I forgot the dash before the xvf please check the man page for tar (I use the traditional way because that is what I’ve used since a very long time ago).

    [Reply]

Join the Conversation