Managing multiple remote servers from separate Terminal windows quickly becomes messy. A cleaner approach is to use tmux, which lets you divide one Terminal window into multiple interactive panes.
In this tutorial, we will build a reusable three-pane workspace with:
- One local Mac terminal
- One SSH session to
dm-tokyo-m4-1 - One SSH session to
dm-tokyo-m4-2 - Visible pane labels
- Mouse-based pane switching
- A short
m4command to launch the workspace - Safe handling when the command is run from inside an existing tmux session
The final layout will look like this:
+------------------------+------------------------+
| | dm-tokyo-m4-1 |
| MacBook Local | ci@172.16.11.91 |
| | |
| +------------------------+
| | dm-tokyo-m4-2 |
| | ci@172.16.11.222 |
+------------------------+------------------------+
1. What Is tmux?
tmux stands for terminal multiplexer.
It allows you to:
- Run multiple terminal sessions inside one window
- Divide a Terminal window into panes
- Keep commands running after disconnecting
- Reconnect to existing sessions
- Switch between local and remote terminals quickly
- Manage several servers from one screen
tmux is especially useful for DevOps engineers, system administrators, developers, and anyone managing multiple remote machines.
2. Use Cases
This setup is useful when you need to:
Monitor multiple servers
You can keep logs, system metrics, and application processes visible across several servers.
Compare server configurations
Run the same command on two machines and compare their outputs side by side.
Manage CI runners
Keep one pane for your local Mac and one pane for each GitHub Actions runner or build machine.
Troubleshoot distributed systems
View logs from multiple services without switching Terminal windows.
Perform deployments
Use one pane for deployment commands and other panes for application logs or health checks.
Keep a persistent workspace
Detach from tmux without stopping your SSH connections, then reconnect later.
3. Prerequisites
You need:
- macOS
- Homebrew
- Access to the remote servers
- SSH access using the
ciuser - Passwordless SSH configured, preferably using SSH keys
The servers used in this example are:
172.16.11.91 dm-tokyo-m4-1
172.16.11.222 dm-tokyo-m4-2
Code language: CSS (css)
4. Install tmux on macOS
Install tmux using Homebrew:
brew install tmux
Verify the installation:
tmux -V
Example output:
tmux 3.5a
Code language: CSS (css)
5. Test SSH Access
Before creating the tmux workspace, confirm that you can connect to both servers:
ssh ci@172.16.11.91
Code language: CSS (css)
And:
ssh ci@172.16.11.222
Code language: CSS (css)
The SSH syntax must include the @ symbol.
Correct:
ssh ci@172.16.11.91
Code language: CSS (css)
Incorrect:
ssh ci172.16.11.91
Code language: CSS (css)
The second command tries to resolve ci172.16.11.91 as a hostname and will fail.
6. Optional: Configure Passwordless SSH
Check whether you already have an SSH key:
ls -l ~/.ssh/id_ed25519 ~/.ssh/id_ed25519.pub
Code language: JavaScript (javascript)
If the key does not exist, create one:
ssh-keygen -t ed25519 -C "rajesh@DM-GYK3HC26H7"
Code language: JavaScript (javascript)
Press Enter to accept the default location:
/Users/rajesh/.ssh/id_ed25519
For completely passwordless login, leave the passphrase empty by pressing Enter twice.
Copy the key to the first server:
cat ~/.ssh/id_ed25519.pub | ssh ci@172.16.11.91 \
'mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys'
Code language: JavaScript (javascript)
Copy it to the second server:
cat ~/.ssh/id_ed25519.pub | ssh ci@172.16.11.222 \
'mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys'
Code language: JavaScript (javascript)
Test both connections again:
ssh ci@172.16.11.91
ssh ci@172.16.11.222
Code language: CSS (css)
They should connect without requesting the remote account password.
7. Optional: Create Friendly SSH Hostnames
Create or edit your SSH configuration:
vi ~/.ssh/config
Code language: JavaScript (javascript)
Add:
Host dm-tokyo-m4-1
HostName 172.16.11.91
User ci
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
Host dm-tokyo-m4-2
HostName 172.16.11.222
User ci
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
Code language: JavaScript (javascript)
Secure the file:
chmod 600 ~/.ssh/config
Code language: JavaScript (javascript)
You can now connect using:
ssh dm-tokyo-m4-1
And:
ssh dm-tokyo-m4-2
This makes the tmux script easier to read and maintain.
8. Create the tmux Configuration
Create the tmux configuration file:
vi ~/.tmux.conf
Add the following configuration:
# Enable mouse support
set -g mouse on
# Show pane titles at the top
set -g pane-border-status top
# Display the custom pane title
set -g pane-border-format " #{pane_title} "
# Increase scrollback history
set -g history-limit 50000
# Start pane and window numbering at 1
set -g base-index 1
setw -g pane-base-index 1
# Reduce escape-key delay
set -sg escape-time 10
# Keep windows numbered sequentially
set -g renumber-windows on
Code language: PHP (php)
Reload the configuration:
tmux source-file ~/.tmux.conf
If you are not currently inside tmux, the configuration will be loaded automatically the next time tmux starts.
9. What the tmux Configuration Does
Mouse support
set -g mouse on
Code language: JavaScript (javascript)
Allows you to:
- Click a pane to select it
- Resize panes by dragging borders
- Scroll inside panes using the mouse wheel
- Select windows using the status bar
Pane titles
set -g pane-border-status top
set -g pane-border-format " #{pane_title} "
Code language: PHP (php)
Displays names such as:
MacBook Local
dm-tokyo-m4-1
dm-tokyo-m4-2
Scrollback history
set -g history-limit 50000
Code language: JavaScript (javascript)
Keeps more terminal output available for scrolling and troubleshooting.
Pane numbering
set -g base-index 1
setw -g pane-base-index 1
Code language: JavaScript (javascript)
Starts window and pane numbers from 1 instead of 0.
Faster keyboard response
set -sg escape-time 10
Code language: JavaScript (javascript)
Reduces keyboard delay, which is especially useful when using Vim inside tmux.
10. Create the Three-Pane Workspace Script
Create the script:
vi ~/tmux-mac-ssh.sh
Paste:
#!/bin/bash
set -u
SESSION="mac-ssh"
open_session() {
if [ -n "${TMUX:-}" ]; then
tmux switch-client -t "$SESSION"
else
tmux attach-session -t "$SESSION"
fi
}
# Reuse the existing session if it already exists
if tmux has-session -t "$SESSION" 2>/dev/null; then
open_session
exit 0
fi
# Create the session with one local Mac pane
tmux new-session -d -s "$SESSION" -c "$HOME"
tmux select-pane -t "$SESSION":1.1 -T "MacBook Local"
# Create the top-right SSH pane
tmux split-window -h -p 50 -t "$SESSION":1.1 \
"ssh dm-tokyo-m4-1"
tmux select-pane -t "$SESSION":1.2 -T "dm-tokyo-m4-1"
# Split the right side into top and bottom panes
tmux split-window -v -p 50 -t "$SESSION":1.2 \
"ssh dm-tokyo-m4-2"
tmux select-pane -t "$SESSION":1.3 -T "dm-tokyo-m4-2"
# Return focus to the local Mac pane
tmux select-pane -t "$SESSION":1.1
open_session
Code language: PHP (php)
Make the script executable:
chmod +x ~/tmux-mac-ssh.sh
11. Script Version Without SSH Aliases
If you do not want to configure ~/.ssh/config, use the server IP addresses directly:
#!/bin/bash
set -u
SESSION="mac-ssh"
open_session() {
if [ -n "${TMUX:-}" ]; then
tmux switch-client -t "$SESSION"
else
tmux attach-session -t "$SESSION"
fi
}
if tmux has-session -t "$SESSION" 2>/dev/null; then
open_session
exit 0
fi
tmux new-session -d -s "$SESSION" -c "$HOME"
tmux select-pane -t "$SESSION":1.1 -T "MacBook Local"
tmux split-window -h -p 50 -t "$SESSION":1.1 \
"ssh ci@172.16.11.91"
tmux select-pane -t "$SESSION":1.2 -T "dm-tokyo-m4-1"
tmux split-window -v -p 50 -t "$SESSION":1.2 \
"ssh ci@172.16.11.222"
tmux select-pane -t "$SESSION":1.3 -T "dm-tokyo-m4-2"
tmux select-pane -t "$SESSION":1.1
open_session
Code language: JavaScript (javascript)
12. Why the Script Checks the TMUX Variable
When you run a tmux command from inside an existing tmux session, attempting to attach another session directly can produce this warning:
sessions should be nested with care, unset $TMUX to force
Code language: PHP (php)
The script avoids nested tmux sessions by checking:
if [ -n "${TMUX:-}" ]; then
Code language: CSS (css)
When already inside tmux, it uses:
tmux switch-client -t "$SESSION"
Code language: JavaScript (javascript)
When running from a normal Terminal window, it uses:
tmux attach-session -t "$SESSION"
Code language: JavaScript (javascript)
This allows the same command to work both inside and outside tmux.
13. Create a Short Terminal Command
Instead of typing the full script path, create an alias named m4.
Add it to your zsh configuration:
echo 'alias m4="$HOME/tmux-mac-ssh.sh"' >> ~/.zshrc
Code language: PHP (php)
Reload the shell configuration:
source ~/.zshrc
Launch the workspace:
m4
14. Alternative: Create a System Command
Instead of a shell alias, create a symbolic link:
sudo mkdir -p /usr/local/bin
sudo ln -sf "$HOME/tmux-mac-ssh.sh" /usr/local/bin/m4
Code language: JavaScript (javascript)
Confirm that /usr/local/bin is in your path:
echo "$PATH"
Code language: PHP (php)
Then run:
m4
The symbolic-link method works from any shell and does not depend on .zshrc.
15. Switching Between Panes
Because mouse mode is enabled, click any pane to select it.
You can also use keyboard shortcuts.
tmux uses a prefix key. By default, the prefix is:
Ctrl+b
Press Ctrl+b, release both keys, and then press the next key.
Move to the next pane
Ctrl+b, then o
Move using arrow keys
Ctrl+b, then Left Arrow
Ctrl+b, then Right Arrow
Ctrl+b, then Up Arrow
Ctrl+b, then Down Arrow
Display pane numbers
Ctrl+b, then q
After the numbers appear, press the pane number.
Maximize the current pane
Ctrl+b, then z
Press the same shortcut again to restore the full layout.
16. Scrolling Inside a Pane
With mouse support enabled, use the mouse wheel or trackpad to scroll.
You can also enter tmux copy mode:
Ctrl+b, then [
Use arrow keys, Page Up, or Page Down to navigate.
Press:
q
to exit copy mode.
17. Resize the Panes
With mouse support enabled, drag the pane border.
Keyboard alternatives:
Ctrl+b, then hold Option and press an arrow key
Depending on the Terminal configuration, you can also use tmux commands:
tmux resize-pane -L 5
tmux resize-pane -R 5
tmux resize-pane -U 3
tmux resize-pane -D 3
18. Detach Without Closing SSH Sessions
To leave the tmux session running:
Ctrl+b, then d
This detaches you from tmux but does not terminate:
- The local shell
- SSH sessions
- Commands running on remote servers
- Logs or monitoring processes
Reconnect using:
m4
Or:
tmux attach-session -t mac-ssh
19. List tmux Sessions
Run:
tmux ls
Example:
mac-ssh: 1 windows (created Tue Jul 14 15:20:00 2026)
Code language: CSS (css)
20. Close a Pane
Select the pane and type:
exit
Code language: PHP (php)
Or use:
Ctrl+b, then x
tmux will ask for confirmation before closing the pane.
21. Restart the Entire Workspace
To destroy the current workspace:
tmux kill-session -t mac-ssh
Start a clean workspace:
m4
You can combine both:
tmux kill-session -t mac-ssh 2>/dev/null
m4
Code language: JavaScript (javascript)
22. Troubleshooting
Error: Could not resolve hostname
Example:
ssh: Could not resolve hostname ci172.16.11.91
Code language: CSS (css)
Cause:
ssh ci172.16.11.91
Code language: CSS (css)
Correct command:
ssh ci@172.16.11.91
Code language: CSS (css)
Error: Sessions should be nested with care
Example:
sessions should be nested with care, unset $TMUX to force
Code language: PHP (php)
This happens when trying to attach a tmux session from inside another tmux session.
Use:
tmux switch-client -t mac-ssh
Code language: JavaScript (javascript)
The script in this tutorial handles this automatically.
Mouse works temporarily but stops after restart
Running this command only changes the current tmux server:
tmux set-option -g mouse on
Code language: JavaScript (javascript)
To make it permanent, add this to ~/.tmux.conf:
set -g mouse on
Code language: JavaScript (javascript)
Then reload:
tmux source-file ~/.tmux.conf
Pane labels are not visible
Confirm that ~/.tmux.conf contains:
set -g pane-border-status top
set -g pane-border-format " #{pane_title} "
Code language: PHP (php)
Reload it:
tmux source-file ~/.tmux.conf
Restart the workspace:
tmux kill-session -t mac-ssh
m4
SSH still asks for a password
Run verbose SSH diagnostics:
ssh -v ci@172.16.11.91
Code language: CSS (css)
Check the local key:
ls -la ~/.ssh/id_ed25519*
Code language: JavaScript (javascript)
Check remote permissions:
ssh ci@172.16.11.91
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
Code language: CSS (css)
Also verify that the public key exists in:
~/.ssh/authorized_keys
Code language: JavaScript (javascript)
SSH connection closes and the pane disappears
By default, when the SSH command exits, the tmux pane also closes.
To keep the pane open after SSH exits, replace:
"ssh dm-tokyo-m4-1"
Code language: JSON / JSON with Comments (json)
with:
"ssh dm-tokyo-m4-1; exec zsh"
Code language: JSON / JSON with Comments (json)
And replace:
"ssh dm-tokyo-m4-2"
Code language: JSON / JSON with Comments (json)
with:
"ssh dm-tokyo-m4-2; exec zsh"
Code language: JSON / JSON with Comments (json)
This opens a local shell in the pane after the SSH connection ends.
23. Recommended Final Files
~/.ssh/config
Host dm-tokyo-m4-1
HostName 172.16.11.91
User ci
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
Host dm-tokyo-m4-2
HostName 172.16.11.222
User ci
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
Code language: JavaScript (javascript)
~/.tmux.conf
set -g mouse on
set -g pane-border-status top
set -g pane-border-format " #{pane_title} "
set -g history-limit 50000
set -g base-index 1
setw -g pane-base-index 1
set -sg escape-time 10
set -g renumber-windows on
Code language: PHP (php)
~/tmux-mac-ssh.sh
#!/bin/bash
set -u
SESSION="mac-ssh"
open_session() {
if [ -n "${TMUX:-}" ]; then
tmux switch-client -t "$SESSION"
else
tmux attach-session -t "$SESSION"
fi
}
if tmux has-session -t "$SESSION" 2>/dev/null; then
open_session
exit 0
fi
tmux new-session -d -s "$SESSION" -c "$HOME"
tmux select-pane -t "$SESSION":1.1 -T "MacBook Local"
tmux split-window -h -p 50 -t "$SESSION":1.1 \
"ssh dm-tokyo-m4-1"
tmux select-pane -t "$SESSION":1.2 -T "dm-tokyo-m4-1"
tmux split-window -v -p 50 -t "$SESSION":1.2 \
"ssh dm-tokyo-m4-2"
tmux select-pane -t "$SESSION":1.3 -T "dm-tokyo-m4-2"
tmux select-pane -t "$SESSION":1.1
open_session
Code language: JavaScript (javascript)
~/.zshrc
alias m4="$HOME/tmux-mac-ssh.sh"
Code language: JavaScript (javascript)
24. Complete Setup Verification
Run:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/config
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
chmod +x ~/tmux-mac-ssh.sh
source ~/.zshrc
Test the SSH aliases:
ssh dm-tokyo-m4-1
ssh dm-tokyo-m4-2
Launch the tmux workspace:
m4
You should now have:
- A local Mac terminal on the left
dm-tokyo-m4-1on the top rightdm-tokyo-m4-2on the bottom right- Visible pane labels
- Mouse-based navigation
- Persistent SSH sessions
- A reusable one-command launch process
Conclusion
A small tmux setup can significantly improve daily server-management work.
Instead of opening several separate Terminal windows, you now have one organized workspace containing:
- Your local Mac shell
- Two remote SSH connections
- Friendly server labels
- Mouse and keyboard navigation
- Persistent sessions
- A simple
m4launcher command
This approach is easy to extend. Additional servers, monitoring panes, log viewers, Kubernetes commands, deployment consoles, or CI-runner sessions can be added later without changing the core workflow.
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals