2

I have some files on a server that I'd like to copy on my local machine. The problem is that I can't directly connect to the remote machine, so I need to connect to an intermediate server first, then I can connect to the destination server via the intermediate one. Is there any way to copy the files directly from the destination server to the local machine and somehow bypass the intermediate server?

Thanks

3
  • How are you connecting to the intermediate? If you are using ssh you can use something called port forwarding to copy the files. It'll look like you are copying directly from the remote but you'll actually be tunneling through the intermediate. The direct path is probably not possible.
    – B Layer
    Oct 16, 2017 at 10:11
  • @BLayer Thanks a lot for the reply. Yes, I am using ssh to connect to the intermediate server.
    – Eman
    Oct 16, 2017 at 10:17
  • It worked? I see you accepted. That was fast. :)
    – B Layer
    Oct 16, 2017 at 10:21

2 Answers 2

2

Going the direct route is probably not possible. But if you are able to log into the intermediate with ssh and then from there log into the remote with ssh then you can use something called local port forwarding to copy the files on your local machine with scp. It'll look like you are copying directly from the remote but you'll actually be tunneling through the intermediate.

Try the following on two separate terminals (command lines, shells, etc.)

Terminal 1:

# set up 'local port forwarding'
ssh -v -T -N -L 2222:remote:22 username@intermediate

Terminal 2:

# do your scp command(s) like this example
scp -P 2222 localhost:/path/to/file .

Substitute appropriate hostnames or IP addresses for remote and intermediate.

In the scp command /path/to/file is the path on the remote server.

3
  • I wish I could up vote twice, Dec 17, 2018 at 15:12
  • In this line scp -P 2222 localhost:/path/to/file . you may need to add the user to log into the remote server. scp -P 2222 remote_user@localhost:/path/to/remote/file .. If you don't, the user from the local machine will be used.
    – Gustavo
    Mar 18, 2021 at 17:49
  • @Gustavo that's true (though probably a far less common scenario). I'm wondering if that would work if you were using public key authentication as opposed to regular password auth? Perhaps with -i privkeyfile but I haven't tried it and don't have an environment right now to test it.
    – B Layer
    Mar 19, 2021 at 3:26
2

You don't need port forwarding, you can do it with ProxyCommand. Add something like this to ~/.ssh/config:

Host some_name
    Hostname        internal.example.com
    User            internal_user
    ProxyCommand    ssh -A -q -l %r -W %h:%p external.example.com

This will allow you to ssh to the internal machine with ssh some_name, use scp, etc.

You must log in to answer this question.

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