I am aware of questions already asked for a tunnel through one gateway server. However couldn't find anything on multiple gateway servers.

How do I configure a ~/.ssh/config file which will allow me to tunnel through two gateway servers:

My machine -> Gateway1 -> Gateway2 -> TestServer

I couldn't find any description or articles about this. Hopefully, someone here would help me solve this. Thanks.


Just add a configuration stanza for each hop after the first to your ~/.ssh/config:

Host testserver
    ProxyCommand ssh -q gateway2 nc -q0 %h 22

Host gateway2
    ProxyCommand ssh -q gateway1 nc -q0 %h 22

Note that you'll probably have to manually connect to each hop once and accept its hostkey (if you haven't done so already).

You might also want to add Hostname fields with the hosts' FQDNs to the SSH config to make sure the names are resolved correctly on each hop.

  • Thanks, it was a painfully obvious solution in the end. :)
    – n3rV3
    Jul 9 '15 at 8:11

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.