SSH and tmux: Essential Tools for Remote Development
Introduction
Secure Shell (SSH) and tmux are essential tools for modern remote development and server management. SSH provides secure access to remote systems, while tmux enables persistent terminal sessions that survive network disconnections. Whether you’re managing servers, developing on remote machines, or collaborating with team members, mastering these tools will significantly boost your productivity.
SSH: Secure Remote Access
Choosing an SSH Client
The default SSH client for both macOS and Linux systems is the built-in terminal. For Windows users, several alternatives are available:
- Terminal (Mac and Linux): Connect using
ssh username@host
in the terminal - Windows Options: PuTTY, Bitvise SSH Client, MobaXterm, or Windows Subsystem for Linux (WSL)
- VS Code: Built-in terminal with Remote-SSH extension for seamless development
Essential SSH Tricks
1. Key-Based Authentication
# Generate SSH key pair
ssh-keygen -t ed25519 -C "your_email@example.com"
# Copy public key to remote server
ssh-copy-id user@remote-server
# Or manually copy the key
cat ~/.ssh/id_ed25519.pub | ssh user@remote-server "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
2. Execute Commands Remotely
# Run a single command
ssh user@remote-server "ls -la /home/user"
# Run multiple commands
ssh user@remote-server "cd /project && git pull && make build"
3. Port Forwarding and Tunneling
# Local port forwarding (forward local port to remote)
ssh -L 8888:localhost:8888 user@remote-server
# Remote port forwarding (forward remote port to local)
ssh -R 8888:localhost:8888 user@remote-server
# Dynamic SOCKS proxy
ssh -D 1080 user@remote-server
4. Jupyter Notebook Tunneling
# On remote server
jupyter notebook --no-browser --port=8888 --ip=0.0.0.0
# On local machine
ssh -L 8889:localhost:8888 user@remote-server
# Access via http://localhost:8889 in your browser
tmux: Terminal Multiplexer
What is tmux?
tmux is a terminal multiplexer that allows you to:
- Create persistent terminal sessions that survive network disconnections
- Split terminals into multiple panes
- Create multiple windows within a session
- Share sessions with other users
- Detach and reattach to sessions
Installing tmux
# Ubuntu/Debian
sudo apt update && sudo apt install tmux
# CentOS/RHEL/Fedora
sudo yum install tmux # or sudo dnf install tmux
# macOS
brew install tmux
# Arch Linux
sudo pacman -S tmux
Basic tmux Commands
Session Management
# Start new session
tmux
# Start named session
tmux new-session -s mysession
# List sessions
tmux list-sessions # or tmux ls
# Attach to session
tmux attach-session -t mysession # or tmux a -t mysession
# Detach from session
# Press Ctrl+b, then d
Window Management
# Create new window
Ctrl+b, c
# Switch between windows
Ctrl+b, n # next window
Ctrl+b, p # previous window
Ctrl+b, <number> # switch to specific window
# Rename current window
Ctrl+b, ,
# List windows
Ctrl+b, w
Pane Management
# Split pane horizontally
Ctrl+b, "
# Split pane vertically
Ctrl+b, %
# Switch between panes
Ctrl+b, <arrow-keys>
# Resize panes
Ctrl+b, Ctrl+<arrow-keys>
# Close current pane
Ctrl+b, x
Advanced tmux Features
1. Copy Mode
# Enter copy mode
Ctrl+b, [
# Navigate with arrow keys or vim keys
# Press Space to start selection
# Press Enter to copy
# Paste with Ctrl+b, ]
2. tmux Configuration
Create ~/.tmux.conf
for custom settings:
# Enable mouse support
set -g mouse on
# Set base index to 1
set -g base-index 1
setw -g pane-base-index 1
# Increase scrollback buffer
set -g history-limit 10000
# Enable vi keys
setw -g mode-keys vi
# Status bar customization
set -g status-bg black
set -g status-fg white
set -g status-left '#[fg=green]#S'
3. Session Sharing
# Create shared session
tmux new-session -s shared -d
# Allow others to attach
tmux attach-session -t shared
# Other users can attach with
tmux attach-session -t shared
Practical tmux Workflows
1. Development Environment
# Start development session
tmux new-session -s dev -d
# Split into multiple panes
tmux split-window -h # Split vertically
tmux split-window -v # Split horizontally
# Arrange panes for development
# Left: Code editor
# Top-right: Terminal for running tests
# Bottom-right: Git status and logs
2. Server Monitoring
# Create monitoring session
tmux new-session -s monitor -d
# Split into monitoring panes
tmux split-window -h
tmux split-window -v
# In different panes:
# htop
# iotop
# netstat -tulpn
# tail -f /var/log/syslog
3. Collaborative Debugging
# Create shared debugging session
tmux new-session -s debug -d
# Share session with team member
tmux attach-session -t debug
# Both users can see the same terminal
# Useful for pair programming or debugging
Integration: SSH + tmux
Best Practices for Remote Development
- Always use tmux for long-running processes
# Start tmux session on remote server ssh user@server "tmux new-session -s work -d" # Attach to session ssh user@server -t "tmux attach-session -t work"
- Use tmux for build processes
# Start build in tmux tmux new-session -s build -d tmux send-keys -t build "make build" Enter # Detach and let it run tmux detach-client -s build # Check progress later tmux attach-session -t build
- Persistent development sessions
# Create development session tmux new-session -s dev -d # Set up your development environment tmux send-keys -t dev "cd /project" Enter tmux send-keys -t dev "vim src/main.py" Enter # Detach and reconnect later tmux detach-client -s dev
Troubleshooting Common Issues
SSH Connection Drops
# Add to ~/.ssh/config
Host *
ServerAliveInterval 60
ServerAliveCountMax 3
TCPKeepAlive yes
tmux Session Recovery
# List all sessions
tmux list-sessions
# Attach to specific session
tmux attach-session -t session_name
# Kill broken session
tmux kill-session -t session_name
Conclusion
SSH and tmux form a powerful combination for remote development. SSH provides secure access to remote systems, while tmux ensures your work persists through network interruptions. By mastering these tools, you can:
- Work efficiently on remote servers
- Maintain persistent development sessions
- Collaborate effectively with team members
- Handle network interruptions gracefully
- Build robust remote development workflows
Start with the basic commands and gradually incorporate more advanced features into your daily workflow. The investment in learning these tools will pay dividends in your productivity and development experience.