SSH tunnel between three machines

I encoutered a scenario that is : A can access B via SSH (B can't access A, because nat), A can access C via SSH ( C can't access A, because nat), B and C can't access each other, there are in different network.

A->B, A->C, C->B? my quesiton is how to set the tunnel or command to let C can access B?


To make an ssh tunnel from B to C, run these commands on host A:

ssh -n -R 3300:localhost:3300 B sleep 999999999 &
ssh -n -L 3300:localhost:22 C sleep 999999999 &

replacing B and C with the appropriate hostnames.

Now on B you can

ssh -p 3300 localhost

and connect to host C. When you want to tear down the tunnel, on host A bring the ssh commands out of the background and type Ctrl-C.


A closely related variation on this question is the case where neither B nor C can be accessed, but both can access A. The goal is to allow C to access B.

i.e. B -> A, C -> A, C -> B?

From B

ssh -n A -R 9007:localhost:22 sleep 999999999 &

From C

ssh -n A -L 9008:localhost:9007 sleep 999999999 &

From C

ssh -p 9008 localhost