An introduction to GNU Screen

Learn how to use this helpful terminal multiplexer tool to better control your command line.
591 readers like this.
An introduction to GNU Screen

Opensource.com

GNU Screen (or just Screen) is a useful and versatile, but somewhat under-appreciated utility that, as part of the GNU project, is included or available in nearly every Linux or UNIX distribution. Screen is a terminal multiplexer, similar to what you would find in a window manager. It is an especially helpful utility if you are restricted to working on a computer or a virtual machine either over an SSH link or through a terminal session on the console. Two of the features that I find most useful are the ability to open multiple windows within the screen session, and the ability to disconnect and reconnect Screen without losing the current session.

Screen is started simply with the screen command.

Screen basics

First, a little bit about how to control Screen. Screen works with a control sequence, Ctrl+a. All characters typed in a screen session are sent directly to the shell, with the exception of Ctrl+a. Ctrl+a followed by a character is how you send command sequences to screen. For example Ctrl+a ? brings up the help page with the default key bindings.

Screen allows you to create multiple windows within your single terminal session. Typing Ctrl+a c starts a new window within Screen. This acts like an independent window with its own shell. Ctrl+a K will kill the current window.

Ctrl+a n and Ctrl+a p will move to the next and previous screens respectively. Alternately, Ctrl+a # switches between screen sessions by number, in the order that they were started in. There is also a command that displays a list of all the current windows, and allows for selection with the cursor keys, Ctrl+a ".

One of the most powerful features of Screen is the ability to disconnect and reconnect your screen sessions. This can be most useful when you are on an unreliable connection, or if you know you'll be moving from one network to another, or one computer to another. Ctrl+a d disconnects your screen session. You can then log out of your SSH session. When you want to continue, log in again and start screen with the -r (for resume) option.

Your screen session will resume from where you left off, with all processes running as you left them. If your session were to disconnect abruptly, you can similarly reconnect with the same command. If the host has not acknowledged that your session has disconnected, you can use screen -r -d for resume and disconnect to allow screen to disconnect the session, and reconnect. This can be a lifesaver if your network connection is unreliable.

Installing Screen

Screen is installed by default on some distributions, such as SuSE and Slackware. On all of the Linux distributions that I tried it on, I was able to use the standard package manager to install screen from the default repositories. For example, on Red Hat Enterprise Linux or Fedora, I was able to use yum or dnf, respectively, to install. Under Debian and derivatives like Ubuntu, you can do the same with apt-get. I was able to install screen using ports on FreeBSD as well.

More advanced Screen features

Screen sessions can be split. A horizontal split is done with Ctrl+a S. This will split your current window in half horizontally. Switching between the regions is done with Ctrl+a Tab. The other window switching commands work within a split screen. A split window can be eliminated with Ctrl+a X. In more recent versions of screen, a vertical split can be created with Ctrl+a |.

advanced screen features

Log files of current screen sessions can be started with the Ctrl+a H command, which will make a file called screenlog.X where X is the number of your screen session. A screenshot of what is currently in your screen window can be invoked with Ctrl+a h, creating a file called hardcopy.X. A screen session can be locked with the Ctrl+a x command which will lock the screen with the current user's password.

Why GNU Screen?

I am frequently asked why to use Screen instead of something newer like tmux? My answer is simple: GNU Screen is easy to use, and ubiquitous. It was already installed on the servers that I work on, and does the jobs I need perfectly. If there was a deficiency that needed to be addressed, I'd quickly look for a better tool, but Screen does the job I need it to. Hopefully, you'll find GNU Screen as useful as I do, and take advantage of this frequently overlooked utility.

For more on GNU Screen, read the man page or check out this quick reference.

Bob Murphy will be giving a talk at SCALE 15x this year entitled: A brief introduction to GNU screen. (Or: how to make the command line work for you, especially when the network is conspiring to stop you.)

User profile image.
Bob Murphy is a Linux systems administrator, long-time desktop GNU/Linux user and computer enthusiast who is passionate about Free Software and helping people better use technology.

8 Comments

Excellent article, Bob! Screen is one of my favorite tools. It makes my tasks as Sysadmin much easier and gives me a lot of flexibility. There is always more to learn about all aspects of GNU/Linux, and your article did teach me something new. I have been using screen for years and never knew it could do split screens.

Thanks for an interesting and informative article!

David: Thanks for the feedback, I'm glad I could bring you a tip that you can use with screen!

CTRL-A is used in emacs to move the cursor to the beginning of the line. It can also be used on the bash command line if emacs-style line editing is turned on with set -o emacs. It's use is so automatic and thoroughly ingrained that 'screen' would seem to be almost worthless to emacs users.

But fortunately, we can add a line to the screen configuration file: ~/.screenrc:
escape ``

That is, the keyword "escape" followed by two backticks. That way, we use the backtick (the unshifted tilde key) instead of CTRL-A. That could interfere with shell programming, but it's more readable to use "$()" instead of backticks anyway. And if you really want to type a backtick, just hit the key twice.

I remap ctrl-a to ctrl-J, as so far I've not found any shell that utilizes ctrl-J for anything else.

GNU screen is great. One of those applications I can't live without. Great article!

Great article! A few years ago, I wrote one as well that was originally published on the defunct Red Hat Magazine, and then re-published on the also defunct Linux Gazette. It is still relevant and a good complement to yours: http://linuxgazette.net/168/silva.html

Screen is definitely my friend (curiously enough, I've never used tmux). I know of no better tool for keeping interactive terminal sessions (which I use frequently) alive even after logging out or losing a remote connection.

Re the CTRL-A issue: It probably helps that I almost always use Emacs as a GUI; in terminal mode, I normally use vi.

Dmitri: Terminix (and many other Linux terminal emulators like terminator or terminiology) are good, but personally, I'm in an environment where I don't have that luxury, and screen is a great tool to use when the options of installing things on the endpoint are limited.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.