Managing Multiple Servers Using GNU Screen Terminal Multiplexer

For those who have tried to manage several terimnal or ssh sessions at once either on Windows using PuTTY or other terminal programs it can be annoying and you can get lost quite quickly.  The one thing you do not want is type the wrong command in the wrong terminal.

Today I hope to introduce you to a tool that have kept me sane for a few years now, when it comes to connecting to multiple servers at once in a single window.  I’m talking about the GNU screen program.

Sample Session

First let’s look at what a screen session might look like:

Example of what a screen session might look like

Here you can see I have 5 different sessions on one screen.

  • To the left editing a WordPress php file
  • To the right there are 4 remote sessions
    • Top 3 are monitoring the uptime of our web cluster
    • Last one monitoring an error log

Installing and configuring

  1. Run: sudo apt-get install screen
  2. Launch it by running: screen
  3. Customize it by editing ~/.screenrc

Here is my ~/.screenrc file and is pretty self explanatory:

#default shell title
shelltitle Laptop

#utf-8 encoding
defutf8 on

# can't remember what this does
termcapinfo xterm ti@:te@

#turn off anoying flashing for visual bell
vbell off

# have the last line show date time and  a list of tabs
hardstatus alwayslastline " %c | %Y-%m-%d |  %w"

# add shortcut keys to resize split windows
bind = resize =
bind + resize +1
bind - resize -1

Running Commands

Inside screen there are several commands you can use to create, switch and manipulate your different screen sessions. All commands by default start with ctrl-a followed by another key.

Some basic commands:

  • ctrl-a c – create a new local terminal window
  • ctrl-a [0-9] – switch to the terminal tab 0-9
  • ctrl-a a – perform a normal ctrl-a key sequence
  • ctrl-a A – rename the tab for the terminal
  • ctrl-a “ – show a list of available sessions
  • ctrl-a S – split screen horizontally
  • ctrl-a | – split screen vertically
  • ctrl-a Q – return to non split view

Openning new tabs:
Other than ctrl-a c to create a new tab you can use the screen command within a screen session to launch a new tab. Here are a few examples.

  • Lauching a reomte ssh in a tab called web00: screen -t web00 ssh
  • Editing a document in a new tab called edit: screen -t edit vim my-file.php

Detaching session for later use

Another cool feature of screen is the ability to detach and save the current terminal sessions for later use. You can detach with the ctrl-a d command.

You can then logout and your session will be available to pickup later.

To reattach a session simply run screen -d -R.

Enough For Now…

There is lots more you can do with screen but this should enough to get you started and get rid a few of those putty windows.

Be Sociable, Share!
  1. Awesome tutorial, i’m going to give this a shot tonight! Thanks man!

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>