Even in times when administrator desktops include powerful processors and large monitors with great graphical interfaces, servers in datacenters and such still need to be managed through a command line. Upgrades, new installations, program development, and monitoring – many things often happen all at once. Admins sometimes have multiple terminal sessions open to access a multitude of servers. With many open windows, you risk losing track of things.
Terminal multiplexers like Tmux and Screen provide some order to this potential chaos. They allow, among other things, multiple terminal windows in one session or parking and recalling sessions without interrupting any of the running processes.
This approach can also help when network connections go down. Admins can also share sessions with their colleagues, which can often help solve tricky problems more quickly.
In this article, I'll focus on the veteran multiplexer GNU Screen [1]. Screen was developed in 1987 by Oliver Laumann at the Berlin Technical University under the name BSD Screen and published in the net.sources user group. Juergen Weigert and Michael Schroeder of the University of Erlangen took over development in the years between 1993 and 2002. The baton passed to Wayne Davison in 2004 before the project sank into a multi-year hibernation with very minor changes made. At the beginning of 2014, however, the current maintainer Amadeusz Slawinski published a new version of Screen [2].
Screen provides much more than just continuous running and recall of processes in a terminal window and collaborative access to them. Multiple terminal windows can be opened, keystrokes logged, and content shared during a screen session. If a longer running process exists in a terminal, Screen can provide an alert when changes occur on the command line. Screen "windows" can also be split, so that you can view multiple sessions simultaneously.
After installing the terminal multiplexer, you start it by typing the screen command. Depending on the distribution, Screen loads an info screen or drops you into a terminal window. In the latter case, you enter commands as usual – so far, so good. The only advantage up to now is that you can recall the session in case of a connection failure. However, Screen can do a lot more.
When working with Screen, you get around by using certain commands and keyboard shortcuts. Screen uses the key combination Ctrl+A as a control signal for further commands. Entering Ctrl+A and then pressing C, for example, opens a new window. Ctrl+A and Shift+2 lists all the windows and allows a change among them (Figure 1).
Alternatively, you can use the screen number from the list and enter Ctrl+A and then <number>, where <number> is from 0 through 9. Using Ctrl+A and ? shows an overview of the key combinations. Ctrl+A and then K closes the active window. Closing all of them shuts down the Screen session. In the unusual case where you need more than 10 terminal windows, you can use Ctrl+A and then Shift+#+<number> to switch to a window with a double-digit number. However, this action requires compiling Screen with a higher MAXWIN number. Alternatively, you can move forward and backward through windows with Ctrl+A and N and Ctrl+A and P, respectively. Use Ctrl+A and then W to find out what window you're currently in.
If you're skipping between multiple Screen windows, you'll soon lose track, because the Bash shell names every window unhelpfully as bash . You also can't tell where a process is running, although you might recall what's behind each window number.
To solve this, you can give each window a different name with Ctrl+A and then Shift+A. Then, you can tell whether an update is running in window 7 or window 8, and that the config file with Vim is being processed in window 5. If you mistakenly use a command in a window, you can cancel it with Alt+Q.
Murphy's Law dictates that you'll underestimate the process duration just when you're about to leave for the day. Such processes include a full network scan or a complete LaTeX installation. If you've started such a process with all foresight in a Screen session, you can decouple from the session with Ctrl+A and then press D. Later, you can log in to the server again over SSH and open the session with
screen -r
If you're running multiple sessions, Screen will show you a list of running sessions with the associated PID. Use the Screen PID to open the screen you want to see. Alternatively, use the -S <name> parameter when opening the Screen session. Switch between sessions with Ctrl+A and Shift+. to the Screen command line and name the session with sessionname <name> .
You can also have the Screen inform you about the status of a running process. If you're downloading large files, migrating servers or compiling large programs, attach a screen with Ctrl+A and Shift+M, and use Ctrl+A and Shift+- to give you a quick status when Screen detects activity or a recently closed job in a window.
Alternatively, you can divide the Screen window into multiple regions to maintain better oversight of running processes (Figure 2). Older Screen versions allow only a horizontal division, which you activate using Ctrl+A and then Shift+S. The newest version also allows vertical division, and you can open a two windows side by side with Ctrl+A and then Alt+<. Screen calls the subwindows "regions," and you use Ctrl+A and Tab to switch from one to the other. Close the currently active region using Ctrl+A and then Shift+X.
During breaks or interruptions, you can lock the session using Ctrl+A and X. Note especially that a locked session doesn't prevent another user from hacking in from somewhere else. The session is still connected, and another user can run screen -r -D to get to it, whereas -D means detach. An unlocked session can be grabbed in an instant.
Some tasks need to be well documented. Admins often copy commands and other output to a text file. Screen makes this easier in that the program can log all input and output if you want. You activate logging with Ctrl+A and then Shift+H. Afterwards, you can find the log in the screenlog.# , where the hash is the number of the terminal window in the Screen session (Figure 3).
Screen also lets you copy text into a window. Use Ctrl+A and then Alt+8 or Ctrl+A and Esc to enable copy mode, then use the arrow keys or H, J, K, or L in Vim mode to move through the text. From the start position, use the space bar to mark the content to copy (Figure 4). Another press of the space bar copies everything into the text buffer, which you then copy using Ctrl+A and then 9.
An interesting Screen feature is multiuser mode, which can be helpful if someone is stuck with a problem and needs some help on the command line. Screen can be enabled for multiuser mode from the beginning of a session by including the -m parameter with the start command, or during a running Screen session you can use Ctrl+A and then Shift+. to open a screen command line. Use
:multiuser on
to activate multiuser mode. You then determine which user can participate with Ctrl+A and Shift+. and the instruction
:acladd username
By default, the added user can do anything, but this can easily be changed using the following:
:aclchg username permbits list
which can also take multiple usernames in the syntax. The permbits are rwx for reading, writing, and executing, as you are probably familiar with from the Bash chmod command. You set the bits with pluses and minuses for "allow" and "deny," respectively. The list is the window or windows the user can access. To give user Fred only read privileges in window 1, for example, you would type
:aclchg fred -w 1
replacing the list with "#" provides access to all session windows.
With all the privileges set, users log in with their accounts and enter the session for the original user (e.g., otto ) with
$ screen -x otto
With multiple sessions, the PID or name of the session needs to be entered after the slash. To get the PID, use
$ screen -ls user
See Figure 5. You may also need to set the suid bit for Screen and make /var/run/screen readable for multiuser mode to work properly. You can do this on the command line with
$ chmod u+s /usr/bin/screen
or
chmod 755 /var/run/screen
When the Screen guests are through, you can use acldel username to remove their privileges. Turning off multiuser mode isn't enough, however: Anyone logged in to the session can stay with it until removed.
Screen doesn't just act like a flexible terminal multiplexer; it can also act like a serial communication program. You can therefore get the output from an Arduino, for example, or any other device with a serial console, even if you can't normally start it up from Screen.
The default Screen setting for transmission is 115200 bps, 8 bits, no-stop bit, and deactivated flow control. Apart from the baud rate, this default setting should suffice for most devices. For example, to connect to a striking GuruPlug, use the following (Figure 6):
$ screen screen /dev/ttyUSB0
You can use:
$ screen /dev/ttyUSB0 9600
to get the serial output from an Arduino with a standard 9600 bps setting.
Screen can also be controlled by scripts, which means that commands can be sent to separate sessions at Screen startup or later at the command level. For example, using
$ screen -X stuff "ls -lh\r
would run the ls -lh command in window 0 of the session. Screen needs you to specify the final carriage return (\r ) to execute the command. If you want to control another window, use the -p option followed by the name or number of the window.
You can also pass Screen commands to open sessions. The command
$ screen -p 2 -X kill
would shut window 2 of the Screen session, or
$ screen -X multiuser on
would make a session multiuser. With multiple Screen sessions running, you can specify which session you want to send a command to with the -S <pid> parameter.
For admins who use the command line often, using Screen can make work easier in many ways. Of course, this applies to other terminal multiplexers as well, but Screen, unlike Tmux, still basically uses the key bindings that were once common used in many serial console programs.
Screen has a huge range of functions, although some are rarely used in practice. In any case, a glance at the documentation helps you see what's available and what might come in handy later on.
Installation
GNU Screen is usually quick to install. On Debian and derivatives such as Ubuntu or Linux Mint, you can install the multiplexer with
$ sudo apt-get install screen
Users of Arch Linux can do this with
# pacman -S screen
To install the newest version [3], however, you can't avoid using the classic ./configure; make; sudo make install trio.
Infos