As you manage Alibaba Cloud Elastic Compute Service (ECS) instances, you will often find yourself working at the command line in so-called pseudo-terminal sessions. Typically, you would connect to your server with an SSH client like PuTTY and after logging in you are dropped into a shell like Bash where you can enter commands and interact with the operating system. For most purposes, this simple setup suffices. However, in certain scenarios, you may need, or at least benefit, from expanding the capabilities of such a session with a terminal multiplexer like
tmux. Just like the operating system running on your phone or computer can execute and display, side by side, multiple applications on its graphical user interface, so can
tmux do with text-based programs and shell sessions. But practical examples are easier to understand than theory alone so let's explore a few use cases:
Although obviously not limited to these use-cases, we can see that we will generally want tmux when we:
Now let's learn how to use tmux.
On Debian or Ubuntu run:
apt update && apt install tmux
yum install tmux
And on OpenSUSE:
zypper refresh && zypper install tmux
Let's start tmux:
Run the following command to start a simple countdown, from 600 to 1 (10*60 seconds = 10 minutes):
for i in `seq 1 $((10*60)) | tac`; do clear; echo $i; sleep 1; done; echo "Finished!"
Now you can test what would happen if your SSH connection would end abruptly. For example, if you're running PuTTY, force-close the window. Afterward, log back in to your server and enter the following command:
This reattaches you to the session and you will see that the countdown continued even while you were disconnected. If the need ever arises, you can also manually detach from a tmux session by pressing CTRL+B, then releasing the CTRL key and afterwards pressing D. CTRL+B is the default so-called prefix key. This combination signals that the next key/keys pressed should be caught and interpreted by tmux. Reattach to the previous session if you have detached:
We've seen how easy it is to keep processes running in the background and reattach to their input and output even if we lose connectivity. Now let's explore the multiplexing part. Let's say one process will take a long time to complete. This is represented by the 600 second counter that should still be running. But we want to do other things on the server during this time. To open another window in tmux press CTRL+B (now release CTRL) and then press C. In the bottom status bar you will see we have two windows now:
"0:" and "1:" represents the index number of the window. After this number, the name of the process currently running in the window is displayed. The "*" symbol is added to the window which is currently displayed (active). Now we can do other work while the process in window 0 progresses. To switch to the previous window press CTRL+B followed by P. Press CTRL+C if the countdown is still running to stop it. To close a window, simply stop all the processes running there and exit the bash session with:
Windows are useful when you need to focus and multitask in full screen, but sometimes you will want to simultaneously see the output of multiple tasks/processes. In this case, you can split a window in panes. To split vertically, press CTRL+B and then % (on most keyboards this will mean actually pressing SHIFT+5 since that's where we find the percent sign; however keyboards that are not in the US layout will require a different combination of keys). The result should look like this:
To split a window horizontally, press CTRL+B followed by " (quotation mark). If we are already in an active pane, this will sub-split it. In our case, the result should look like this:
To change the active pane you want to work in press CTRL+B then O. This will switch to the next one. A list of useful keyboard shortcuts will be provided at the end of this tutorial.
We currently have a window with three panes. To create another workspace, you can launch a new window and then also split that into panes. Furthermore, you can even launch a new tmux session, which can consist of an entirely different collection of windows and panes. You can then switch between these sessions as required. We've seen how to use keyboard shortcuts, now let's learn a new way we can interact with tmux. You can enter command mode interaction by pressing CTRL+B followed by
: the colon sign. The input cursor will move to the bottom status bar, where you can type the name of the tmux command you want to execute. Type this to start a new session:
Press ENTER to execute the command. A new session will initialize with one window and one pane. To switch to the previous session, press CTRL+B and then ( the open parenthesis sign.
Here's another tmux command you might find useful. Press CTRL+B then
: and enter the command
set -g mouse on. This will enable mouse interaction. Now you can switch to another active pane by simply clicking on the respective pane box. By clicking and dragging the line separating panes, you can resize them.
You can add such options to a configuration file in your home directory. This would apply them automatically each time you launch tmux.
You can create this file now and add the previous option with:
echo 'set -g mouse on' >> ~/.tmux.conf
If your user's name on your Alibaba ECS instance is "johnsmith" you will find the file in this location:
To manage windows, after pressing the prefix key (by default, CTRL+B), press:
To manage panes, after pressing the prefix key (by default, CTRL+B), press:
To manage sessions, after pressing the prefix key (by default, CTRL+B), press:
To scroll up and see previous terminal output, press CTRL+B followed by PAGE UP. PAGE UP, PAGE DOWN and arrow keys can be used to navigate through the output. Press q when you want to quit this mode and return to the command prompt/current output.
This summarizes the basics and most widely used options of tmux. However, the utility is very flexible, customizable and includes hundreds of other functions. If you want to read more about it, you can either consult your distribution's manual page with a command like
man tmux, read the manual online, if you find an HTML page is easier to navigate https://manpages.debian.org/stretch/tmux/tmux.1.en.html or even read a book on the subject: https://leanpub.com/the-tao-of-tmux/read
Alibaba Clouder - July 18, 2019
Alibaba Clouder - November 19, 2019
Alibaba Cloud Storage - May 8, 2019
Alibaba Container Service - November 13, 2019
Alibaba Clouder - February 15, 2018
Alibaba Developer - January 10, 2020
An online computing service that offers elastic and secure virtual cloud servers to cater all your cloud hosting needs.Learn More
Alibaba Cloud Server Load Balancer is built to cope with high volume traffic and each year in November is put to the test during Alibaba’s annual Global Shopping Festival. Alibaba relies on Server Load Balancer to provide uninterrupted service during the festival by switching requests between data centers and transferring transactions to the most available server.Learn More
Automate performance monitoring of all your web resources and applications in real-timeLearn More
More Posts by Alibaba Clouder