SCP establishes a new connection when copying files, which sometimes can make it hard if there are firewalls involved.

For example,

Host-A <--> Host-B <--> Host-C <--> Host-D

  • Host-A is connected to Host-B via SSH, and then Host-B is connected to Host-C via SSH, and then to Host-D.
  • Host-A cannot connect to other computers other than Host-B, Host-B can only connect to Host-A and Host-C, Host-C can only connect to Host-B and Host-D, and last Host-D can only connect to Host-C.

The command (on Host-D) scp /home/user/file Host-A:/home/user/newfile will attempt to create a new SSH tunnel between the two, which is not what i want. I want the existing SSH tunnel chain to copy the file back through the previous hosts until it reaches Host-A.

How can i do this?


You can re-use existing connections. Either use command-line switch -M, or (client) config option ControlMaster. See this answer on StackOverflow for details.

I've used this extensively, combined with ControlPersist to to close the connection only after a delay. This avoids the connection time you would otherwise incur, and drastically improves performance when you're closing and re-connecting multiple times (as I often do during interactive use).

Do note that it does not work very well for connecting to some hosts — GitHub, for example — so you may need to explicitly exclude certain hosts from using this option, if you set it up as your default.

  • 1
    Oh, and if the connection hangs, as it does occasionally, you can simply delete the ControlMaster file for the connection, and re-connect. – jpaugh Feb 1 '18 at 21:23

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.