Get the highlights in your inbox every week.
An introduction to GNU Screen
An introduction to GNU Screen
Learn how to use this helpful terminal multiplexer tool to better control your command line.
GNU Screen or tmux?
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.
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.
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 |.
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.
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.)