Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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'm a bit new to screen and autossh:

I'm simply looking to "fully automate" my ssh sessions, make them implicitly persistent, and have them automatically restore should my connection drop.

I've tried using screen directly, but find some of its keystrokes (e.g. ctrl+a) interfere with my shell - i.e. they override shell/program functionality using the same keystroke. Is there a way around this?

In truth, I don't really even want to be aware that screen is running at all. I simply want to "ssh " from a terminal window, and have that connection persist should my network die, laptop go into sleep mode, etc.

Is my requirement naive? Am I oversimplifying?

share|improve this question
possible duplicate of How to keep persistant SSH sessions? – Dennis Williamson Jul 30 '10 at 8:08

I use the following setup:

I have an alias that connects using SSH to my server of choice, starting/reconnecting screen as needed. I also remap the Ctrl-A shortcut to Ctrl-Z because I use that one much less often and it's easy to type.

alias myscreen='TERM=xterm ssh myserver -a -x -t /opt/local/bin/screen -xRR -A -e^Zz -U -O'

I can then open a terminal and type myscreen, and I will connect to myserver. I can even open multiple terminals, type myscreen multiple times and get multiple terminals connected to the same screen. I use this often, showing one screen terminal in one window and another in the other.

If you want to reconnect automatically you can do things like

$ while :; do myscreen; done

or even

$ while :; do while ! ping -c1 -t1 myserver >/dev/null; do sleep 10; done; myscreen; echo Connection dropped at $(date); sleep 5; done

Note that all the above is on OS X connecting to Solaris so there may be subtle differences in the syntax of ping and the location of screen.

One more thing: You can customize screen quite a bit, I'm especially fond of these bits of my .screenrc (on the remote side):

# A nice status line at the bottom
hardstatus alwayslastline "%-w%{= BW}%50>%n %t%{-}%+w %<%=|%h"

# special xterm hardstatus: use the window title.
termcapinfo xterm 'hs:ts=\E]2;:fs=\007:ds=\E]2;screen\007'

# Do not use xterm's alternative window buffer, it breaks scrollback
termcapinfo xterm*|xs ti=\E7\E[?47l
share|improve this answer
Actually I never heard of autossh up till now, so perhaps that makes that second command line a whole lot simpler, just replace ssh with autossh in the myscreen alias – w00t Jul 30 '10 at 8:17

Not sure what autossh does. I use a bash function to start ssh with screen:

ga ()   {
if [[ ${host} == "" ]]
    echo "Provide hostname no to connect to."
    screen -S ${host} -t ${host} ssh -q -X -l ahumane ${host}

It sets the name of the screen to the ssh host so you can see it in "screen -ls"

share|improve this answer

Your Answer


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