The Basics Of tmux – The Terminal Multiplexer (Linux)

If you have ever found yourself launching multiple terminal / command line windows and then losing the window you needed, or find that your remote session to your Linux device has lost connectivity which means an active process also ending then tmux may be of help to you.

tmux is a terminal multiplexer that allows for multiple terminal sessions to be running and for the end user to switch between those sessions. It also keeps processes running if the connection is dropped (e.g. if your SSH session to the linux device gets cut off).

Installing tmux

sudo apt-get install tmux
Installing tmux
Installing tmux

Launching A tmux Session

From the terminal / command line a tmux session can be created using the command:

tmux

However it is more preferable to use:

tmux new -s SESSION_NAME

e.g. the below creates a tmux session called geektechstuff

tmux new -s geektechstuff

Giving a session a name makes it easier to recall what you launched the session for, or what task you wanted to run in a session.

Listing running tmux Sessions

To see what tmux sessions are currently running use the command:

tmux ls
tmux ls
tmux ls

Attaching A tmux Session

If you have already had a tmux session open and need to connect to it then use the command:

tmux attach

To attach the last active tmux session.

tmux at -t SESSION_NAME

Viewing Active tmux Sessions

Using the keyboard combination CTRL+B and W (that’s CTRL+B then quickly press W) splits the terminal / command line into 2 panes showing the active tmux sessions / a brief of what each one is doing in the top pane and lets you scroll through each one to see it in the bottom pane.

geektechstuff_tmux_3
CTRL+B W quickly shows what it us running in each tmux session

In the above example I have the following tmux sessions open:

  • geektechstuff
    running bash but not currently doing anything
  • logs
    running the tail command against the systlog
  • pingtest
    running the ping command against my website

Detaching A tmux Session

If you want to detach from a tmux session and leave the session running then press CTRL+B D (that is CTRL+B followed quickly by D).

Closing / Ending A tmux Session

If you have finished with the tmux session and want to close / end it, type:

exit

This will close / end the session and any process running within it.