Screen: Tips & Tricks

[ Friday, 13 July 2007, Treadstone ]

In this article I will describe a very useful program: GNU Screen. Usually this program is used by people who have a shell account on a Unix server. But it can be also helpful to people who haven’t yet started to use a terminal or even Linux/Unix at all. Screen — simply — is a program which enables users to create more system shells without the need of logging in multiple times. Moreover it allows to leave programs running after you’ve logged out. What can it be useful for? For example to continue compiling or downloading something when someone wants to use the computer, or to run more than one terminal for one ssh connection. There are many more possible ways of using it…

Author: Łukasz Olender

Throughout the text I will use this C-a for the shortcut of Ctrl+a and C-d for Ctrl+d. I’d like to emphasize that C-a A (Ctrl+a and next A) and C-a a are two different shortcuts :) . I will also refer to the program simply as “Screen”, not GNU Screen, for simplicity reasons.

1. Quick start

To create a new terminal session just enter the command screen. It will grant you the access to, what appears at first look, to be an unchanged shell. But in fact there are many useful features a key sequence away. But let’s start from the begining :)

In the new shell, you can do the same things as in a normal terminal. When you want to shutdown the screen, you press the combination C-d. If you want to leave programs opened in screen running after logout, you have to press C-a d before. Then the text [detached] will appear on the display, what means that screen is running in the background, and you can log out knowing that all your programs are still functioning. When you want to reenter the screen session, type screen -r. You can have more than one screen session, detaching from active ones and creating new ones. But to reattach to any of detached session, you have to know its PID (Process ID), or name. To see running sessions type screen -list. The first column contains the PID of each session. To restore a session type screen -r [PID]. But it is very inconvenient to remember or list these numbers every time you want to reattach. Fortunately you can use the “-S” switch. With its help you can give a name to each session when creating it. To create a session named “wget” you type screen -S wget. This makes it far easier to get back to any session. Just enter screen -r wget and it’s done.

2. Running programs in the background

Sometimes it would be useful to make screen automatically open a program in its session, give it a name and detach returning to a normal shell, in one command. Use the “-md” options to do this. Combining this with the “-S” option gives some interesting effects:

screen -S ekg_session -md /usr/local/bin/ekg &

Screen will create one session, name it “ekg_session”, start the ekg program running inside it and then detach it. Using this method, checking your account on a unix server, you can have either online or “away from the computer” status all the time by one command, while doing other things on the server. Naturally, you can combine many commands into a single command, which makes returning to your work environment easier:

screen -S ekg -md /usr/local/bin/ekg & screen -S mc -md /usr/bin/mc & //
screen -S rtorrent -md /usr/bin/rtorrent &

This makes screen create three differently named sessions in the background with my favourite terminal programs – ekg, mc and rTorrent.

3. Regions and tabs.

Even though running multiple screen sessions is catered to, its creators give you another choice of how to work. You can run multiple shells within one session! What’s more, you can even display these shells in one terminal window.

But so as not to get lost in these windows, I suggest you create tabs similar to those found in web browsers. You can do this by writing to your configuration file ~/.screenrc one of the following codes:

hardstatus alwayslastline      
hardstatus string '%{= mK}%-Lw%{= KW}%50>%n%f* %t%{= mK}
%+Lw%< %{= kG}%-=%D %d %M %Y %c:%s%{-}'


hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}
(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %d/%m 
%{W}%c %{g}]'


hardstatus alwayslastline
hardstatus string '%{gk}[ %{G}%H %{g}][%= %{wk}%?%-Lw%?%{=b kR}
(%{W}%n*%f %t%?(%u)%?%{=b kR})%{= kw}%?%+Lw%?%?%= %{g}][%{Y}%l%{g}]
%{=b C}[ %m/%d %c ]%{W}'

With the first code each tab will have a number, a name plus time and date. The second one will give you tabs that will be created in the center and the active tab will be highlighted in red and also have the hostname displayed. The last code will additionally display the processor usage. You can create your own codes but it is a rather specialized task – I refer you to the system manuals for more information.

You already have tabs, now it’s time to create new terminals.

