This question already has an answer here:

I have an mysql Server on hostD. I can access hostD through hostC, hostC through hostB, hostB through hostA and I access hostA using putty on my home device

home(windows)-->hostA-->hostB-->hostC--hostD(mySQL Server)

My goal is to access the server from my home device.

Another challenge is that I don't have any root access on A,B and C. Now I've been experimenting with the '.ssh/config' file and this is what I got so far:

 Host BTunnel
  HostName hostB
  LocalForward 3306 localhost:3306

 Host CTunnel
  HostName hostC
  LocalForward 3306 localhost:3306

 Host DTunnel
  HostName hostD
  LocalForward 3306 localhost:3306

This way I can connect from Putty--> hostA and type in consecutively:

ssh BTunnel
ssh CTunnel
ssh DTunnel

To get a tunnel from A to D. But I would like to have a solution, where I only type in 1 command from host A to get a Tunnel to D, maybe by chaining up the existing host aliases.

Can anyone offer a solution?

marked as duplicate by karel, Pimp Juice IT, music2myear, Kamil Maciorowski, G-Man Oct 28 '17 at 7:49

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


Well, you can just chaining the commands. You can do this even without any config files. Execute at hostA:

ssh -L 3306:localhost:3306 hostB ssh -L 3306:localhost:3306 hostC ssh -NL 3306:localhost:3306 hostD

If you have configs and public keys in place, you can do:

ssh BTunnel ssh CTunnel ssh -N DTunnel

(I would include the -N at the last command because I don't need shell access, just the forwarding.) You can make an alias for it in .bashrc (or similar):

alias mytunnel="ssh BTunnel ssh CTunnel ssh -N DTunnel"

you can start with mytunnel now.

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