Saturday 5 August 2006This is 17 years old. Be careful.

At work, my ssh session to our servers time out quite frequently. It used to bother me a lot because it meant having to login to the machines many times a day, and losing the on-screen history of what I had been doing.

Not anymore. A co-worker introduced me to screen, a terminal multiplexor. In a nutshell, it manages virtual terminals that persist even when you log out. So if a connection to a server is dropped, logging in again will re-connect you to the session you had been working in.

Screen manages many virtual terminals, so one OS window can contain many Linux sessions within it, reducing desktop clutter.

Screen is quite complex, in the emacs mold: it can do many things, and has many commands, some of which are bound to keystrokes, but many of which are not. There are esoteric escape sequences that can be used to hyper-customize the environment, and more options than I can possibly understand. If you find yourself using many console windows on Unix boxes, do yourself a favor and learn enough about screen to get it working for you.

The documentation out there tends to be a bit overwhelming as well, falling into two camps: the exhaustive reference manual that begins with the command-line switches, or the informal tutorial that starts out talking about stuff you don’t understand. Here’s a shorter getting-started guide that might be helpful.

I’m tempted to write a mini-tutorial myself, but I don’t think I could really improve on what’s out there. I will give you my .screenrc file:

# Ned's .screenrc
escape ^L^L
vbell off
defscrollback 8000
hardstatus alwayslastline
hardstatus string '%{= KW}%-w%{= gW}%50> %n %t %{= KW}%+w%< %{= kG}%-=%{-}'

What does this do?

  • Sets the escape character to ^L, because I don’t need that for any real use, and the default ^A is too handy for command-line editing.
  • Turns off the visible bell, which is too much flashing for me.
  • Sets a scrollback buffer of 8000 lines, because I like to look back through my console window for old stuff. One thing about screen: it manages the console window such that your real console window never scrolls, so you have to change where you store your off-screen lines. Your true console window doesn’t need them anymore; “defscrollback 8000” configures screen to keep them for you.
  • Sets the hard status line to be the last line in the window, and to give it a simple tabbed look. This makes the last line in the window a list of your current screen sessions, in a nice green-on-gray theme.

Screen is a god-send.


I remember the first time I found out about Screen, I was so excited. I could finally start log into my Linux box from my laptop between classes, start a Gentoo emerge, and then close the laptop when I had to go to class.

My laptop's network connection would close, but the screen session on the Linux box would keep compiling.
"screen -x" lets you have multiple simultaneous connections to the same session; that's useful for pair programming and similar situations.

One "problem" with screen -- whereas before it was annoying when I lost the past few hour's worth of session history, now on those occasions when machines have to be rebooted, I lose _weeks_ worth of session history, and that's _really_ painful.
If you're on Windows, the 'putty' ssh client has an option to send keep-alives at a configurable interval. Very handy for preventing lost connections.
I think screen is one of the most underappreciated UNIX tools out there... I think I wrote about it a few months back myself, but I've been using it since a friend showed it to me in 1991 (!!).

I use backquote (`) instead of a control character, incidentally.

I once went through the whole alphabet and discovered that the only key I do not actually use is Ctrl+B. Thus my .screenrc has 'escapre ^bb'.

I also find xterm's (well, gnome-terminal's actually) scrollback easier to use (mouse wheel), so I the followng line to .screenrc to have both:

termcapinfo xterm|xterms|xs ti=\E7\E[?47l
you may also be interested in autossh(and especially the rscreen script that comes with it) it automatically reconnects times out ssh sessions and reopens your screen sessions. Incredibly useful.
I love "screen" too, except for two things:
-it's damn hard to find about: unless you have a co-worker that introduces you to that tool, there is little chance that you'll stumble on it
-for some reason, it's rather hard to explain. You did a pretty good job though. Here's my own attempt as well: Essential utility: "screen"
I have the same problem with frequently-disconnected SSH sessions. this is useful:
Thanks for the tip about the PuTTY keep-alives. I hadn't seen the option before, and it seems to really help!
I agree. Screen is one of the best *nix tools in existence. Also, like putty, the OpenSSH client on *nix systems allows you to configure a keepalive. I used to get very frustrated at my numerous ssh sessions getting killed as I ran to lunch or to get some coffee. So, if you're using OS X like me (or any other *nix), just put these 2 lines in your ~/.ssh/config file:

ServerAliveInterval 300
ServerAliveCountMax 3

That'll keep you logged in all day. :)
I was going too just post a comment along the lines of "Yeah, I love screen, too!", but after reading the comments first I have to thank Marius for that screenrc line. I was getting so frustrated with not being able to use the scrollbar since I started using screen regularly.
screen is great, but there does not seem to be an equivalent for the AS/400. I once set up a linux box so that AS/400 developers could telnet to that, then open up a screen session containing a telnet connection to the AS/400 so that when the connection dropped they could log back in without losing work/corrupting databases.
I've known of screen for a long time but never bothered using it until someone pointed out it's serial port support a few years back:

screen /dev/ttyS0 9600

I never ran minicom again.

Something I do use, however, is NX. When I'm working on a server, I can have up to 10 shells open simultaneously and it's pretty annoying to have to reconnect them all when I move my laptop between work to home. Now I just start an NX session and run konsole (great for multiple shells if you know the keystrokes) and maybe a web browser. Before I move to another location I suspend the session so that when I reconnect, everything is where I left it. If I want to work on a server without a graphical environment, I use a NX connection to one that does, and ssh from there.

As for the Putty disconnection problem, I've heard the complaint from many Windows users. Supposedly, there's some timeout value in the registry that you can increase, but I haven't used Windows seriously in over 10 years so I would have no idea how to find it.

Add a comment:

Ignore this:
Leave this empty:
Name is required. Either email or web are required. Email won't be displayed and I won't spam you. Your web site won't be indexed by search engines.
Don't put anything here:
Leave this empty:
Comment text is Markdown.