Tmux Is A Server Administration Essential

Tmux is a terminal multiplexer. That might not sound particularly interesting, but it is one of the most useful programs in the server administrator’s toolbox. A terminal multiplexer can split a terminal window into multiple windows, each of which runs a shell.

When you log in to a server over SSH, you are faced with a prompt into which you can type commands. What if you want to run more than one command at the same time? Perhaps you need to run tests while simultaneously looking at memory consumption in htop. That can be done by creating two SSH sessions, but with Tmux you can simply split your existing terminal in two and run the commands simultaneously.

Multiplexing is useful, but Tmux’s killer feature is its ability to detach from a session and reattach to it later. Let’s say you want to set up several windows, each displaying a logging or monitoring tool. This takes time to set up, and without Tmux you would have to recreate the arrangement every time you logged in. When you log out, your shell will be closed along with all the programs you started — something of a pain if you are doing the same on several servers.

But Tmux allows users to detach from a session, which will continue to run in the background. Once you have organized your workspace, you can detach from Tmux and disconnect from the server. When you log back in over SSH, you can reattach to the Tmux session and everything will be as you left it.

Installing Tmux

Tmux isn’t installed by default on CentOS 7, but it is part of the base repository. You can install it with:

yum install tmux

 

Once installed, run Tmux and nothing much will change except for the addition of a indicator bar at the bottom of the window.

The Tmux Prefix

To send commands to Tmux, you use a prefix. The prefix helps to to distinguish between key chords intended for the shell and those intended for Tmux.

The default prefix is ctrl-b. When you want to send a command to Tmux, press ctrl-b first, followed by the command.

Basic Window Management in Tmux

I have been using the term window to refer to splits in the terminal interface, but Tmux calls these panes and refers to the area that can be split into panes as a window.

Windows can be split into panes vertically and horizontally.

Ctrl-b %

 

This splits the window into two panes along a vertical axis, creating a left and right pane.

Ctrl-b “

 

This spits the window along a horizontal axis, creating a top and bottom pane. Every pane can be split into smaller panes in the same way.

Navigating Panes

Basic navigation uses a combination of the prefix and cursor keys. You can move up, down, left, and right between windows by hitting ctrl-b $arrow_key.

You can navigate more quickly using the ctrl-b q command. It flashes up a number in each pane, and pressing that number will take you to the associated pane.

To close a pane, you can type exit at the shell or hit ctrl d.

Detaching and attaching

Once you have a Tmux session set up the way you like it, you can detach from it like so:

Ctrl-b d

 

Then you can log out. When you log back in, you can attach to the same session with:

tmux attach

 

This assumes you are only have one Tmux session. If you have more than one, you can see a list with:

tmux ls

 

Choose the number of the session you want to attach to:

tmux attach -t 0

 

It is also possible to give sessions a name by starting a new session with that name:

tmux new -s monitor_session

 

That creates a session called “monitor_session” that you can attach to with:

tmux attach -t monitor_session

 

You can also rename sessions:

tmux rename-session -t 0 monitor_session

 

This renames session 0 to monitor_session.

I have covered the bare bones of Tmux here: it is a highly configurable application and I would encourage you to dig into the documentation to learn more about what it can do.

Dedicated Server Special

Take advantage of our Double RAM offer on the E3-1230v2 4 x 3.30GHz+HT server! Only $134.95 per month. Managed and Unmanaged options available at checkout.

GET STARTED