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.
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>
To begin a new tmux session:
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:
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:
Now we have three panes in our window. It should look like this:
To switch to the next pane (panes are numbered left-to-right, top-down):
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.
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:
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:
To move backwards in the index:
Windows provide more concrete separation than panes. Two windows are never visible at the same time.
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:
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:
A list of all command keys is accessible with:
Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.Sign up
Click below to sign up and get $100 of credit to try our products over 60 days!