autossh + screen = fully automatic relogin

When working inside screen over ssh in the Mac OS X Terminal application, it for some reason always hangs after the computer has slept. There’s seems to be no way to resurrect it, other than closing the Terminal and opening a new one, letting the old ssh connection die in loneliness.

However, this problem is avoided by either not using screen (magically, a “normal” ssh connection doesn’t hang the Terminal), or by using autossh to automatically restart a lost connection. I decided to try out the latter option.

I’m daily using my Terminal for both coding and talking to people over IRC. Both of these actions are carried out on a remote server inside a screen session. It’s really annoying when opening my computer after it has slept and I have to start a whole new session starting with opening a new Terminal, ‘ssh’, logging in and reattaching the screen. It’s not much, but it’s enough to annoy you if you have to do it tens of times a day.

Autossh came to my rescue. Autossh is a wrapper around the normal ssh, periodically polling the ssh connection to see if it’s lost. The usage is really simple; instead of saying ‘ssh’ you simply say ‘autossh -M portnr’. The portnumber is used for polling the underlying ssh connection.

So fine, I solved the main problem, the Terminal hanging. But that wasn’t enough. Writing my username and password all the time was really getting to my nerves, so I solved that by using DSA keys for ssh authentication. But even that was not enough. Writing ‘’ as parameter to autossh was annoying, so I set up a config file for ssh, defining a host ‘remote’ that I configured to use my username and the DSA key that I just created.

Ok, I’m getting there. But still I didn’t like the fact that I had to ‘arrow up’ or write ‘screen -dr’ every time autossh reconnected. So I simply added ‘screen -dr’ to my .bash_profile on the remote server. This way, when autossh reconnects, my remote bash automatically reattaches the screen session, and all I have to do when awakening my Mac is to wait for autossh to realize that the connection has been lost. Tick tack, and I’m inside my screen session again.



Filed under All software sucks, Linux, Mac

4 responses to “autossh + screen = fully automatic relogin

  1. Pingback: iTunes everywhere at Some people call me Fredrik

  2. Brilliant! Nice setup, I’m going to copy-cat you.

  3. PS: actually if you reboot, autossh must be started. I suggest Automator and custom workflow.

  4. uksparky

    You can abort a hung ssh session by typing ‘~.’ (tilde, dot without the quotes). That should help you in future from having to close your dead sessions 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s