As developers, we frequently need to monitor multiple log files (tail -f
), run parallel commands, or maintain different working contexts while connected to remote servers. Opening separate SSH connections for each terminal window becomes particularly painful when:
- Using shared computers without SSH key authentication
- Working with corporate firewalls limiting multiple connections
- Needing to maintain session state during unstable network conditions
Two battle-tested tools solve this problem elegantly:
Option 1: GNU Screen (The Classic)
Basic workflow:
# Start a new screen session
screen -S session_name
# Create new window (Ctrl+a c)
# Switch between windows (Ctrl+a n/p)
# Detach from session (Ctrl+a d)
# Reattach: screen -r session_name
Option 2: tmux (Modern Alternative)
More feature-rich example:
# Start tmux
tmux new -s my_work
# Split panes vertically (Ctrl+b %)
# Split panes horizontally (Ctrl+b ")
# Create new window (Ctrl+b c)
# Navigate panes (Ctrl+b arrow keys)
# Detach (Ctrl+b d)
# Reattach: tmux attach -t my_work
For persistent productivity:
# ~/.tmux.conf customization example
set -g mouse on
set -g base-index 1
bind | split-window -h
bind - split-window -v
Monitoring multiple services simultaneously:
# In tmux/screen:
# Pane 1: Web server logs
tail -f /var/log/nginx/access.log
# Pane 2: Application logs
tail -f /var/log/myapp/error.log
# Pane 3: System metrics
watch -n 5 "free -m && df -h"
Combine with SSH config for maximum efficiency:
# ~/.ssh/config
Host myserver
HostName server.example.com
User myuser
# Automatically attach to existing tmux session
RequestTTY yes
RemoteCommand tmux attach -t work || tmux new -s work
As developers, we frequently need to monitor multiple log files simultaneously using commands like tail -f
. While public key authentication solves the multiple-login problem for local machines, university or public computers often force us to repeatedly enter passwords for each new SSH connection.
The most robust solution is using terminal multiplexers. These tools create persistent sessions that survive disconnections and allow multiple terminal windows within a single SSH connection.
Using tmux (Recommended)
Install tmux if not present:
sudo apt-get install tmux # Debian/Ubuntu
sudo yum install tmux # RHEL/CentOS
Basic tmux workflow:
# Start new session
tmux new -s logsession
# Create new window (Ctrl+b c)
# Switch between windows (Ctrl+b n/p)
# Split vertically (Ctrl+b %)
# Split horizontally (Ctrl+b ")
# Detach session (Ctrl+b d)
# Reattach later: tmux attach -t logsession
Using GNU Screen
For older systems without tmux:
screen -S logsession
# Create new window (Ctrl+a c)
# Switch windows (Ctrl+a n/p)
# Split screen (Ctrl+a S)
# Detach (Ctrl+a d)
# Reattach: screen -r logsession
Configure SSH to reuse existing connections:
# ~/.ssh/config
Host *
ControlMaster auto
ControlPath ~/.ssh/control:%h:%p:%r
ControlPersist 1h
This creates a master connection that subsequent SSH sessions will reuse, avoiding repeated authentications.
For dedicated log monitoring, consider this advanced technique:
# Create named pipe
mkfifo /tmp/multilog
# In first terminal:
tail -f /var/log/nginx/access.log > /tmp/multilog &
# In second terminal:
cat /tmp/multilog | grep "ERROR"
Add this to ~/.tmux.conf for better default behavior:
# Easy prefix (Ctrl+a instead of Ctrl+b)
set -g prefix C-a
unbind C-b
bind C-a send-prefix
# Mouse support
set -g mouse on
# Start window numbering at 1
set -g base-index 1
Remember to reload tmux config (Ctrl+a : then type source-file ~/.tmux.conf
)