A powerful bash script for connecting to multiple Linux hosts simultaneously using tmux. Works within your existing tmux session by splitting the current window into multiple panes. Perfect for system administrators, DevOps engineers, and anyone managing multiple servers.
- Works in Current tmux Window: Splits your current tmux window into multiple panes
- Multiple Connection Methods: Command-line arguments, stdin, or host files
- Synchronized Input: Type commands once, execute on all hosts
- Flexible SSH Commands: Support for different SSH clients (ssh, tsh, etc.)
- Customizable Layouts: Choose from various tmux pane arrangements
- Error Handling: Robust validation and helpful error messages
- Verbose Logging: Optional detailed output for debugging
- tmux: Terminal multiplexer (required)
- SSH client: Any SSH client (ssh, tsh, etc.)
- Bash: Version 4+ recommended
# Install tmux (Ubuntu/Debian)
sudo apt install tmux
# Install tmux (CentOS/RHEL)
sudo yum install tmux
# Install tmux (macOS)
brew install tmux# Make script executable
chmod +x multi-ssh.sh
# Copy to your PATH (optional)
cp multi-ssh.sh /usr/local/bin/
# Start tmux first
tmux
# Then run the script inside tmux
./multi-ssh.sh web1 web2 db1This script must be run from within a tmux session. It will split your current tmux window into multiple panes, one for each host.
# Step 1: Start tmux
tmux
# Step 2: Run the multi-ssh script
./multi-ssh.sh host1 host2 host3multi-ssh.sh [OPTIONS] [HOSTS...] [-- COMMAND]
echo "host1\nhost2" | multi-ssh.sh [OPTIONS] [-- COMMAND]# Simple connection
./multi-ssh.sh server1 server2 server3
# With specific command
./multi-ssh.sh web1 web2 -- htop# From echo
echo -e 'web1\nweb2\ndb1' | ./multi-ssh.sh
# From file
cat hosts.txt | ./multi-ssh.sh
# With command
echo -e 'host1\nhost2' | ./multi-ssh.sh -- 'tail -f /var/log/syslog'Create a hosts.txt file:
web1.example.com
web2.example.com
# This is a comment
db1.example.com
# Empty lines are ignored
app1.example.com
Then use:
cat hosts.txt | ./multi-ssh.sh| Option | Description | Default |
|---|---|---|
-h, --help |
Show help message | - |
-v, --verbose |
Enable verbose output | Off |
-s, --session NAME |
Use specific session name | multi-ssh-$ |
-l, --layout LAYOUT |
Set tmux layout | tiled |
-c, --ssh-cmd CMD |
SSH command to use | ssh -A |
-k, --kill-session |
Kill existing session | Keep existing |
Customize behavior with environment variables:
export SSH_CMD="ssh -o StrictHostKeyChecking=no"
export LAYOUT="even-horizontal"
export SESSION_NAME="my-servers"
export VERBOSE=1# Start tmux first
tmux
# Connect to three web servers
./multi-ssh.sh web1 web2 web3
# Monitor logs across multiple servers
./multi-ssh.sh app1 app2 app3 -- tail -f /var/log/application.log
# Use custom SSH options
SSH_CMD="ssh -i ~/.ssh/prod_key" ./multi-ssh.sh prod1 prod2# Start tmux first
tmux
# Verbose mode with custom layout
./multi-ssh.sh -v -l even-horizontal web1 web2 web3
# Custom SSH command for specific environment
./multi-ssh.sh -c "ssh -o ConnectTimeout=5" host1 host2# Create a production hosts file
cat > prod_hosts.txt << EOF
web1.prod.company.com
web2.prod.company.com
api1.prod.company.com
api2.prod.company.com
EOF
# Start tmux and connect to all production servers
tmux
cat prod_hosts.txt | ./multi-ssh.sh -v
# Quick system check across all servers (in new tmux window)
tmux new-window
cat prod_hosts.txt | ./multi-ssh.sh -- 'uptime && df -h / && free -m'Available layouts for the -l option:
tiled(default): Equal-sized panes in a grideven-horizontal: Horizontal split with equal widthseven-vertical: Vertical split with equal heightsmain-horizontal: Large pane on top, smaller ones belowmain-vertical: Large pane on left, smaller ones on right
Once connected, use these tmux shortcuts:
| Key Combination | Action |
|---|---|
Ctrl+b d |
Detach from session |
Ctrl+b x |
Close current pane |
Ctrl+b : |
Enter tmux command mode |
Ctrl+b z |
Zoom current pane |
Ctrl+b Arrow |
Navigate between panes |
# Disable synchronization
Ctrl+b : set-window-option synchronize-panes off
# Enable synchronization
Ctrl+b : set-window-option synchronize-panes on1. "tmux is required but not installed"
# Install tmux first
sudo apt install tmux # Ubuntu/Debian
sudo yum install tmux # CentOS/RHEL
brew install tmux # macOS2. "This script must be run inside a tmux session"
# Start tmux first
tmux
# Then run your multi-ssh command
./multi-ssh.sh host1 host23. SSH Connection Failures
# Test individual connections first
ssh host1
# Use verbose mode to debug
./multi-ssh.sh -v host1 host2
# Check SSH configuration
ssh -v host14. Permission Denied
# Make script executable
chmod +x multi-ssh.sh
# Check SSH keys
ssh-add -lEnable verbose output for troubleshooting:
# Method 1: Command line option
./multi-ssh.sh -v host1 host2
# Method 2: Environment variable
VERBOSE=1 ./multi-ssh.sh host1 host2# Extract hosts from Ansible inventory
ansible-inventory --list | jq -r '.webservers.hosts[]' | ./multi-ssh.sh
# Or from inventory file
grep -E '^\[webservers\]' -A 10 inventory.ini | grep -v '^\[' | ./multi-ssh.sh# Connect to multiple Docker containers
docker ps --format "table {{.Names}}" | tail -n +2 | xargs -I {} ./multi-ssh.sh {}- SSH Keys: Use SSH keys instead of passwords for automation
- Host Verification: Consider SSH host key verification settings
- Session Names: Avoid predictable session names in shared environments
- Command Logging: Be aware that commands are visible in tmux history
Feel free to submit issues and pull requests. Areas for improvement:
- Connection health checking
- SSH key management
- Host grouping and tagging
- Output logging to files
- Integration with configuration management tools
This tool is provided as-is under the MIT License. Use at your own risk.
Pro Tip: Create aliases for common scenarios:
alias web-ssh='cat ~/config/web_hosts.txt | multi-ssh.sh -s web-servers'
alias db-ssh='cat ~/config/db_hosts.txt | multi-ssh.sh -s database-servers'
alias prod-check='cat ~/config/prod_hosts.txt | multi-ssh.sh -- "uptime && df -h /"'