Tmux, short for Terminal Multiplexer, is a powerful tool that offers a number of benefits for those working in the terminal. If you’re a system administrator, developer, or anyone who regularly connects to multiple servers, Tmux can be a game-changer.

What is Tmux?

Tmux allows you to control several terminal sessions within one window. It’s a great way to multitask as you can run multiple programs side-by-side without needing to switch between different windows or tabs. In addition, Tmux sessions are persistent. This means you can disconnect from them, leave them running in the background, and later reconnect from where you left off.

Why use Tmux?

Using Tmux for managing multiple servers has numerous benefits:

  1. Multitasking: Connect to multiple servers side by side in the same terminal window.

  2. Session Management: Detach from and reattach to sessions, allowing long-running tasks to persist beyond connection lifetimes.

  3. Scriptability: Automate regular tasks, like setting up your environment, with Tmux scripting.

Creating a simple tmux session

# To start a new tmux:
tmux

# To start a session named webservers
tmux new -s webservers

Splitting the windows

Once in a session

  1. Press Ctrl-b and then " to split the window horizontally
  2. Press Ctrl-b and then % to split the window vertically
  3. Press Ctrl-b and then x to close the window you are in
  4. Press Ctrl-b and then w to show the windows available in your session

The Tmux Config

Create a .tmux.local in your linux home directory add in

bind-key a set-window-option synchronize-panes\; display-message "synchronize-panes is now #{?pane_synchronized,on,off}"

This will allow you to press Ctrl-b and then a to go into synchronizing the panes and your input so you can type in all panes at once

a good tmux config can be found here which will get you started gpakosz - github - tmux config

Handling Multiple Servers with Tmux

Consider a scenario where you regularly connect to several servers via SSH. Opening multiple terminal windows or tabs and initiating separate SSH sessions can be tedious and disorganized.

Here’s where Tmux comes to the rescue. We will use a simple bash script that creates a Tmux session and automatically sets up an SSH connection to each server in separate Tmux panes.

The script is as follows:

#!/bin/bash

SESSION_NAME="my-servers"
SSH_SERVERS=("server1" "server2" "server3" "server4" "server5")

if tmux has-session -t $SESSION_NAME 2>/dev/null; then
    tmux attach -t $SESSION_NAME
    exit
fi

tmux new-session -d -s $SESSION_NAME ssh "${SSH_SERVERS[0]}"
for server in "${SSH_SERVERS[@]:1}"; do
    tmux split-window -t $SESSION_NAME -v "ssh $server"
    tmux select-layout -t $SESSION_NAME tiled
done
tmux rename-session -t $SESSION_NAME $SESSION_NAME
tmux attach -t $SESSION_NAME

In this script, replace "server1" "server2" "server3" "server4" "server5" with the hostnames or IP addresses of your servers. Running this script will create a new Tmux session named "my-servers" and connect to the servers specified in the SSH_SERVERS array. If a session with the same name already exists, it will simply attach to it.

You can add this script to your shell’s startup file (e.g., .bashrc for Bash) if you want this setup every time you open a terminal.

Manipulating Tmux Views

Tmux provides various commands to manipulate the layout of your panes. For example, if you want to swap the positions of two panes (say server1 and server2), you can do so with the swap-pane command. Here’s how:

  1. Switch to the pane you want to move by pressing Ctrl-b followed by the arrow key corresponding to the direction of the pane.
  2. Press Ctrl-b and :, then type swap-pane -s %source -t %target and press Enter. Replace source and target with the actual pane numbers. You can find pane numbers by pressing Ctrl-b and q.

Zooming In and Out

Sometimes, you may want to focus on a single pane. Tmux provides a zoom functionality for this purpose:

  1. Navigate to the pane you want to zoom in by pressing Ctrl-b followed by the arrow key.
  2. Press Ctrl-b and z to zoom into a pane. The selected pane will occupy the whole terminal window.
  3. To zoom out and return to the previous layout, press Ctrl-b and z again.

Detaching and Reattaching to Tmux Sessions

If you want to leave a Tmux session running in the background, you can detach from it:

  1. Press Ctrl-b and then d to detach from the current Tmux session. Your session will continue running in the background.
  2. To reattach to a detached session, use the command tmux attach -t session-name in your terminal. Replace session-name with the actual name of your Tmux session.

Saving and Restoring Tmux Sessions

By default, Tmux sessions are not persistent across reboots. However, you can achieve this with the help of a Tmux plugin called tmux-resurrect. Here’s how you can install and use it:

  1. First, install the Tmux Plugin Manager (TPM) by cloning the TPM repository in your home directory:
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
  1. Add the following lines to your ~/.tmux.conf file to enable TPM:
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-resurrect'
run '~/.tmux/plugins/tpm/tpm'
  1. Press Ctrl-b and I to fetch and source the plugins.

Now, you can save and restore your Tmux sessions:

  1. Press Ctrl-b and Ctrl-s to save your current Tmux session.
  2. After a reboot, or whenever you want to restore your session, press Ctrl-b and Ctrl-r.

Please note that tmux-resurrect does not restore SSH sessions or other programs’ state, but it will restore the layout, current directory, command history, etc.



Buy Me a Coffee