Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Multi-session Terminal Management in Tmux

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how to achieve multi-session terminal management in Tmux. The content is concise and easy to understand. It will definitely make your eyes shine. I hope you can gain something through the detailed introduction of this article.

Install tmux under Linux

Tmux is available in most official Linux repositories.

On Arch Linux or its variants, execute the following command to install:

$ sudo pacman -S tmux

Debian, Ubuntu or Linux Mint:

$ sudo apt-get install tmux

Fedora:

$ sudo dnf install tmux

RHEL and CentOS:

$ sudo yum install tmux

SUSE/openSUSE:

$ sudo zypper install tmux

Above, we have completed the installation of Tmux. After that, let's move on to some Tmux examples.

Tmux command example: Multiple session management

Tmux defaults to Ctrl+b as the prefix for all commands. Remember this shortcut before using it.

Note: Screen's prefix command is Ctrl+a.

Create a Tmux session

Run the following command in the terminal to create a Tmux session and attach it:

tmux

Or,

tmux new

Once you enter the Tmux session, you will see a green sidebar sunk at the bottom, as shown below.

Create a Tmux session

This green sidebar can easily indicate whether you are currently in a Tmux session.

Exit Tmux session

To exit the current Tmux session, simply use Ctrl+b and d. Instead of triggering both shortcuts, press Ctrl+b and then d.

After exiting the session, you should see the following output:

[detached (from session 0)] Create a famous session

If you use multiple sessions, you are likely to confuse applications running in multiple sessions. In this case, we need to talk and give names. For example, a session that requires web-related services creates a Tmux session called "webserver"(or whatever it is).

tmux new -s webserver

Here is the new Tmux famous conversation:

Tmux session with custom name

As you can see in the screenshot above, the name of this Tmux session has been labeled "webserver". In this way, you can easily distinguish between applications in multiple sessions.

To exit the session, tap Ctrl+b and d.

View Tmux session list

View the Tmux session manifest and execute:

tmux ls

Example output:

List Tmux sessions

As you can see, we opened two Tmux sessions.

Create an unattached session

Sometimes you may want to simply create a session, but you don't want to automatically jump into it.

Create a non-attached session and give it the name "ostechnix" and run:

tmux new -s ostechnix -d

The above command will create a session called "ostechnix" but will not attach to it.

You can verify this by using the tmux ls command:

Create an unattached session

Attach to Tmux session

You can attach to the last session created by:

tmux attach

Or,

tmux a

If you want to attach to any of the named sessions, such as "ostechnix," run:

tmux attach -t ostechnix

Or, abbreviated as:

tmux a -t otechnix Close Tmux session

When you are finished or no longer need a Tmux session, you can close it by issuing the following command:

tmux kill-session -t ostechnix

While in the session, use Ctrl+b and x. Click y to close the session.

This can be verified with the tmux ls command.

Close all sessions under all Tmux services and run:

tmux kill-server

Be careful! This will terminate all Tmux sessions without any warning, even if there are running tasks in the session.

If there is no active Tmux session, you will see the following output:

$ tmux lsno server running on /tmp/tmux-1000/default Cut Tmux window

Cut the window into several small windows, in Tmux, this is called "Tmux pane". Different programs can run simultaneously in each pane and interact with all panes simultaneously. Each pane can be resized, moved, and closed without affecting the others. We can cut the screen horizontally, vertically, or a mixture of both.

horizontal cut pane

To cut the pane horizontally, use Ctrl+b and "(half a double quote).

Cut Tmux pane horizontally

The panel can be further cut using key combinations.

Vertical cutting pane

Cut the panel vertically, using Ctrl+b and %.

Vertical cut Tmux pane

Horizontal and vertical mixed cutting pane

We can also cut panes in both horizontal and vertical schemes. Take a look at the screenshot below:

Cut Tmux pane

First, I cut horizontally with Ctrl+b, then vertically with Ctrl+b %.

As you can see, I ran different programs under each pane.

Toggle pane

Switch panes by Ctrl+b and arrow keys (up, down, left, right).

Send commands to all panes

In the previous example, we ran three different commands in each pane. In fact, you can also send the same command to all panes.

To do this, use Ctrl+b and type the following command, then press Enter:

:setw synchronize-panes

Now type any command in any pane. You will see that the same command affects all panes.

Swap panes

Use Ctrl+b and o to swap panes.

Display pane number

Use Ctrl+b and q to display pane numbers.

Terminate pane

To close the pane, type exit and press enter. Or, press Ctrl+b and x. You'll see confirmation. Press y to close the pane.

close pane

Zoom in and out of the Tmux pane

We can zoom in the Tmux pane to the full size of the current terminal window for better text visibility and to see more content. This is useful when you need more space or focus on a particular task. After completing this task, you can zoom out (unzoom in) the Tmux pane to its normal position. For more details, see the links below.

How do I scale the Tmux pane to increase text visibility?

Autostart Tmux session

When working with remote systems via SSH, it is always a good practice to run a long-running process in a Tmux session. Because it prevents you from losing control of running processes if your network connection is suddenly interrupted. One way to avoid this problem is to start the Tmux session automatically. For more details, please refer to the links below.

Automatically start a Tmux session when logging on to a remote system via SSH

summary

At this stage, you have acquired basic Tmux skills to manage multiple sessions, see the man page for more details.

$ man tmux The above content is how to implement multi-session terminal management in Tmux. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report