How to use the tmux multiplexer for a more efficient Linux terminal

With tmux you can remote in once, split your terminal into panes, and get to work.

Image: Jack Wallen

Chances are you use Linux in your data center. You probably remote into one or more of those Linux servers and do much of the admin work. If you're nodding your head, you probably sometimes wish you had could work with a more powerful terminal tool. Fortunately, for you, there is such a thing in tmux, a terminal multiplexer.

The tmux tool does a lot of things, but what it does best is turn a single terminal into a multi-pane tool, where you can take care of more than one task at a time. With tmux in play, you don't need to SSH into that same machine multiple times, you can remote in once, split your terminal into panes, and get to work.

Of course, tmux is much more than that. The tmux tool also allows you to create new windows and then go back and forth between windows and panes. Tmux can also create sessions that can then be detached and used. If it sounds a bit complicated, trust me it's not. Let's get tmux installed and start using it.

SEE: Disaster recovery and business continuity plan (Tech Pro Research)

Installation

I'll demonstrate tmux on Elementary OS, but the app can be installed from nearly any standard repository. To install tmux on any Debian-based distribution, issue the command:

sudo apt-get install tmux -y

This should pick up any necessary dependencies and install the package.

Creating and using panes

The first thing you must do is start tmux. This is done from the bash prompt, by typing the command:

tmux

You should now see a green status bar at the bottom of the window with the hostname, time, and date at the right corner and the currently opened windows at the left (Figure A).

Figure A

Figure A

Our terminal window with tmux running.


You are now in a tmux session. The first thing you need to understand is what's called the prefix combination. To do most things with tmux, you need to first type the prefix key combination, which is [Ctrl]+[b]. For example, to split a screen into two vertical panes, you first hit the prefix combination (again, [Ctrl]+[b]), release those keys, and then type %. You should now have two vertical panes in one window (Figure B).

Figure B

Figure B

Tmux has split the terminal into two panes.


At this point, you might find yourself stuck in the newly created pane. Fortunately, tmux can navigate between panes. How? Hit the prefix combination and then use your left arrow key to move from the right pane to the left pane (or the right arrow to move from the left to the right pane).

You can then split one of your panes into two horizontal panes by hitting the prefix combination and then typing ". You should now have one vertical pane with two horizontal panes and one pane without (Figure C).

Figure C

Figure C

Our multi-pane window.


To close a pane, simply type the exit command or the [Ctrl]+[d] combination.

Creating and using windows

What if panes aren't enough? You can use tmux to create new windows (and even split the new windows into panes). To create a new window, hit the prefix combination and then type c. You should now see a new window. You can go back to the previous window by typing the prefix combination followed by p.

Detaching and attaching sessions

Let's say you have a command running (like tail -f /var/log/syslog), but you don't want to end the command (and you'll want to come back to that still-running command later). The first thing you need to do is detach the session. This is done by hitting the prefix combination and then typing d. Once you've detached, you'll find yourself back at the regular bash prompt (out of tmux). To see what tmux sessions you have running, issue the command:

tmux ls

The above command will list out any sessions you have running (Figure D).

Figure D

Figure D

Available tmux sessions.


As you can see I have session 0 and session 2 still running. To attach session 2, issue the command:

tmux attach -t 2

You should now find yourself back in that session, with your command still running (Figure E).

Figure E

Figure E

The tail command is still following /var/log/syslog in tmux session 2.


You can also name tmux sessions (so it's easier to remember what you're doing with each session), like so:

tmux new -s "SSH DCENTER 1"

If you detach that session, you can attach it like so:

tmux attach -t "SSH DCENTER 1"

Find out more

And that's the gist of using tmux. To find out more about this handy command, type man tmux and read through the manual page.

Also see