SYSADMIN: TERMINAL MULTIPLEXERS
|We take a brief break from our normal our sysadmin coverage to reveal two of the most important commands you should learn: screen and tmux.
Many of us use the command line for lots of tasks – from the mundane to the complicated. But there’s one single type of command that, if you’re not already using it, will transform every command you type. This is a class of command known as the humble terminal multiplexer. You might be thinking that anything called a multiplexer might not sound so humble, but bear with us. It’s not difficult to understand and it can make a huge difference to the way you use any Linux machine. The term ‘multiplexer’ comes from electronics jargon, where it’s used to describe a device that transforms multiple inputs and a single output before being ‘demultiplexed’ into its composite parts at a destination.
With a terminal multiplexor on the command line, the multiple inputs are shell sessions and the single output is your display or single physical terminal. When described like this, it might not sound like the most exciting kind of command, but after you’ve got used to working within its environment, it’s difficult to go back to a terminal that doesn’t offer the same features; among a huge list, our favourites are the ability to spawn other terminals dynamically, switch between terminal sessions at any point, split the screen into different views and restore a running session at any point, even after disconnecting from the session completely. If you like running command line applications like Mutt for email, or Irssi for IRC, they’re also perfect for persistent remote sessions.
They work best when you don’t have access to a windowed environment, because it’s easier to open multiple shell windows, although we’d argue less productive. The natural environment for a terminal multiplexer is the headless server. These are running somewhere remotely, and usually only accessible via SSH. You’re often connected for long periods and often want to do more than one task at the same time. NAS boxes, Raspberry Pis, set-top-boxes and family PCs are equally well suited targets. A terminal multiplexer will allow you to open a single connection to any of these devices and use the same terminal session for multiple tasks. They should also allow you to disconnect from a session, complete with running processes, and re-connect at a later time.
They’re synonymous with window managers in X – the part of your desktop that remembers where Firefox is running and lets you move the window around, restore its position when you reconnect or switch between running applications with a keyboard combination. They could even be thought of as the ultimate tiling window managers, albeit without visible tiles or window management, although with a little configuration, you can get close.
Screen test
This being open source there’s more than one solution, and the two most popular are probably equally used and mostly matched for functionality. The first is the venerable GNU Screen and this is the command you’re most likely to find pre-installed on your remote server or local NAS. Just type screen -h to check. If it’s not there, screen will always be part of your distribution’s principle repository or software archive. The other popular alternative is called tmux, a more descriptive name – terminal multiplexer. We’re going to cover the basics of both so that you get a good idea of what they’re capable of, and perhaps, which one you prefer. They start off working in much the same way before diverging slightly.
If we had to recommend just one terminal multiplexer, it would be tmux. It’s a project that’s still actively developed and had a few more advanced features. But we’re also covering screen because you might already have it installed.
Learning something of both also means that if you already know how to use one you should be able to migrate to the other tool by focusing on the similarities. This is particularly useful if you’ve always used screen and yet want to move to tmux. Tmux is often considered the more modern option, thanks to its better code and config files, but the ubiquity of screen means that this is the one you’re more likely to have used already, and for that reason, this is the one most people already know about, but there’s also lots both tools have in common, and looking at this commonality is the best place to start if you want to learn how to use either.
Both screen and tmux are launched by invoking their names on the command line. Screen will display a text-based splash screen asking you to press Space. After you’ve pressed Space and you’re dropped back to the normal command line, it gives no other indication that screen is running in the background, other than changing the name of the session, usually visible in the window border at the top. And unless you use a special keyboard shortcut to summon screen’s control mode, the new session will perform exactly like a native session, and it’s sometimes easy to forget whether you’re operating within the confines of screen, or you’ve not run screen yet.
The tmux option
Tmux, by comparison, makes it much more obvious, because it places a coloured bar at the bottom of the session. Like the launch bar of a desktop, this can be configured to your preference, as much of the experience can be after you’ve become acquainted with the way that tmux works.
The super special secret escape sequence keyboard shortcut that puts both screen and tmux into their meta-command mode is slightly different: for screen, it’s pressing Ctrl and A together. For tmux, it’s Ctrl and B. What isn’t immediately obvious, and something that will catch you out the first time you use either tool, is that there’s no feedback to indicate the change of input mode that comes when you successfully hold down the keyboard shortcut. You just have to take it on faith that any further keys you press will be interpreted by either screen or tmux, rather than by the command line session you’re running.
To give you some idea of what these tools are capable of, we’ll run through how you might typically use either screen or tmux. From this silent entry point, press C to create a new session. If there was anything visible in your old session, this will disappear and new blank session will appear. But worry not! That previous session is still running and is still easily accessible. You may even have noticed that the status line at the bottom of tmux will have updated to show the presence of the new Bash terminal.
Enter the escape sequence combo and press N. This is the terminal equivalent to switching to the next virtual desktop, and as we’ve only created two, this command will take you back to the previous session. You can switch between sessions directly by entering a number instead – sessions start at zero, and pressing P will take you back to your previous session. Now start something running, such as top and enter the escape sequence followed by D. This will ‘detach’ the terminal from all the sessions running within screen and tmux. You can now disconnect from the server, close the terminal, or go and make a cup of tea. Everything you started in your various sessions will keep executing.
There’s some variation in the way that screen and tmux re-attach to a running session. Typing screen -r will re-attach to the running screen session, while tmux a will re-attach to tmux. If more than one session is running, screen will list them while tmux will simply connect to the first. If you want to specify a session, add the number value from the list. You can list running sessions in tmux by typing tmux ls and connecting to your chosen session with tmux a -t session-name. And that’s all there is to the basics of using both tools. You can now start sessions and do all your usual command-line magic, safe in the knowledge you can disconnect and resume, and switch between any number of persistent terminal sessions. It’s a serious upgrade to the way the terminal normally behaves.
Taking it further
Both screen and tmux will allow you to split the display, for example, so you can see two or more sessions at the same time. Use S in screen and double quotes in tmux for this. Screen won’t automatically create a new terminal though, so you need to press the escape sequence followed by Tab to switch focus to the new region and then the escape sequence again, followed by C to create a new terminal.
This is where tmux starts to pull ahead, because it’s more adaptable to both horizontal and vertical splits and how you can arrange them within sub-groups and move between them. You really can use tmux like a tiling window manager for the terminal, and it can be brilliant if you’re reconnecting to view a server’s logs, or building and developing code, or even if you’re just copy files somewhere.
We take a brief break from our normal our sysadmin coverage to reveal two of the most important commands you should learn: screen and tmux.
Screen and tmux shortcuts
Function | screen | tmux |
---|---|---|
New Window | C | C |
Next Window | N | N |
Previous Window | P | P |
Jump to window | 0-9 | 0–9 |
Detach | D | D |
Show shortcuts | ? | ? |
List windows | “ | S |
Split vertically | Shift+S | “ |
Next region | Tab | cursor keys |
Close region | Shift+X | X |