订阅内容
Linux 

Prior to Red Hat Enterprise Linux 8, the screen command was included. In version 8, the decision was made to deprecate screen and use tmux instead. tmux is a terminal multiplexer which means that you’re able to have a process running, disconnect from the system, and then reconnect at a later time and from a different computer so that you can continue working in that process. An easy way to demonstrate this is to SSH to a remote system, start tmux, and then from inside of that, start a ping command to a remote system, disconnect from tmux, resume tmux, and you’ll see that the ping is still going. Some of the common use cases for this are:

  • If you have a long-running process, such as an upgrade of an application, and you either don’t want to leave the ssh session running the whole time or you’re concerned that your network connection might drop.
  • Going along with that, if you have multiple sessions running, you can use labels for the different sessions.
  • If you want to have somebody else see what you’re doing, you can start a tmux session and then have that other person SSH into your system and attach to that tmux session.

In order to start a basic session, just run the tmux command. This brings you into a tmux session and you’ll be able to run commands and do things just like you normally would as your user. To disconnect from your session, but still leave it running, hit Ctrl+B and then D. To resume the session, I can run:

$ tmux ls    ### to see if there are any other tmux sessions
$ tmux a    ### this will connect to the most recent session that was created.

If there are multiple sessions, then you can connect by the number of the session:

[root@opendemo ~]# tmux ls
0: 1 windows (created Thu Jan 28 19:58:27 2021) [103x22]
1: 1 windows (created Thu Jan 28 20:00:54 2021) [103x22]
2: 1 windows (created Thu Jan 28 20:01:08 2021) [103x22]
3: 1 windows (created Thu Jan 28 20:01:39 2021) [103x22]

[root@opendemo ~]# tmux a -t 1
[detached]

[root@opendemo ~]#

An example of how I use this is that the very last command of my personal start script which connects to the VPN and does other things is to run:

# /usr/bin/ssh -t shell-el7.redhat.com tmux attach

This connects me by SSH to that remote server and then attaches to my tmux session that I know is there with irssi running. For information about using irssi, please see my article Using Irssi inside a Linux tmux session.

[ You might also enjoy: Building with Buildah: Dockerfiles, command line, or scripts ]

The second example use case above was for labeling the different sessions. Now that we’ve gone over some of the basics of using tmux, let’s see an example of using labels:

[root@opendemo ~]# tmux new -s "database upgrade"
[detached]

[root@opendemo ~]# tmux new -s "other process"
[detached]

[root@opendemo ~]# tmux ls
database upgrade: 1 windows (created Thu Jan 28 19:50:03 2021) [103x22]
other process: 1 windows (created Thu Jan 28 19:50:23 2021) [103x22]

[root@opendemo ~]# tmux attach -t "database upgrade"
[detached]

[root@opendemo ~]#

You may notice that if you have text that scrolls past the top of your screen that you cannot simply hit the Page Up key to see it.

To be able to use your cursor keys in tmux so that you can go back in the buffer, press Ctrl+B and then the [ key. At this point, you’ll be able to use the Arrow keys, Page Up, or Page Down to move around in the output on the screen.

If you want to split the tmux session into having different panes, you can split your session vertically by Ctrl+B and then %. To split the pane horizontally, you’d use Ctrl+B and then a single double quote . In the screenshot below, I first split the session vertically, and then I split the right pane horizontally.

The tmux display with three screens

To move the cursor from one pane to the next, you would use Ctrl+B and then whichever Arrow key to move in that direction.

To kill a session without attaching to it and then type exit, you would use kill-session and then specify the session to kill:

Kill a tmux session

There are more options for tmux, and they are available to see by using Ctrl+B and then ?:

The tmux options displayed

[ Learn the basics of using Kubernetes in this free cheat sheet. ] 

Further reading can be found in the man page for tmux. This article has given a quick introduction to using the tmux command so that a disconnected remote session does not end up killing a process accidentally. Another program that is similar to tmux is screen and you can read about screen in this article.


关于作者

I am a Senior Principal Security Architect at Verizon. Before that, I worked at Red Hat in various roles such as consulting and in the Solutions Architect where I specialized in Smart Management, Ansible, and OpenShift. In my free time, I enjoy spending time with my family, exercising, and woodworking.

 

Read full bio
UI_Icon-Red_Hat-Close-A-Black-RGB

按频道浏览

automation icon

自动化

有关技术、团队和环境 IT 自动化的最新信息

AI icon

人工智能

平台更新使客户可以在任何地方运行人工智能工作负载

open hybrid cloud icon

开放混合云

了解我们如何利用混合云构建更灵活的未来

security icon

安全防护

有关我们如何跨环境和技术减少风险的最新信息

edge icon

边缘计算

简化边缘运维的平台更新

Infrastructure icon

基础架构

全球领先企业 Linux 平台的最新动态

application development icon

应用领域

我们针对最严峻的应用挑战的解决方案

Original series icon

原创节目

关于企业技术领域的创客和领导者们有趣的故事