Linux Format

Tmux: Terminal multiplexe­r

Neil Bothwick shows you how to do more in the shell and do it all at once…

- Neil Bothwick has a computer in every room and a compelling desire to control them all at one time, remotely from the terminal on one computer. Hence this tutorial…

Tmux is a terminal multiplexe­r. That term may sound familiar if you have ever used screen [Tutorials, p74, LXF192]. A terminal multiplexe­r enables you to run one or more detachable terminal sessions inside a single terminal or virtual console. Some of the graphical terminals provide tabs, allowing you to run several terminals in the same window, but a multiplexe­r goes much further. It also doesn’t require an X terminal, although it is very happy running inside of one. Tmux is probably not installed on your distro by default, but should be in the package repositori­es, so install it from your distro’s software manager in the usual way. Once it’s installed, open a terminal or console and run $ tmux

Nothing much appears to happen apart from the terminal window clearing and a status bar appearing at the bottom. Run a command, anything that gives some output, say top , then press Ctrl+b, then c (that is, press Ctrl+b, release, then press c and release). The window clears, and your previous command appears to be gone. Run another command, then press Ctrl+b, then n and there is your original command back again. Subsequent presses of Ctrl+b, n switch between the two windows, or more if you pressed Ctrl+b, c.

Much more than tabs

We can hear some muttering that you can do that with tabs in some X terminals, and you can, but tmux can do it anywhere you run a shell, and it has some far more powerful tricks up its sleeve. Run something that takes a while to complete – it could be a video transcode with ffmpeg, a simple sleep command or the old favourite $ telnet towel.blinkenlig­hts.nl then press Ctrl+b, then d and your entire tmux session disappears, but it is still running, just detached from your terminal. Open another terminal and run: $ tmux attach and, as if by magic, your previous session reappears with the commands still running.

This is not limited to local terminals either: you can reattach a tmux session over an SSH connection. This makes tmux especially useful for remote administra­tion. If you want to run a lengthy command from a normal SSH session, you need to stay connected until it completes – especially

problemati­c if you are mobile. If you run the command inside a tmux session, you can detach as soon as it is running and connect again later to see how it is progressin­g.

If you have a tmux session running and you run tmux again (without attach), you will start a separate session. When you run tmux attach it will connect to the first available session, so how do you connect to another? Like so: $ tmux list-sessions $ tmux attach -t N

The first command lists your sessions, each with a number, and the second attaches to session number N .

Sessions, Windows and Panes

There is a certain amount of jargon associated with tmux. It uses a client/server approach; the first time you start a tmux session the server is automatica­lly started, and the tmux client then interacts with the server. When run without a following command, it starts a new session. Each tmux session is a separate instance and operates independen­tly of any other sessions, although they all run on the same server. Each session contains at least one window, and you create windows with Ctrl+b, c. These windows all exist within the same session, so when you detach and reattach, they are still there. Windows fill the terminal window, so you can see only one at a time – they are listed in the status bar at the bottom of the terminal and you use Ctrl+b, n to switch to the next.

You’ve seen all of this already, but windows can also be split into panes. Press Ctrl+b followed by the double-quote character, to split a window into two panes, one above the other. Use Ctrl+b, then % for a side-by-side split. Each window contains a single pane when it is created, so you are splitting that pane into two. You can see this in action by pressing the split keys again: only the current pane is split.

If you administer multiple computers, this is a real gamechange­r. You can have a single terminal window displaying multiple SSH logins at the same time. After a few pane splits, you may find the window gets a little messy with different sized panes. To rearrange them, press Ctrl+b followed by Alt+n where n is any of the keys from 1 to 5, each giving a different layout, you’ll use Ctrl+b Alt+5 most often.

Way back we explained how to use ClusterSSH to run terminal commands [Tutorials, p74, LXF179] and view the output on multiple computers – it opens a small xterm for each host. We can replicate this with tmux: press Ctrl+b then the colon (:) to open the tmux command line, then type: setw synchroniz­e-panes Now anything you type goes into each of the panes in that window. This applies to the current window only; any other windows and sessions are unaffected.

Configurin­g tmux

Tmux looks in two files for configurat­ion settings. Global options are stored in /etc/tmux.conf while user settings live in ~/.tmux.conf. Both files are optional – tmux has useful defaults, but if an option is present in both, then the user file takes precedence. These allow you to tweak the behaviour of tmux, eg if you want to assign the synchroniz­e-panes setting from the above example to a key, you could include this line in one of the config files bind-key S setw synchroniz­e-panes

If this key is already defined by default, you will change its behaviour, so it may be best to pick something that is unused. You can see the existing key bindings by entering the command prompt with Ctrl+b, : and running list-keys . The full list of commands available is covered in the tmux man page; you can test these in the command prompt and then put them in a configurat­ion file to make them permanent.

Direct control

So far we have run tmux with no arguments or with attach to connect to a session, but there are other options. You can give one or more tmux commands some arguments to have them executed when tmux starts. Multiple commands are separated by a semi-colon, which must be escaped as the shell also uses it as a command separator, eg: $ tmux new-session \; split-window -h

This creates a new session and then splits it into two panes. You can also follow the command by a shell command that is executed in the window or pane, in which case the pane will close once the command exits. Let’s go back to the idea of managing multiple SSH sessions in the same window: $ tmux new-session ssh host1 \; split-window ssh host2 \; split-window ssh host3...\; select-layout tiled \; setw synchroniz­e-panes

That’s a fairly long and unwieldy command, but you could add it to a shell alias or a one-line shell script and be able to administer all your computers from a single terminal or console. This highlights one of the strengths of tmux for power users: it is well suited to being run and controlled from scripts. There is a lot more to tmux – we have barely scratched the surface here – but there is plenty of useful informatio­n in the man page and online.

 ??  ??
 ??  ?? Tmux has a large number of commands available, but thankfully one of them is list-commands.
Tmux has a large number of commands available, but thankfully one of them is list-commands.
 ??  ??
 ??  ?? One of tmux’s talents is running a command on multiple computers and viewing the output from all of them.
One of tmux’s talents is running a command on multiple computers and viewing the output from all of them.

Newspapers in English

Newspapers from Australia