Multiple SSH Tunnels

This week I needed to set up connections to 4 different Windows servers, each hosted at the same hosting provider. They will be running different components of one application, so I’ll probably be accessing all of them at once. Simple enough, except that in order to connect to the servers I need to access the hardware VPN that is located in our office on the other side of the country!

I knew that I needed to set up a SSH tunnel from my laptop (a MacBook) to the servers, but I certainly did not want to open 4 different shells each time I wanted to connect to all the servers. Each of the 4 tunnels needs to pass through the same server located within the firewall at our office.

Thankfully, the ssh command allows you to specify multiple tunnels through the same server in one command. The command to do this is: ssh -L :: -L :: @ So, say that I wanted to set up local ports 10001 and 10002 to connect to a couple of servers at and tunnel through

he command would then look like: ssh -L -L Hope this helps someone else out!

  • TheWilson

    Hi Steven. This did help out someone :)

    Also this is the first result for the search :
    ssh forward multiple ports

  • dergachev

    Excellent, thank you Steven! And on top of everything, I’m using this with Vagrant and Drupal. :)

  • Wycito

    Awesome, so used to use GSTM that neglected to use the terminal for such things, so I find myself is someone else’s linux box and you came to the rescue :), thank you

  • Pizzicato

    Thanks mate!

  • Dave Lahr

    Thank you! Very helpful.

  • Jim

    nicely done – thank you

  • Coop1979

    This might be an old article, but it is still helping people. I use tunnels on a regular basis, but never realized you could do more than one in a single command.

    Thanks to this article I have now set up a nice little script that checks if I’m connected via LAN or WLAN, which LAN or WLAN I’m connected to, checks if I’m connected to the appropriate VPNs for that particular LAN or WLAN, and then sets up the tunnels to my most used destinations, or tells me to do it manually if the network I’m connected to is not on my trusted whitelist.

    Thanks for the inspiration :)