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

If I am logged in via SSH on 2 servers and want to copy some files from one to the other (e.g. configuration from an old to a new server), what's the easiest way to do that?

share|improve this question

On server1:

scp <files> server2:/dest/dir/

Or, if you have rsync installed:

rsync -azvu basedir/ server2:destdir/

If not, pipe through tar then:

tar cf -  -C srcdir | ssh server2 tar xf - -C dstdir

The last two copy entire directory trees. The first one or more files in a directory.

In other words, just use shell tools from the first one to copy directly to the second one.

share|improve this answer
    
That would require a login though (which gets harder if it involves keys) and does not allow something like sudo (well, maybe the pipe-tar-through SSH one does?). But it is the most practical way to transfer larger data. – Bart van Heukelom Jun 8 '12 at 10:32
1  
It does allow it. Just put a "sudo" in there. You can set up public key authentication to make it passwordless. – Keith Jun 8 '12 at 10:34

If the total size isn't too big, one can use this little trick.

  1. One the source server, do tar -cz <filestocopy> | base64
  2. Copy the output to clipboard
  3. On the destination server, do base64 -d | tar -xzv
  4. Paste, enter, Ctrl-D

How does this work?

  1. Creates a compressed tar of the files and base64 encodes it so it can be copy-pasted as plain text.
  2. Sets up a command chain that does the reverse; decodes base64 input and extracts the tar.
  3. Paste the data to feed it into that chain, then enter and Ctrl-D to send the end-of-file.
share|improve this answer

With two open ssh sessions the fastest way to copy would be:

  1. Use netcat (nc)

    Sender/Source machine:
    tar -czf - /source/dir | nc -l 2342
    
    Receiver/Destination machine:
    nc -w 10 sourcemachine 2342 | tar -C /target/dir -xz -
    
  2. Use scp with less secure encryption. The default is blowfish usually but arcfour is a much faster alternative.

    scp -c arcfour mogga@flinflon:/home/users/mogga /dev/null
    

I usually use the second option (simply because I can remember it more often).

share|improve this answer
    
Interesting. I'm currently working on EC2 servers where I would have to open up an extra port for that, but I could use that port for other tools as well. – Bart van Heukelom Jun 8 '12 at 12:17
    
Just tested it and it works well. Small error: the -p option (source option) should not be there in the destination command. – Bart van Heukelom Jun 8 '12 at 12:22
    
oops...you're right. -p source_port Specifies the source port nc should use, subject to privilege restrictions and availability. It is an error to use this option in conjunction with the -l option. – mogga Jun 8 '12 at 12:31

You must log in to answer this question.

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