Skip to main content

Tips for using tmux

The tmux command replaced screen. It allows you to reconnect to dropped sessions, helping you maintain long-running applications or processes.
Image
Train tracks crossing

Image by Michael Gaida from Pixabay

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.

Image
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:

Image
Kill a tmux session

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

Image
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.

Check out these related articles on Enable Sysadmin

Topics:   Linux   Command line utilities  
Author’s photo

Peter Gervase

I currently work as a Solutions Architect at Red Hat. I have been here for going on 14 years, moving around a bit over the years, working in front line support and consulting before my current role. In my free time, I enjoy spending time with my family, exercising, and woodworking. More about me

Free Event: Red Hat Summit 2021 Virtual Experience

Join Red Hat Summit Virtual Experience for live demos, keynotes, and technical
sessions from experts around the globe—happening April 27–28 and June 15–16.

Related Content

OUR BEST CONTENT, DELIVERED TO YOUR INBOX