I want to copy a file from an ssh session to my local host. The problem is the ssh session isn't accessible from my local host, I need to first connect to an intermediate ssh (ssh1) session, then access to the ssh session in question (ssh2).

So far, I tried to use scp in this way :

scp username@ssh1:username@ssh2:file .

This connects to ssh1, but instead of connecting to ssh2 next, it searches for the file username@ssh2:file. Of course, this works :

1. ssh username@ssh1
2. scp username@ssh2:file .
3. exit 
4. scp username@ssh1:file .

but this is time consuming, especially with entering passwords. Is there a better method?


Yes, there is better method. Use Master session capability of SSH (newer versions). It creates controll session, so you don't have to authenticate for some period of time.

It's described here:


Probably all that you need is to edit/create your .ssh/config. Add there definitions which control the Master sessions:

ControlMaster auto
ControlPath ~/.ssh/cm_socket/%r@%h:%p
ControlPersist 4h
ServerAliveInterval 30

Then you can specify your first hop server definition like:

Host first_hop
Hostname <your first host FQDN or IP>
User <your user>

And the second hop will use your first hop server as proxy:

Host second_hop
Hostname <your second host FQDN or IP>
User <your user>
ProxyCommand ssh -W %h:%p first_hop

Don't forget to create the ~/.ssh/cm_socket directory and config permissions should be 644.

Then you should be able to SSH or SCP directly to/from your second server. There can be more servers chained like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.