Terminal Session Persistence on Linux: tmux and screen Mastery
How to maintain persistent terminal sessions using tmux and screen on Linux, including configuration, scripting, and recovery from disconnects.
Introduction
You are running a long build on a remote server via SSH. Your connection drops. When you reconnect, the build is gone. This happens to everyone at least once. Terminal multiplexers like tmux and screen solve this problem by maintaining sessions that persist independently of your SSH connection. This post covers practical tmux and screen usage for development and server management.
Environment
The examples use tmux 3.3a on Ubuntu 22.04 LTS. tmux is the modern choice, but screen is still widely installed. Both are available in standard repositories.
tmux -V
# tmux 3.3a
screen -v
# Screen version 4.09.01 (GNU) 30-Jan-22Problem
SSH connections can drop due to network issues, laptop sleep, VPN disconnections, or server maintenance. Without a terminal multiplexer, anything running in that terminal is terminated along with the connection.
# This will be killed when SSH disconnects
ssh user@server
./long-running-build.sh
# Connection closed. Build progress lost.Analysis
Terminal multiplexers work by creating a session on the server that multiple terminal connections can attach to and detach from. When you detach, the session continues running. When you reattach, you see exactly where you left off.
tmux is generally preferred over screen because:
- Better scripting and configuration
- More intuitive keybindings
- Active development and community support
- Built-in status bar customization
Solution
1. Basic tmux usage
# Start a new session
tmux
# Start a named session
tmux new -s work
# Detach from session (Ctrl+B, then D)
# List sessions
tmux ls
# work: 1 windows (created Mon Jan 25 10:00:00 2025)
# Reattach to a session
tmux attach -t work
# Kill a session
tmux kill-session -t work2. tmux configuration file
# ~/.tmux.conf
# Change prefix to Ctrl+A (easier than Ctrl+B)
unbind C-b
set -g prefix C-a
bind C-a send-prefix
# Enable mouse support
set -g mouse on
# Start windows and panes at 1, not 0
set -g base-index 1
setw -g pane-base-index 1
# Enable 256 color support
set -g default-terminal "screen-256color"
# Increase scrollback buffer
set -g history-limit 50000
# Status bar
set -g status-bg colour235
set -g status-fg colour136
set -g status-left '#[fg=green]#S '
set -g status-right '#[fg=yellow]%Y-%m-%d %H:%M'
# Keybindings for splitting
bind | split-window -h
bind - split-window -v3. tmux for development workflows
# Create a session with pre-configured windows
tmux new -s dev \; \
split-window -h \; \
split-window -v \; \
select-pane -t 1 \; \
send-keys "git status" Enter \; \
select-pane -t 2 \; \
send-keys "npm run dev" Enter \; \
select-pane -t 3 \; \
send-keys "htop" Enter4. tmux scripting for automation
#!/bin/bash
# deploy.sh - Automated deployment with tmux
SESSION="deploy"
tmux new-session -d -s $SESSION
# Window 1: Deploy
tmux send-keys -t $SESSION "cd /opt/app && git pull && npm install && pm2 restart all" Enter
# Window 2: Monitor logs
tmux new-window -t $SESSION -n "logs"
tmux send-keys -t $SESSION:logs "pm2 logs" Enter
# Window 3: System monitoring
tmux new-window -t $SESSION -n "monitor"
tmux send-keys -t $SESSION:monitor "htop" Enter
echo "Deployment started. Attach with: tmux attach -t $SESSION"5. Basic screen usage
# Start a new session
screen -S work
# Detach (Ctrl+A, then D)
# List sessions
screen -ls
# There are screens on:
# 12345.work (Mon Jan 25 10:00:00 2025)
# 12346.logs (Mon Jan 25 10:01:00 2025)
# Reattach
screen -r work
# Reattach or create if it does not exist
screen -R work6. screen configuration
# ~/.screenrc
# Enable 256 color
term screen-256color
# Large scrollback buffer
defscrollback 10000
# Status bar
hardstatus alwayslastline
hardstatus string '%{= kG}[%{G}%H%{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d %{W}%c%{g}]'
# Start with 4 windows
screen -t "shell" 0
screen -t "logs" 1
screen -t "monitor" 2
screen -t "editor" 37. Recover from broken SSH sessions
# If tmux session survives, just reattach
tmux attach -t work
# If screen session survives
screen -r work
# If the session is stuck (e.g., from a different client)
screen -D -r work # Detach other clients and reattach8. tmux with SSH
# Use SSH agent forwarding with tmux
ssh -A user@server
tmux new -s work
# Inside tmux, SSH connections inherit the agent9. Share sessions between users
# Create a shared session
tmux new -s shared -d
chmod 777 /tmp/tmux-1000/default
# Another user can attach
tmux -S /tmp/tmux-1000/default attach -t shared10. tmux plugins
# Install TPM (Tmux Plugin Manager)
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
# Add to ~/.tmux.conf
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'
# Install plugins
# Press Ctrl+B, then I11. Resurrect: save and restore sessions
# Save current session
Ctrl+B, then Ctrl+S
# Restore last saved session
Ctrl+B, then Ctrl+R
# Auto-save every 15 minutes
echo "set -g @continuum-save-interval '15'" >> ~/.tmux.confLessons Learned
Terminal multiplexers are essential for anyone working over SSH. tmux is the modern standard and worth learning properly. The resurrect and continuum plugins provide session persistence across reboots, which is invaluable for long-running development environments. Always configure a comfortable prefix key and enable mouse support. And create a .tmux.conf that matches your workflow rather than relying on defaults.
This blog does not accept any external sponsorships, affiliate marketing, or ad revenue.