I'm using a reverse ssh tunnel from home to work, so I can check my home PC every now and then during working hours, all without the need of opening my home router for incoming connections.

Both PC's (home and work) are running Linux (however, different distros).

I'm running the ssh command on my home PC in a loop (I need the connection for VNC access only):

while [ true ]
  ssh -T -N -R 15900:localhost:5900 me@work-PC
  sleep 5m

And on the work PC, I connect the VNC viewer to localhost:15900.

This is my .ssh/config on my home PC:

host work-PC
  ProxyJump employers-Gateway
  HostName work-PC
  User me

host employers-Gateway
  HostName web.company.com
  User me

It works fine as long as the ProxyJump (employers-Gateway) is not rebooted. When I reboot the work-PC, the ssh connection ends (as expected) and a new one is opened after 5 minutes. But when the employers-Gateway is rebooted, the connection just gets stalled. In these cases, I get this message on the terminal:

Warning: remote port forwarding failed for listen port 15900

Is there something I can do to make the ssh connection also close when the ProxyJump becomes unavailable ?


Today, the've rebooted the gateway. Unfortunately, David Dai's solution did not work. This is what I saw in the terminal running the tunnel loop:

Timeout, server web.company.com not responding.
client_loop: send disconnect: Broken pipe
ssh: connect to host employers-Gateway port 22: Connection timed out
kex_exchange_identification: Connection closed by remote host
Warning: remote port forwarding failed for listen port 15900

Could it be that the work-PC still thought it had the old connection open and refused to open the tunnel a second time (from its perspective) ?

So maybe I was looking on the wrong end. Maybe it's the final target (work-PC) that's unaware of the collapsing tunnel, not the originator (home-PC).

Any thoughts about that ?


You can try adding the following parameters to your configuration file:

host work-PC
  ProxyJump employers-Gateway
  HostName work-PC
  User me
  ServerAliveInterval 15
  ServerAliveCountMax 2

ServerAliveInterval 15 tells ssh to request a response from server every 15 seconds.
ServerAliveCountMax 2 tells ssh to exit if it doesn't get a response from server for 2 times.

| improve this answer | |
  • Looks very promising, I just had no opportunity to test it yet ... they managed to achieve a 100% uptime of the gateway since I asked this question, and they don't want to reboot just for my testing purposes. – user9964388 Jan 31 at 23:21
  • Please see my updated question. – user9964388 Feb 17 at 18:46
  • well, on the server side there are 2 corresponding parameters ClientAliveInterval and ClientAliveCountMax. have a try of these. – SparedWhisle Feb 17 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.