Tmux tutorial, multiscreen terminal and a better ssh experience

Tmux is a terminal multiplexer, that is a piece of software that allows you access to multiple terminal sessions from a single terminal window. It is extremely useful when doing maintenance on some server via SSH because it will allow you to simultaneously see the output of multiple terminal commands at the same time without having to run multiple ssh sessions.

Installing tmux on Centos 5.x or 6.x

At the time of writing this article tmux is available in the RHEL EPEL repository. I won’t go into details explaining how to add the EPEL repository and install additional software from it, but this article from the Rackspace knowledge center gives a pretty detailed and straightforward explanation on how to do it:

Installing RHEL EPEL repo on Centos 5.x or 6.x

After you have completed the steps outlined there you just have to run:

yum install tmux

Installing tmux on Ubuntu

The process for Ubuntu is much more simple given that you are running a recent enough version. The <strong>tmux </strong>package is included in the base repository so its just a matter of:

sudo apt-get update
sudo apt-get install tmux

Using tmux

After tmux is installed by following the instructions above start by typing this in the terminal

tmux

You will notice that the terminal slightly changes to something like the one shown in the image

Tmux shell in action

Tmux shell in action

This is essentially a new terminal with the default shell logged in as your current user account. It looks somewhat different from a traditional terminal session in the sense that it shows a green (by default, but can be changed) status bar with additional information such as the number of active sessions and which is the current session you are working on. The current page that you see on your screen is referred to as a “window” in the tmux context. You can have multiple windows, which operate similarly to having multiple browser tabs open and furthermore you can split each window into multiple panes and execute commands in each pane.

In order to interact with tmux you need to first press the control key, which by default is CTRL-B. After that you can press one of the following keys for the most basic actions (first press ctrl-b, then release and then press one of the following):

Key Result
c Create a new window
n Change to next window
p Change to previous window
Split pane horizontally
% Split pane vertically
, Rename current window
o Move to next pane

If you choose to rename the current window, you will see the changes reflected in the status bar of tmux in the list of all current windows. That is convenient to keep track of long-running tasks you are monitoring. When using the “o” shortcut to move between panes keep in mind the order (panes are numbered left-to-right, top-down). For faster navigation you can also use the keyboard arrows to switch between panes, so if you press CTRL-B and then one of the keyboard arrows the pane in the direction of the arrow will be activated. You can close the current panel, by typing exit in it, which will cause the other panes in the window to adapt and fill the entire space. If you dont have any other panes in the window, typing exit will close that window and if it’s the last window, it will close tmux and return to the session that launched it.

Advanced usage

What happens under the hood is actually a bit more complicated. Tmux runs in a client-server mode, where each time you launch tmux a new sessions is created and the client is attached to that session. This gives you quite a bit of flexibility since you can “detach” the client from a certain session and let it run in background and then later re-attach to it and continue where you left off, having all the windows and panes saved in the same state. In order to detach from the current session hit the control key + d:CTRL-B + D

After you have detached from a session you are returned back in the session that launched tmux. You can type:

tmux list-sessions

and this will give you a list of currently active sessions like the one below. Note that the left most number is the session ID, which you can use to attach back to that session. This output also gives some information about the running sessions, such as number of windows, date created, size, etc.

Currently active tmux sessions

Currently active tmux sessions

In order to attach back to a running session, look at the id of that session in the output above and type (change the 0 to the id of the desired session):

tmux attach -t 0

and this will bring you back to the same state that you left of, with the exact same window and panes arrangement.

Scripting in tmux

A really powerfull feature of tmux is that you can write scripts for it. I won’t go into details about that and the man page provides pretty good description of the commands and options you can use.

  • cisraelc

    sorry I am using my email address and password for google

  • Astrill Arnold Chitwa

    wonderful article.

  • Pedro Acácio

    Great tip! I will use it with Guake on ubuntu. I think it is a good combination.
    I became a fan of your blog today when I read the post http://www.techchattr.com/never-use-opencart.
    God bless you o

  • denemeleerrrr

    denemelerim artık sonuç vermiş durumda adamım teşekkürler

  • denemeler

    denemeler