Open a screen session then press C-a c. A new tab is created. But both tabs have the same title. To change the title of the active tab, press C-a A and enter a new name. Creating tabs without a shell but with a program running automatically is a useful feature. The new tab will inherit the name of the program. To use this option press C-a : and enter the following command screen [program name] (this is the way you enter all commands during the screen session). To go to the next tab press C-a n and to return to the previous one press C-a p. To switch between the tabs use C-a ". This will show you a list of all the tabs.


Regions are a very useful feature. They enable opening many programs and shells in one window of screen. To do it just press C-a S. The window will split into two separate areas. There will be your actual shell at the top part of the window and the bottom part will be empty. To switch to the bottom part press C-a [Tab]. You have to create a new shell in the new window, so press C-a c. You can create more regions by pressing C-a S more than once and creating new shells in them. Tabs display the active region shellname in its titlebar. You can also switch between tabs being in a region, so e.g: you have 4 tabs and 2 regions. Each region can display one of these tabs. A nice trick is that many regions can display the same tab and they will react simultaneously. It is difficult to describe but is very helpful when you work in multiuser mod and you want to see what another person is doing. You can change the size of each region by entering resize [size], where size is the number of lines.

Two windows filling whole the display with screen splitted into two regions
Img. 1 Two windows filling whole the display with screen split into two regions

4. Configuration file

Previously I mentioned about the configuration file. I wrote that it is in a subdirectory in the home directory named ~/.screenrc. But you can have tens of such files, and when you want to use any of them run screen with the option -c [filename]. What these files can be? Simply it can contain any of the commands that you usually enter after pressing C-a :. Below I include one of configuration files which causes splitting a window into two regions and running ekg in the top one.

  • screen ekg — runs ekg
  • title ekg — gives title to the window
  • split — splites window into two regions moving ekg to the top one
  • focus — switches to the bottom region
  • screen — runs default shell, in my case it is bash
  • title bash — sets title to the bottom region
  • focus — switches back to the top one

As you can see, you can automate a lot of actions which you would do after connecting to a server or running a terminal. A full list of commands can be found in the manual.

5. Logging to file.

To log all the data displayed in a screen session or tab into a file, enter the following command:

  • logfile [filename] where filename is the name of the file where all the data will be logged. The default name is screenlog.0 (or 1,2,3… if there are more screen shells).
  • flush [seconds] is used to set delay between each write to the logfile, by default it’s 10 seconds.
  • To start logging enter log on, to stop log off.

6. The password to screen session.

If there is an important program running in a session you can protect it by using a password. To do this enter password. Then you will be prompted to type your password twice.

After that the hashed password will be displayed. Next you have to copy it to the configuration file after the word “password” in the same line. To cancel the password associated with the session just enter password none in the session or delete the line from the configuration file to cancel the password permanently.

7. Screen running right after connecting to a remote host.

In fact this tip has not much in common with screen but still I think it is very useful. If you want to run screen right after connecting to a remote host just enter ssh [username]@[remote host name] -t screen -RD. Unfortunately this command will work only if there is only one screen session running on the remote host. If there is more than one session you have to know its PID or name and use this: ssh [username]@[remote host name] -t screen -R [sessionname/PID]. When you shut down the screen, SSH will automatically log out.

8. Locking the display.

To lock the display use the following command lockscreen or the shortcut C-a x. It doesn’t block the session (like it was in point no. 2), but only “freezes” the display. So detaching and attaching again will cause that you grant the access to session. It can be useful when you are connected to a remote server using the previous tip and you have to go away from the computer. You can’t change the password, because your own user password will be used.


I think these tips will be useful for you. I have been using screen for a long time without knowing about such things as regions, tabs and all features I described here.

Proof-read by trashcat, chaddy

About the Author

Łukasz Olender

New AdTaily ads!

Are you a film buff?

film buffs community, movie recommendations and reviews

RSS: Comments

You can follow the comments to this article through a special channel RSS 2.0 .

Related articles: CLI

 more »

Related articles: HOWTOs

 more »

PolishLinux Top Content

Become our fan on Facebook! on Facebook

Follow PolishLinux on Twitter!

Follow polishlinux on Twitter

Google Ads