// Tutorial //

How To Install And Use tmux On Ubuntu 12.10

Published on April 18, 2013
Default avatar
By J Kim
Developer and author at DigitalOcean.
How To Install And Use tmux On Ubuntu 12.10

About tmux

tmux is a terminal multiplexer. It allows you to access a tmux terminal using multiple virtual terminals.

tmux takes advantage of a client-server model, which allows you to attach terminals to a tmux session.

This means that you can run several terminals at once concurrently off of a single tmux session without spawning any new actual terminal sessions.

This also means that sudden disconnects from a cloud server running tmux will not kill the processes running inside the tmux session.

tmux also includes a window-pane mentality, which means you can run more than one terminal on a single screen.

The virtues of the client-server model and window-pane mentality are discussed further below.

Installing tmux on a VPS

Before installing tmux on the VPS, it's a good idea to update apt to ensure we have the latest packages.

	<pre>sudo apt-get update</pre>

Then install tmux:

	<pre>sudo apt-get install tmux</pre>

Basic Usage

To begin a new tmux session:

	<pre>tmux</pre>

tmux will automatically login to your default shell with your user account.

One difference is the appearance of the tmux status-line at the bottom of the screen. It will show you information about your current tmux session.

As it is, you can treat this like a completely new terminal. In fact, it really is just a new terminal running from inside tmux.

This new terminal is called a window in the context of tmux. Let's split our window into two panes.

tmux can be controlled using a prefix key (by default, Ctrl-b) and a command key. The command key to split into two panes is %. From within tmux:

	<pre>Ctrl-b %</pre>

The window will split into two panes. The first pane will retain the work from before the split. The second will login to a brand new terminal.

We can split our second pane horizontally:

	<pre>Ctrl-b "</pre>

Now we have three panes in our window. It should look like this:

three panes

To switch to the next pane (panes are numbered left-to-right, top-down):

<pre>Ctrl-b o</pre>

Exiting out of a shell with exit will destroy the associated pane. Adjacent panes will expand to make use of the freed space.

Closing the last pane will end tmux.

Using Windows

A step in context above panes are windows. Windows behave similarly to tabs in a browser.

When tmux starts up, it gives you a window and a single pane inside the window.

To create a new window:

	<pre>Ctrl-b c</pre>

tmux will switch to the new window automatically. You can see the new window indicated in the status-line. Windows are numbered from 0, so our new window is number 1.

Now you can create panes and treat this window like we did before. We can even create another window. Our three windows are numbered 0, 1, and 2.

To move to the next window in the index:

	<pre>Ctrl-b n</pre>

To move backwards in the index:

	<pre>Ctrl-b p</pre>

Windows provide more concrete separation than panes. Two windows are never visible at the same time.

Leveraging the Client-Server Model

What really happens when you invoke tmux is more complex than it initially appears. tmux actually creates a new session. Then a single window is created in the session. A single pane is created in the window.

New sessions are created when tmux is invoked. It's the highest level of context containment. A single instance of tmux is linked to a single session.

When you exit the last shell in the last pane in the last window of a session, the session will terminate. Instead of exiting from the session entirely, you can _detach_ from the session.

A session will continue to run in the background (tmux will fork into the background), running whatever shells were on it when you detached.

When you re-attach to the session later, you will return to the session in the same state it was when you detached. All windows and panes are retained by the session.

This is particularly useful for letting processes run on a virtual private server while not staying connected to the VPS.

To detach from a session:

	<pre>Ctrl-b d</pre>

To re-attach to a session:

	<pre>tmux attach -t [session name]</pre>

Sessions, like windows, are numbered beginning from 0. If you forgot which session you wanted to attach to, you can view active sessions:

	<pre>tmux list-sessions</pre>

A list of all command keys is accessible with:

	<pre>Ctrl-b ?</pre>

If you’ve enjoyed this tutorial and our broader community, consider checking out our DigitalOcean products which can also help you achieve your development goals.

Learn more here


About the authors
Default avatar
J Kim

author

Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?
3 Comments

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

wtf .this my computer do not installed termux for Ubuntu #A.T.S_

I’ve recently finished a book on tmux, The Tao of tmux. It’s available to read for free on the web. You can also grab it on Amazon as well as DRM-free in PDF/mobi/epub from leanpub.

The reason I wrote the book is I prefer the approach of separate tmux into its objects: Server, Sessions, Windows and Panes. Wrapping your brain around this concept makes everything easier since commands, config and so on will all mention them.

When I’ve heard about tmux for the first time some months ago, first thing that came into my mind was to leave screen with its -dmS arguments away and even set tmux as yet another system shell for a couple of users. Very handy for starting up servers by scripts and having their interactive console available later without messing with pipes and log files up. Well with the simple screen at least everything was running smoothly for several years. I could not find it as easy with tmux as with screen especially having tmux set as a system shell almost from the beginning. :P now I am the fan of both tools to be used according to users preference and specific situation.

Terminal multiplexer is obviously more advanced and has wider possibilities than screen, e.g. combining the -f argument of tail in one pane with ii from suckless.org in another and yet another for orchestrating the whole leads to projects like iiii from https://github.com/tb01110100/iiii. Notice however higher resources consumption by tmux.

I imagine other users sessions take-overs by a random BOFH is slightly easier with tmux than with screen only because of pseudo-terminal permissions to be altered in the second case and client-server design of tmux in the first, but I have never tested that yet. xD