Transfer file from remote machine to local machine

Karl Auer kauer at biplane.com.au
Mon Jul 17 06:21:02 UTC 2017


On Mon, 2017-07-17 at 00:15 +0800, Yubin Ruan wrote:
> I got a remote machine A and a remote machine B. Usually I log into
> remote machine A using ssh and then use A as a relay to log into
> remote machine B. But when I am in B, I don't know how to copy file
> directly from B to my local machine.

You need a tunnel!

Assuming that:
a) you can ssh from your system to machine A
b) you can ssh from machine A to machine B
c) you have credentials for machine B

... then you can set up a tunnel.

On your system:

   ssh -L 2222:localhost:2222 youA at machineA

On A:
   ssh -f -N -L 2222:localhost:22 youB at machineB

On your system:

   scp -p 2222 youB at localhost:/path/on/B/file.dat .

That will copy /path/on/B/file.dat to your current working directory on
your local system.

If you have password access on A and B you will be asked for your
password three times - once by A, once by B, then again by B. 

If you have publickey access on all system, make sure you put a copy of
your private key from A for B on your local system[1], and specify it
to scp using the -i option.

If you'll be doing this more than once, wrap it all up in a script. You
may find you have problems with ptys and so on if you script this; if
that happens, check out the -t option to ssh for the first hop.

There is nothing special about "2222" - you can use any free port
number above 1024. Nor does it have to be the same port number on both
A and B - as long as the destination on A is the same as the source on
B.

There *is* something special about the port number 22. If the ssh port
on B is not 22, you should use that other port number instead.

The above commands are off the top of my head. Apologies for typos,
proceed with care and adjust as needed.

Regards, K.

[1] Be careful with private keys! Respect any security policies that
may apply, and store the key at least as securely as you do any other
employer/customer keys. This technique may violate local security
policies on A, B or both; if in any doubt, check with the local
sysadmins BEFORE you try this.

-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Karl Auer (kauer at biplane.com.au)
http://www.biplane.com.au/kauer
http://twitter.com/kauer389

GPG fingerprint: A52E F6B9 708B 51C4 85E6 1634 0571 ADF9 3C1C 6A3A
Old fingerprint: E00D 64ED 9C6A 8605 21E0 0ED0 EE64 2BEE CBCB C38B






More information about the ubuntu-users mailing list