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:
Multitasking: Connect to multiple servers side by side in the same terminal window.
Session Management: Detach from and reattach to sessions, allowing long-running tasks to persist beyond connection lifetimes.
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
- Press
Ctrl-band then"to split the window horizontally - Press
Ctrl-band then%to split the window vertically - Press
Ctrl-band thenxto close the window you are in - Press
Ctrl-band thenwto 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:
- Switch to the pane you want to move by pressing
Ctrl-bfollowed by the arrow key corresponding to the direction of the pane. - Press
Ctrl-band:, then typeswap-pane -s %source -t %targetand pressEnter. Replacesourceandtargetwith the actual pane numbers. You can find pane numbers by pressingCtrl-bandq.
Zooming In and Out
Sometimes, you may want to focus on a single pane. Tmux provides a zoom functionality for this purpose:
- Navigate to the pane you want to zoom in by pressing
Ctrl-bfollowed by the arrow key. - Press
Ctrl-bandzto zoom into a pane. The selected pane will occupy the whole terminal window. - To zoom out and return to the previous layout, press
Ctrl-bandzagain.
Detaching and Reattaching to Tmux Sessions
If you want to leave a Tmux session running in the background, you can detach from it:
- Press
Ctrl-band thendto detach from the current Tmux session. Your session will continue running in the background. - To reattach to a detached session, use the command
tmux attach -t session-namein your terminal. Replacesession-namewith 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:
- 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
- Add the following lines to your
~/.tmux.conffile to enable TPM:
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-resurrect'
run '~/.tmux/plugins/tpm/tpm'
- Press
Ctrl-bandIto fetch and source the plugins.
Now, you can save and restore your Tmux sessions:
- Press
Ctrl-bandCtrl-sto save your current Tmux session. - After a reboot, or whenever you want to restore your session, press
Ctrl-bandCtrl-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