0

I run this ssh tunneling script on my remote server (CentOS) in a terminal to get to my home computer (Ubuntu):

while true
do
  ssh -R 49666:localhost:22 (my name@ my home ip) -N
  sleep 15
done

I have it loop (the sleep 15 part) because tunnels collapse occaisonally and when it does, somebody needs to physically be there to start it again. The loop is a workaround, although maybe not a good one...

Then from home, I can ssh to the server by ssh'ing to localhost on port 49666.

Everyone once in a while, though, I randomly just get disconnected for EXACTLY two hours and 15 seconds. I see this in my auth.log:

Accepted publickey from (work ip)
error: bind: Address already in use
error: channel_setup_fwd_listener_tcpip: cannot listen to port: 49666

I was originally thinking that I shouldn't run the SSH -R in a loop, because it'll obviously keep trying to bind to the same port. But the thing is, the above failure happens maybe every 14 hours, not every 15 seconds, as the loop would imply. I suspect that if you try to SSH -R a connection that already exists, it just does nothing?

My next thought was that maybe some other service pops up on port 49666 sometimes and messes up the tunnel, and then when the script tries to bind to it 15 seconds later, it can't because the other service is using it. But I can't figure out what the other service, if it exists, could be. I use netstat, lsof, etc but I never see anything running on that port. I'm not sure if there is a way to see a port's history, or if you can only see things using it while they are active. But I suspect that nothing is trying to use it, but it's possible...

After I get the "already in use" failure, I can't ssh. But then, every time, EXACTLY two hours and 15 seconds later, I get this:

Accepted publickey from (same work ip)

but without the subsequent failures, and I can ssh as normal again. The amount of time until the next failure seems random, but from the failure until the next re-establishment of the tunnel, it's EXACTLY two hours and 15 seconds. I assume there is some kind of 2 hours cooldown firewall thing, and then the script says to wait 15 seconds, so that adds up to the time. The pattern here makes me think there is some way to config that out. The problem is I have no idea why the "address already in use" thing happens to begin with, and I don't know what side (client or remote server) the two-hour cooldown is happening on. I have pretty much all the default settings on my home computer. I don't have sudo at work (but I can get a sudo to check something for me sometimes) so I don't know what their sshd_config is like, but I imagine there is nothing weird.

Can somebody please help me identify: 1: Why the "address already in use" failure happens and what to do about it? and 2: Why the exact two hour and 15 second gap and what to do about it?

3
  • My guess is that your ISP or router is closing connections after exactly two hours. Wouldn't be surprised. Have you tried testing other connections? Dec 23, 2016 at 11:07
  • Only LAN ones between my own computers, which seem to work in perpetuity. But those are regular ssh's, not tunnels.
    – iammax
    Dec 23, 2016 at 13:02
  • The address already in use is an indication of that the server still thinks the connection is alive, but the client tries to reconnect because its thinks the connection is dead, sounds like a bad router
    – Ferrybig
    Nov 25, 2017 at 13:36

1 Answer 1

0

Use autossh. It will periodically check the tunnel and once it breaks, it reconnects automatically:

autossh -M 2000 -R 49666:localhost:22 (my name@ my home ip) -N

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .