April 20, 2016
by Rares Aioanei
If you’re new to server administration and command-line, perhaps you haven’t heard of terminal multiplexers or what they do. You want to learn how to be a good Linux sysadmin and how to use the tools of the trade. Or perhaps you’re a seasoned administrator already and administer quite a few machines, and want to make your life a little easier. Or maybe you’re somewhere in between.
Either way, this article will explain what terminal multiplexers are, what they do and most importantly, how you can benefit from using them.
A terminal multiplexer is nothing more than a program that allows its user to multiplex one or more virtual sessions, so the user can have several sessions inside one single terminal. One of the most useful features of such programs is the fact that users can attach and detach such sessions; how is this useful will become clear shortly.
Let’s say you have to administer a remote server via ssh/command-line but your connection is not very stable. That means you have to reconnect often and don’t want to start work all over again. Terminal multiplexers offer the feature of saving your sessions between connections so you can continue just where you started.
Please note that such sessions are not persistent between reboots (in our case above, reboots of the server you’re connecting to) so it’s best to know this in order not to expect such a feature. The reason for this is the fact that the multiplexer runs shell sessions, from which you may be running a text editor, a monitoring tool and whatnot. Since all those processes will not be there anymore after a reboot, there is no reason why this feature should be implemented as it would not have any real use.
We spoke in our introduction about attaching and detaching : this is exactly what this feature does. Continuing with our use case, where you have an unstable connection, once you get disconnected, you can just ssh into the server again and reattach to the running session (or choose between sessions to reattach to) and you’ll be right where you
More than one window
If you’re used to tiling window managers – like dwm, XMonad or i3 – you can think of terminal multiplexers as the terminal equivalent of such WMs.
Some more than others, they offer you multiple windows, the ability to easily switch between them, change layouts or even resize the windows. Perhaps you want to use a graphical environment only when needed and spent the rest of the computer time in a console, or perhaps you don’t have any choice, as working on a server usually means
command-line only so, like we said, anything that makes your life easier as a CLI user is more than welcome.
Another advantage is resource management – if you’re working on a constrained machine, having only a terminal open a multiplexer in it is easier on CPU/RAM consumption rather than multiple windows or tabs. Or perhaps, depending on what you need, you can replace the graphical interface altogether.
Sessions can not only be attached and reattached, as described above, but they can also be shared. In practical terms, it means more than one user can attach to an existing session and work together as they see fit.
Terminal multiplexer list
What follows is a list of terminal multiplexers and everything you need to get started, including installation, basic usage, features and, of course, some comparison between them so you can decide easier what fits you and your needs best.
If you’re just getting started, perhaps it’d be a good idea to try them all, as it’s not a long list, and see for yourself what is it that is best for you. We will provide screenshots for the impatient anyway, but it is our opinion that nothing is better when it comes to deciding what to use than direct usage, so if you have the time, try them. As usual, we will not try to replace the manual pages of any of the programs presented below, so for a complete reference, please use ‘man $program’ to get the complete information.
One of the most popular (if not the leader, actually) terminal multiplexing software on Unix-like systems is GNU screen – henceworth named as simply ‘screen’ for the sake of brevity. screen offers a large enough plethora of features to make it have a stable and devout group of users. As the name implies, screen is released under the GPLv3 and is associated with the GNU project as a whole. One concept that is essential to the everyday use of screen (and other similar software) is the command shortcut. A command shortcut is a keybinding, that’s customizable via a configuration file or by other means (like source code, for example), which is followed by a keystroke that allows the user to send a command to the multiplexer.
Let’s take a simple example : perhaps the most used command in software if this sort is the one that creates a new window. So what we’ll have to do is press the command shortcut followed by a key that tells screen to create a new window for us. The default command shortcut in screen is Ctrl+A (for the rest of this article we will use the C-a notation), and the command that follows is ‘c’, which stands for create. But let’s start with the beginning and let’s see how to install screen. In Debian/Ubuntu and other Debian-based Linux distributions the command to install is
$ sudo apt-get install screen
On Fedora and Redhat-based distributions you can install screen by issuing the following linux command :
$ sudo yum install screen
Regarding Fedora, on latest versions of it you might need to replace yum by dnf, as it’s become the default command-line package management tool. But the command above should work, because it will warn you that yum is deprecated and redirect you to dnf. Given the fact that GNU screen has a long history behind it (the first release announcement was made on net.sources in 1987), it’s become pretty ubiquitous on almost any OS that has something to do with Unix.
If you’re inside X, open a terminal window (or use a TTY), and type ‘screen’. You will perhaps notice that your terminal type has changed to ‘screen’. This can be verified by typing
$ echo $TERM
after starting screen.
Now that you’re started with screen, type the command to create a new window and see what happens (C-a + c). If you’re expecting some graphical sign showing that now you have two windows, you will be a bit disappointed. You will be taken to a fresh window but that’s it. So now we’ll have to see how to navigate between windows. If you know what window you want to go to – especially useful when you only have a few open – you can use C-a followed by a ‘ character. You will be prompted to enter the window identifier and, upon entering it, you will be taken to said window. Window switching can also be done more straightforward with C-a + $identifier, where $identifier is, at least for now, the number of the window, or, even simpler, by using C-a + Tab (just like Alt+Tab in many window managers). To go to the last window used just type C-a twice. If you, for example, need to follow the output of one console while working on something else, you have the split command – C-a + S (note the capital s in there).
A feature we’ve been talking about earlier is the one that allows you to detach/attach sessions at will. As an example, perhaps you want to close the client terminal for, say, a reboot and then be able to get back to your remote session. Use C-a + C-d to do the detaching part, and when coming back just add -S as an argument to screen, following the session name. To get the list of attached displays, use C-a, followed by * (asterisk).
Other features include copying and pasting (C-a + [ for copy and C-a + ] for paste), naming windows (C-a + A) or window info (namely you can display some information about the current window in the message line – use C-a + C-i for this). Finally, killing a window is done by using C-a + k.
A few notes that are worth remembering : First, what you read above only scratches the surface; screen is a capable program and we’ve selected only what we considered to be most important for a user to get started. Please refer to the documentation, online or the manual page, for the complete set of features. Second, if you’re using bash and/or
emacs, you might want to change the default command shortcut (C-a), because it will sooner or later interfere with the respective programs shortcuts. How to change the command shortcut is left as an exercise to the reader.
Probably the most popular alternative to GNU screen is tmux. Unlike it, it is BSD-licensed and is even part of the default install of some BSD operating systems, like OpenBSD and NetBSD. However, that doesn’t mean that it’s not readily available in most Linux distributions. For Debian/Ubuntu-based, just type
$ sudo apt-get install tmux
, while for Redhat/Fedora-based, the install command would be
$ sudo yum install tmux
Now that you’re a bit familiar with terminal multiplexers, we’ll skip the basics, like the definition of a command shortcut and so forth, and get right to it. So start a terminal and simply type ‘tmux’. You will see, if the installation process completed successfully, something very similar to this :
The first thing you will notice is the fact that tmux displays by default a message line, as opposed to screen. But let’s start with the beginning : the default command shortcut in tmux is Ctrl + b (C-b), followed by the respective command. After scrolling through this article you will notice that some of the basic commands are similar between terminal multiplexers; thus, to create a new window in tmux, the command is ‘c’ (complete command would be C-b + c). Note how the message line now displays both windows, along with their default identifiers, which are positive integers, starting from 0. In order to navigate to another window you can use its respective identifier (e.g. C-b + 1) or, to access the last used window, use C-b + l.
The commands are generally pretty straightforward and easy to get accustomed to, since they generally follow the initial letter rule – ‘c’ for create, ‘l’ for last and so on. Again, please refer to the manual page for a complete list of commands.
With a simple terminal emulator (that is, without any multiplexer), you will notice that you can easily scroll up using the mouse scroll wheel or Shift + PageUp. tmux, as other multiplexers, has its own rules regarding copy/paste/scroll, so usual scrolling as described above will not work anymore. C-b + [ enters the stage : this command will allow you to copy text or scroll up for text. With C-b + ] you can paste the most recently selected text, and if you want to get out of copy/scroll mode, type ‘q’.
Like screen, tmux offers the option to attach/detach sessions; this feature, as noted before, is one of the most important ones for any self-respecting terminal multiplexer.
One of the most simple scenarios is the one where the user has, for various reasons, log out of the active session and wants to get back at it as soon as possible. C-b + d is used to detach the session (the current one) and then, when getting back to the terminal, all one has to do is type ‘tmux attach’ and that’s it, we’re back right where we left from. How you can attach to the session of your choice, provided there is more than one, is left as an exercise to the user.
Last but sure not least, we have dvtm, of which it can first be said that it is the most resemblant (at least from our humble list) of a tiling window manager.
Not that screen or tmux don’t have the capabilities necessary, it’s just that dvtm looks like that by default, as you can see here :
To start from the beginning, in order to install on Fedora or CentOS, just do
$ sudo yum install dvtm
, noting that on CentOS you will need the EPEL repository enabled and working, since the default repositories don’t have dvtm available. On Debian-based distributions, the command used to install should be
$ sudo apt-get install dvtm
Since you’re now more familiar with multiplexers, let’s get into it. Simply type ‘dvtm’ in a terminal and you’re set. The command shortcut here is Ctrl + g (C-g), so, as we’ve seen before, to create a new window just do C-g + c. You can simply change the default command shortcut to, say, C-y (make sure that won’t interfere with other shortcuts as set perhaps by your window manager or desktop environment first), you can invoke/start dvtm thusly : ‘dvtm -m ^y’.
Closing windows is achieved by default with C-g + x, and switching is done using the j and k keys. You will, especially if you’re an avid vi/vim user, some similarities with a certain text editor’s shortcuts, so you should be right at home if vi/vim is your thing. Like we’ve seen in the tmux section, using C-g + $window_number should take you to the window you need, and dvtm also sports a feature that allows you to send input to all visible windows : use C-g + a for this, followed by the command/input to send; use C-g + a again to restore normal behavior.
We’re now getting to the part that we already hinted at before, the similarities with tiling WMs. dvtm sports the following options when it comes to layouts :
- vertical stack – that means that the so-called master area gets the left half of the screen and the rest are stacked in the right half
- bottom stack – as you guessed already, basically the same, but the master area is on the top half and the rest stacked in the bottom half
- grid – all window get an equal share of the screen
- fullscreen – the name says it all – all windows get all the screen estate available
Regarding copy/paste modes, as described in the tmux section, the principle is the very same, how it’s done and the shortcuts differ. C-g + e pipes the scroll buffer to an external editor, while what said editor writes to standard output can be then pasted with C-g + p.
Finally, the detach/attach functionality is there, only it’s achieved by using external tools. While this mighty seem a turn-off at first, please remember that dvtm is all about simplicity and adherring to the Unix principle of doing one thing and doing it well. The external tool we’re gonna use is called abduco and you can use it like this :
$ abduco -c dvtm-session
to attach. It is usually a separate install and this can be done the same way you already used to install tmux, and again, on CentOS and other RHEL derivatives you will need EPEL enabled for the installation to work. Moving on, detaching is done using C-g + \ and reattachment is done with
$ abduco -a dvtm-session
As an alternative to abduco you can use dtach, and how it’s done is left as an exercise to the reader.
We hope we gave you everything you’d need to get started using terminal multiplexers or at least offer you new information about the matter if you already used some or even all of them. Probably by now you’re asking : “OK, I get it, but which one should I use?” – the answer to that is “The one that suits you best”.
There are two things here : one, there are too many user scenarios and use cases to be able to give a definitive answer and two, remember that the three terminal multiplexers we covered are probably the most popular in the field, but by no means the only ones. So we say use them, try them out, modify them to suit your needs as best as possible and then you will know what you want to use.
Even better, if you have the time and the skills and you need a feature that’s (yet) not there, open a feature request or better yet, hack the program yourself and share your code. We can only hope you enjoy these tools and become more efficient in your work.