Screen

Saturday 5 August 2006

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.

tagged: » 13 reactions

Comments

[gravatar]
Jay P. 8:39 PM on 5 Aug 2006

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.

[gravatar]
Todd Larason 10:46 PM on 5 Aug 2006

"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.

[gravatar]
jws 11:25 PM on 5 Aug 2006

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.

[gravatar]
Titus 11:51 PM on 5 Aug 2006

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.

--titus

[gravatar]
Marius Gedminas 5:35 AM on 6 Aug 2006

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

[gravatar]
le 11:58 AM on 6 Aug 2006

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.

[gravatar]
Julien Couvreur 1:30 PM on 6 Aug 2006

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"

[gravatar]
Justin Mason 7:32 PM on 6 Aug 2006

I have the same problem with frequently-disconnected SSH sessions. this is useful:

http://taint.org/wk/RemoteLoginAutoScreen

[gravatar]
Ned Batchelder 11:55 AM on 7 Aug 2006

Thanks for the tip about the PuTTY keep-alives. I hadn't seen the option before, and it seems to really help!

[gravatar]
Nathan 5:09 PM on 8 Aug 2006

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. :)

[gravatar]
Calvin Spealman 3:43 PM on 9 Aug 2006

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.

[gravatar]
Alan Bell 7:04 AM on 11 Aug 2006

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.

[gravatar]
James Oakley 10:57 PM on 11 Aug 2006

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:

name
email
Ignore this:
not displayed and no spam.
Leave this empty:
www
not searched.
 
Name and either email or www are required.
Don't put anything here:
Leave this empty:
URLs auto-link and some tags are allowed: <a><b><i><p><br><pre>.