Skip to content
Advertisement

SSH Tunnel to a machine on another network [closed]

I’m trying to tunnel to a machine on a private network. I can ssh to one of the machines on the private network. My network is 10.4.0.x and the private network is 192.168.1.x.

I can ssh to the tunnel machine and then ssh to the destination machine from there without problems.

But I just want to tunnel from my machine directly to the destination. I am using this command but it is hanging. How do I specify the username of the machine I’m trying to access?

ssh -N -L localhost:22:DESTINATION_MACHINE:22 BRIDGE_MACHINE_USER@BRIDGE_MACHINE

ssh -N -L localhost:22:192.168.1.4:22 bridge@10.4.0.41

Advertisement

Answer

You can do it in two steps. The first command below sets up the port forwarding. The second command establishes an SSH session with the final destination machine.

First command:

ssh -N -L localhost:3306:destination:22 bridge-user@bridge-machine &
  • -N: don’t create an interactive login to bridge-machine, just connect and do the port forwarding.
  • -L localhost:3306:destination:22. Arrange that any network request received on port 22 on the local machine is forwarded, via this ssh session, and re-issued on bridge. Since bridge can route to destination, the re-issued request will work. We choose 3306 because we need a port number that is not presently being used. We use 22 as the destination because that is the well-known port for SSH.
  • bridge-user@bridge-machine – Since this SSH session terminates at bridge, we need bridge credentials.
  • & – run in the background so that we can issue the second command.

Second command:

ssh destination-user@localhost -p 3306
  • destination-user – Since the end-point of this connection is destination, we need credentials from there.

  • @localhost – We are actually connecting to the forward service established in step 1.

  • -p 3306 – The command from step 1 arranged that any requests received on localhost:3306 will be re-issued from bridge to destination:22. So we treat localhost:3306 as a local alias for destination:22.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement