GNU Screen – a primer

GNU Screen is a little-used but very powerful utility, allowing you to do more from a Linux console, whether from a “real” console or logged in to another machine via SSH.

It is “a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells)“.

This is a simple guide to getting started with GNU Screen.

So what can Screen do for you? Well, you can:

  • Have multiple “windows” in one console, which you can switch between;
  • split the console into areas, to display more than one of these windows at once;
  • “Detach” from your screen session, leaving all the applications etc running as they are, and re-attach later;
  • Share a screen session with another user – perhaps to walk somebody through something, letting them watch what you do.

OK, how about an example? Let’s say you’re compiling a kernel on a (slow) remote machine over an SSH connection. You know that, if you close your xterm (or terminal emulator of choice) or loose your connection to the remote host, the compilation will be interupted.

If you started screen before starting to compile, you could just detach from the screen session, leaving the compile running. Later on, you can re-attach to the screen session and see how things are going.

OK, let’s start using it. Often screen is installed by default – if not, you’ll have to get it and install it – that’s out of the scope of this primer, but most distros will probably have a package available for easy installation using your normal method.

Type ‘screen’ to get started. If this is the first time you’ve used screen, you’ll get a welcome screen – press Enter when you’ve read it.

You’ll be dropped back to a shell – but this shell is running inside screen.

Do something in that shell – perhaps just run ls for example.

Now, let’s create a second window. Press Ctrl+a, then “c”. You’ve now got two windows open in this screen session (although you’re only seeing one at a time).

At this point, let’s cover screen key commands – Ctrl+a is the default key combination to get screen’s attention, to tell it you’re about to give it a command. Once you’ve typed Ctrl+a, you’ll type a letter or number, to tell screen to do something.

The most important command of all is Ctrl+a, “?” (a question mark), which will show you a help window:

GNU Screen key bindings help screen

Switching windows

To switch between windows, you can use Ctrl+a, “p” to go to the previous window, or Ctrl+a, “n” to go to the next window.

If you know the number of the window you want to go to, use Ctrl+a, <num> – e.g. for the first window, use Ctrl+a, 0 (zero), for the second use Ctrl+a, 1 and so on.

You can name windows for easy reference with Ctrl+a,A – you’ll be prompted to enter the name for the window.

You can list all open windows with Ctrl+a, ” (a double-quote, so shift+2), which will provide a window list like:

GNU screen window list screen

You can type the number of a window to go straight to it, or use the Up and Down arrow keys to scroll up and down the list, and Enter to select.

Splitting the screen

Another powerful feature is the ability to split the screen into regions, allowing you to see more than one window at a time (handy for monitoring an IRC channel whilst writing an email or doing something else perhaps).

To split the screen, use Ctrl+a, S. To switch focus between the different windows (or regions) use Ctrl+a, Tab.

For each region, you can select any of the current windows to display in it just as described before (if you really want, you can have the same window open in both windows). The screens are displayed one above the other:

GNU Screen split-screen mode

You can also resize a region with Ctrl+a, :resize <num of lines>.

Detaching/re-attaching

To detach from your session, use Ctrl+a, d. To start screen and re-attach to an already running session, use screen -r. To get a list of current sessions, use screen -list.

Hopefully this primer has been enough to show you the power of GNU Screen – there’s a lot more to be learnt about it, which the documentation, key bindings screen, or a quick Google will tell you.

If you have any hints or tips for Screen, feel free to post them in the comments.

2 thoughts on “GNU Screen – a primer”

  1. Hiya, it’s been a long time since I attended a HertsLUG meeting (Nov 2004 I think) as my 2 year visit to the UK has long ended (as has the purely educational sampling of many many beers). I still keep an eye on the list tho. Thanks for the pointers, I’m installing it on Ubuntu now (which, by the way can be done with a ‘sudo apt-get install screen’). Cheers, Bob.

Comments are closed.