I have 2 servers as [email protected] and [email protected], I want to connect to [email protected] but I cannot connect to it directly due to restrictions, I have to first connect to [email protected] and then I have to connect to example2.com from example1.com.
Now in my local I want to connect my application to remote psql server which is on [email protected]:5432. For this I have to do following steps.
SSH into example1.com
ssh [email protected]
Now from example1, After ssh, go inside example1.com and then forward port 5432 from example2 to 5434 of example1 by entering following in example1.com:
ssh -L 5434:localhost:5432 [email protected]
Now from my local, in another terminal tab, I have to type following command to forward port 5434 of example1.com (which is coming from 5432 of example2) to my local 5432.
ssh -L 5432:localhost:5434 [email protected]
Now finally in my local, my application can connect to localhost with port 5432, as if psql is running locally.
I want to make this whole into a single command so that I can do it in just one step and also stop it in one step, rather than doing the above 3 steps because again and again I have to type so much, I am willing to create a executable file (like a shell script) as well.
So far I tried doing this:
ssh -L 5432:localhost:5434 [email protected] ssh -L 5434:localhost:5542 -N [email protected]
This is exactly the solution I want, it works like a charm, IT WORKS, but , there is a BUT now. And that but is, when I do CTRL + C, it kills my local ssh connection for 5434 of example1 to my local 5432, but it does not kill the connection between example2.com and example1.com , due to this when I run the above command again it gives me this error:
bind: Address already in use
channel_setup_fwd_listener_tcpip: cannot listen to port: 5434
And then I have to change port again, and then again it hangs there, and when I do CTRL+C again it would block that port,
So what I need?
So I need another way of port forwarding or I need a way to kill the connection between example1.com and example2.com, I just need one step (single line) to start and 1 step to stop (single line).
So far I have been killing these connections by doing ps -aux | grep 5434
from inside of example1.com and the killing it using kill command.
My local SSH config (~/.ssh/config)
Host *.*.*.*
StrictHostKeyChecking no
Host *
ServerAliveInterval 50
ServerAliveCountMax 10
ForwardAgent yes
Edit #1:
ssh -J [email protected] ssh -L 5432:localhost:5432 -N [email protected]
Tried the above command but I am getting this error:
open failed: administratively prohibited: open failed
stdio forwarding failed
ssh_exchange_identification: Connection closed by remote host
Probably this has something to do with example2 being a private IP, only example1 is a public IP (not sure if this is the problem).
example2
being a private IP, onlyexample1
is a public IP" – Irrelevant. The mechanism of-J
resolves the target host on the last (in your case the only) jump host. The target host may not be reachable from your local computer, it doesn't matter.