screen on ubuntu

Screen is a window manager for Linux. The power of Screen is described well in this article:

The same way tabbed browsing revolutionized the web experience, GNU Screen can do the same for your experience in the command line. GNU Screen allows you to manage several interactive shell instances within the same “window.” By using different keyboard shortcuts, you are able to shuffle through the shell instances, access any of them directly, create new ones, kill old ones, attach and detach existing ones.

Instead of opening up several terminal instances on your desktop or using those ugly GNOME/KDE-based tabs, Screen can do it better and simpler. Not only that, with GNU Screen, you can share sessions with others and detach/attach terminal sessions. It is a great tool for people who have to share working environments between work and home. By adding a status bar to your screen environment, you are able to name your shell instances on the fly or via a configuration file called .screenrc that can be created on the user’s home directory.

If you are a Linux user, chances are you have been using additional Putty windows if you needed simultaneous access to more than one secure shell on the same host. Maybe you are using the IRSSI IRC client in one window, and a regular bash shell in the other window. With screen you can use one window for everything, and even display multiple terminals at the same time:

So let’s get started setting up screen on Ubuntu. It is not a trivial job, but well worth the effort.

Many Ubuntu versions include Screen out of the box, but if it is not already installed you can use:

apt-get install screen

To start using Screen open a terminal and type

screen

Depending on the setup of Screen you may be greeted by a startup message. Press space or enter to proceed, and a window with a bash shell opens. This shell is running in an emulated terminal – managed by Screen – but you can use it like any normal bash shell. For now, type exit to close the shell. You will return to the previous bash shell, and a message is displayed:

[screen is terminating]

Screen closes because the last terminal inside the session was terminated.

The next sections introduce the most important operations in screen, including their keyboard shortcuts.

A Screen session starts off with a single emulated terminal, but it becomes increasingly more useful when we add more terminals. Use

Control+a c

Note that the manual writes the shortcuts using a different syntax: C-a c. They mean the same – pressing Control+a, then pressing c.

Switch to the next terminal in the Screen session:

Control+a n

Switch to the previous terminal in the Screen session:

Control+a n

Emulated terminals keep running when the physical terminal (i.e. Putty) is closed. This means you can do some work inside a Screen terminal, disconnect from the server, and come back the next day to find the terminal exactly as you left it.

Open a Screen session using screen, and do some work in the shell. To detach your open Screen session just type:

Control+a d

or simply close it by closing the terminal window. After typing Control+a d you are dropped back into the original shell, and a [detached] message is displayed.

Screen keeps track of all sessions, to view them use the command screen -ls:

$ screen -ls
There is a screen on:
2073.pts-0.myhostname   (07/20/2011 02:58:06 PM)        (Detached)
1 Socket in /var/run/screen/S-arto.

To reopen the Screen session you need to reattach with the screen -r command:

$ screen -r

Presto, you are back in the Screen session and can continue using the shell.

A Screen session can be exited by terminating every process running inside it. A faster way to do the same thing is to use Screen’s quit command:

Control+a :quit

So first press Control+a, then a semicolon, then type ‘quit’.

This will kill all programs running inside the current Screen session and exit the session. You will get [screen is terminating] as a confirmation that your Screen session has ended.

You can scroll up and down in the current terminal by pressing:

Control+a [

This will enter Copy Mode, and allow you to scroll through using PageUp/PageDown. Press Escape when you are done.

Screen keeps its configuration file in the same location that many applications do: in a dot file in your user’s home directory. This file is named .screenrc. Useful configuration options in ~/.screenrc include:

  • Adding a ‘tab bar’, which shows the current terminal and all available ones.
  • Setting up default terminals that are automatically created on startup.
  • Creating custom keyboard shortcuts, for example to switch the active window.

My personal .screenrc file contains:

# Tab bar
caption always '%{= dg} %H %{G}| %{B}%l %{G}|%=%?%{d}%-w%?%{r}(%{d}%n %t%? {%u} %?%{r})%{d}%?%+w%?%=%{G}| %{B}%M %d %c:%s '

# Default screens
screen -t irssi 0 irssi
screen -t bash 1

#Keyboard shortcuts
bindkey ^[[D prev
bindkey ^[[C next

And it looks like this:

The active window can be switched using Ctrl+Left and Ctrl+Right.

The caption line adds a status bar with the hostname, system load, open terminals and date/time.

Each screen -t command results in the automatic creation of a terminal. It uses the following syntax:

screen -t NameOfScreen ScreenNumber ShellCommand

If you want more information on advanced features such as using split-screen: check out the manual.

How to use Screen on Ubuntu

Leave a Reply

Your email address will not be published. Required fields are marked *