Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I use ssh -L -g, but intermittently the connection is lost, thus:

$ ssh -L 8080:localhost:8080 -g
Last login: Tue Apr 24 07:19:04 2012 from
Read from remote host Connection timed out
Connection to closed.

Can anyone suggest a robust way to keep these connections alive, and reconnect it they are closed.

share|improve this question

You could try Mosh.

share|improve this answer
Damn, that looks awesome, I work on the train every day and I can't believe I never thought to look for a general solution to the problem. As you can probably see, I actually found autossh which appears to solve the specific problem I have, but I'll definitely check Mosh out. – dukedave Apr 25 '12 at 2:47
mosh doesn't handle port forwarding – Elazar Leibovich Jan 25 at 6:14
up vote 3 down vote accepted

autossh appears to fulfill my requirement, from the readme:

autossh is a program to start a copy of ssh and monitor it, 
restarting it as necessary should it die or stop passing traffic.

Substituting the arguments given in the question:

autossh -M 0 -L 8080:localhost:8080 -g

(the -M 0 is required to turn the monitoring loop off)

I was able to kill -9 the connection from and see the connection re-established automatically by autossh:

local$ autossh -M 0 -L 8080:localhost:8080 -g
Last login: Wed Apr 25 03:23:56 2012 from$

Execute kill -9 on PID of connection on

local$ Connection to closed.
Last login: Wed Apr 25 03:34:01 2012 from
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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