30

I'm using Ccygwin on WinXP (with the bash shell). I want to SCP a file from my localhost to a remote machine -- host2. However, I can only SSH to an intermediate machine -- host1, and then from there SSH to host2. (Note, I ccan't access host2 from my localhost).

I thought tunneling was my answer, but when I try to set up a tunnel

ssh -L 9999:localhost:9998 dalvarado@host1 'ssh -L 9998:localhost:1234 -N dalvarado@host2'

But after typing this command and hitting enter, the system just hangs. What is the proper way to setup a tunnel and then SCP a file after?

Thanks, -

3

6 Answers 6

35

Since OpenSSH 7.3, you can use -J or -o ProxyJump to specify the bastion/jump host. Therefore, to SSH to node2 via node1:

ssh -J you@node1 you@node2

SCP doesn't have the -J argument, but it does allow -o, so this works:

scp -o ProxyJump=you@node1 file.txt you@node2:~
1
22

This has already been answered best here.

To summarize: put the following in ~/.ssh/config

Host target.machine
User          targetuser
HostName      target.machine
ProxyCommand  ssh [email protected] nc %h %p 2> /dev/null

and then simply scp to target.machine any time you want to proxy via proxy.machine!

Also works for ssh, so will save you time ssh-ing to the target machine too.

Credit should go to user24925 who answered this in 2011.

15

To set up a SSH tunnel, use the following format:

ssh -L 9999:host2:22 user@host1

This command connects to host1 as user and tunnels port 9999 on the computer issuing the command to port 22 on host2. -N is optional, or you can use something like top or watch to keep the session alive if needed.

Then, simply scp to host2 on localhost:9999.

3
  • 1
    When I run this command, am I supposed to end up getting logged in to host1? Also, after running this command, I opened another bash shell, and ran "scp hello.txt localhost:9999", but got a "ssh: connect to host localhost port 22: Connection refused" error. What am I doing wrong here?
    – Dave
    Commented Aug 2, 2012 at 13:41
  • 3
    When you run this command, you will be connected to host1, yes. Your scp command syntax is incorrect though. Try this scp -P 9999 hello.txt user@localhost:/path/to/destination/file where user is the user on host2 that you want login as.
    – Rain
    Commented Aug 2, 2012 at 21:04
  • 1
    @Rain, you could put this example in the main answer ;)
    – dmeu
    Commented Oct 26, 2015 at 9:17
3

You could first scp the file to host1, like this:

scp file dalvarado@host1:.

Then do this to get it to host2:

ssh -t dalvarado@host1 'scp file dalvarado@host2:.'

The -t option to ssh forces it to allocate a pseudo-terminal, which may make it easier for scp on host1 to prompt you for a passphrase/password. If you have ssh-agent running and configured everywhere, you shouldn't be prompted for a passphrase/password.

I offer this alternative, because if you used a tunnel, you'd still need two commands: one to setup the tunnel and one to copy the file through it. This seems simpler.

1
  • Fantastic solution!!!
    – Riccardo
    Commented Apr 17, 2016 at 20:54
1

Now you can send your file over ssh tunnel with -J option

scp -J proxy-host your-file destination-host:[directory]

If you need to specify port on the proxy host you have to use -P option

scp -P port-number -J proxy-host your-file destination-host:[directory]

Finally if you need to send files from subdirectories you can use -r option

scp -r -P port-number -J proxy-host your-file destination-host:[directory]

The order in which you specify the options matters!

0

To copy from remote C, through jump box B to your local host A

scp -J username@B username@C:/file/location /file/path/on/local

You must log in to answer this question.

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