Using Screen for Multiple Terminal Sessions

I still remember where I was when I first saw tabbed browsing. It was in my future brother-in-law’s flat in London, and a friend of his was browsing the web. I noticed that, instead of having multiple Internet Explorer (IE6!) windows open, there was only one window open on a new browser I hadn’t heard of before and he was flicking between tabs within that one window. Immediately I changed my browsing habits and became a Firefox enthusiast die-hard enthusiast for many years to come.

We are often in a similar situation when it comes to terminal sessions on our Linux systems. You are working away on something, and want to open a second terminal to do another job at the same time. PuTTY doesn’t support tabs, so that means opening another PuTTY window and another SSH session to your server. It is inconvenient and an inefficient use of system resources.

Screen is a great command line application in Linux which solves this problem. With screen, you can start a session and then can have as many ‘windows’ within that session as you want. The great benefit is that you can ‘detach’, that is close down and walk away from a session, and then open it up again much later and it will remember where you left off.

Imagine starting some task which will take multiple hours, like a detailed network scan. Rather than leaving your PuTTY session open, you can start it within a screen session, then detach and shut down your actual PuTTY SSH connection. The next day, you can log back onto the server again, resume the screen session and pick right up where you left off. Similarly, it might be useful to have htop running in one terminal window while you work away or run some tests in another.

Starting a New Session

To start a new screen session, simply type screen. The start page contains details about the program and licensing, etc. Simply press Enter again to move pass this and into the first window.

Once in screen, there is nothing to particularly tell you that anything has changed – you are facing a command prompt waiting your first instruction. You can use this as normal, to change directories and list directory contents, i.e. all the normal things you would do on the server. The real beauty comes in when you want to open a second window.

Using Multiple Windows

Screen commands all begin with Ctrl + a (lowercase a). Press Ctrl + a, and then c (Ctrl + a, c). This will create a new window, with a new command prompt. To get back to your previous window, press Ctrl + a, p. You now have two command line windows to work with. To get back to your previous window, press Ctrl + a, p. Press Ctrl + a, w to see how many windows you have and which one you are on at the current moment (indicated by the *). You can also jump straight to a window by using its number, i.e. Ctrl + a, 2.

Exiting screen

To exit a screen session, you can either close all windows and end your work, or leave your session running and just exit out of the session. To close any windows you are working on, press Ctrl + a, k. You will be asked if you want to kill the window [y/n]; click y to confirm.

To exit out of screen but leave your session running, press Ctrl + a, d, to detach. Once back at your normal command prompt you can re-enter your session by typing screen -r.

It is possible also to have multiple screen sessions running. Each session is given a random number, so to see the active screen sessions type screen -ls (from outside a screen session). You can re-enter a specific session by typing screen –r and then the session number. You can type just the first few numbers of the session number, ‘846’ for the first example below, and screen will know which session you want to re-enter, i.e. screen -r 846

One other thing to take note of: Ctrl + a is obviously an important key sequence in screen. In normal usage though, Ctrl + a is also the bash shell command to move back to the start of the line you are on. To move to the start of the line within screen, press Ctrl + a, a.

Below are some of the most common commands in working with screen. It is a very useful utility for doing multiple command line tasks and all of the other common linux commands should work while you are within a screen session.

screen Start screen
screen -ls Show active screen sessions
screen -r Resume a screen session. If there is only one, it will resume that one. If there are multiple, enter the session number after -r
Ctrl + a, ? Help (display a list of commands)

Ctrl + a, c Create a new window (shell)
Ctrl + a, d Detach (close out) but keep the session active

Ctrl + a, w List all windows (the current window is marked with "*")
Ctrl + a, n Go to the next window
Ctrl + a, p Go to the previous window
Ctrl + a, 0-9 Jump straight to a numbered window
Ctrl + a, Ctrl + a Toggle back and forth between two windows

Ctrl + a, k Kill the current window
Ctrl + a, Ctrl + \ Quit screen – kill all windows

Leave a comment