Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ssh port forwarding (tunneling) in linux

I have a specific scenario that I want to solve. I currently connect to a host via port forwarding:

 laptop -> gateway -> remote_server_1

and another host:

 laptop -> remote_server_2

with passwordless login working on both. Neither of the remote servers are visible to the outside world. Now I'm running a service on remote_server_2, that I'd like to be able to access on remote_server_1. I presume I have to setup reverse port forwarding from remote_server_1 to my laptop, and then on to remote_server_2, but I'm not sure how to do this. Anyone come across this situation before?

Edit: The full solution in case anyone else needs it:

mylaptop$ ssh -L 3001:localhost:3000 server_2
server_2$ netcat -l 3000

Then setup the tunnel via gateway to server_1:

ssh -t -t -L 3003:server_1:22 gateway

Then access it from server_1:

ssh -R 3002:localhost:3001 -p3003 localhost
echo "bar" | nc localhost 3002`

and hey presto server_2 shows bar :-)

like image 225
tdc Avatar asked Nov 10 '11 11:11

tdc


1 Answers

You have to do exactly as you've described. Setup the server on server_2.

mylaptop$ ssh -L 3001:localhost:3000 server_2
server_2$ netcat -l 3000

Then access to it from server_1.

mylaptop$ ssh -R 3002:localhost:3001 server_1
server_1$ echo "foo" | netcat localhost 3002

server_2 will show foo.

like image 136
Didier Trosset Avatar answered Sep 22 '22 07:09

Didier Trosset