ssh port forwarding (tunneling) in linux
Asked Answered
B

1

7

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 :-)

Belldas answered 10/11, 2011 at 11:43 Comment(0)
D
4

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.

Device answered 10/11, 2011 at 12:3 Comment(3)
This nearly works, except I have to tunnel through to server_1 as well. So if I do: mylaptop$ ssh -L 3003:gateway:3002 server_1 and mylaptop$ ssh -R 3003:localhost:3000 -p3003 localhost am I on the right lines? Helpfully server_1 doesn't have netcat on it (I don't have admin rights on that one)Belldas
Sorry just realised netcat is nc on there. Anyway it's not quite working yet.Belldas
I'll give you the accepted answer, as it gave me enough clues to go on - full answer in my edit.Belldas

© 2022 - 2024 — McMap. All rights reserved.