ทำไม SysAdmin ต้องใช้ tmux
ผม SSH เข้า servers ทุกวันวันละหลายสิบครั้งก่อนใช้ tmux ทุกครั้งที่ SSH connection หลุดงานที่ทำอยู่ก็หายหมด long-running commands ที่รันค้างไว้ก็ตาย tmux แก้ปัญหานี้ 100% เพราะ session ของ tmux รันบน server ต่อให้ SSH หลุด session ก็ยังอยู่แค่ attach กลับเข้าไปก็ทำงานต่อได้ทันที
tmux ทำได้มากกว่าแค่ keep sessions alive มันเป็น terminal multiplexer ที่ให้คุณแบ่งหน้าจอเป็นหลาย panes ทำงานหลาย windows พร้อมกันและ share session กับคนอื่นได้ด้วยผมใช้ tmux ทุกวันทุก server ตลอด 15 ปีที่ผ่านมา
ติดตั้ง tmux
# Ubuntu/Debian
apt install tmux
# RHEL/Rocky/Alma
dnf install tmux
# ตรวจสอบ version
tmux -V
# tmux 3.4
Sessions — กลุ่มของ Windows
# สร้าง session ใหม่
tmux new -s work
# สร้าง session แบบ detached
tmux new -d -s monitoring
# ดู sessions ทั้งหมด
tmux ls
# work: 3 windows (created Fri Feb 28 10:00:00 2026)
# monitoring: 1 windows (created Fri Feb 28 10:01:00 2026)
# Attach เข้า session
tmux attach -t work
tmux a -t work # shortcut
# Detach จาก session (ไม่ปิด session)
# กด Ctrl+b แล้วกด d
# Kill session
tmux kill-session -t monitoring
# Rename session
# Ctrl+b แล้วกด $
Windows — เหมือน tabs ใน browser
# สร้าง window ใหม่
# Ctrl+b แล้วกด c
# สลับ windows
# Ctrl+b แล้วกด 0-9 (ไปตาม index)
# Ctrl+b แล้วกด n (next)
# Ctrl+b แล้วกด p (previous)
# Ctrl+b แล้วกด w (ดูรายการ windows)
# Rename window
# Ctrl+b แล้วกด ,
# ปิด window
# Ctrl+b แล้วกด &
# หรือพิมพ์ exit
Panes — แบ่งหน้าจอ
# แบ่งหน้าจอแนวตั้ง (ซ้าย-ขวา)
# Ctrl+b แล้วกด %
# แบ่งหน้าจอแนวนอน (บน-ล่าง)
# Ctrl+b แล้วกด "
# สลับ panes
# Ctrl+b แล้วกด ลูกศร (ขึ้น/ลง/ซ้าย/ขวา)
# Ctrl+b แล้วกด o (next pane)
# ปรับขนาด pane
# Ctrl+b ค้าง แล้วกดลูกศร
# Zoom pane (ขยายเต็มจอ / กลับ)
# Ctrl+b แล้วกด z
# ปิด pane
# Ctrl+b แล้วกด x
# หรือพิมพ์ exit
Cheat Sheet
# ทุกคำสั่งเริ่มด้วย Prefix key: Ctrl+b (default)
# แล้วตามด้วย command key
# === Sessions ===
# d Detach
# $ Rename session
# s List sessions
# ( Previous session
# ) Next session
# === Windows ===
# c Create window
# , Rename window
# w List windows
# n Next window
# p Previous window
# 0-9 Go to window N
# & Kill window
# === Panes ===
# % Split vertical
# " Split horizontal
# o Next pane
# z Zoom toggle
# x Kill pane
# { Move pane left
# } Move pane right
# Space Toggle layouts
# === Copy Mode ===
# [ Enter copy mode
# q Exit copy mode
# / Search forward
# ? Search backward
# === Other ===
# : Command prompt
# ? List key bindings
# t Show clock
ไฟล์ config ของผมที่ใช้จริง
# ~/.tmux.conf — tmux configuration by อ. บอม
# เปลี่ยน prefix จาก Ctrl+b เป็น Ctrl+a (เหมือน screen)
unbind C-b
set -g prefix C-a
bind C-a send-prefix
# เริ่มนับ window จาก 1 (ไม่ใช่ 0)
set -g base-index 1
setw -g pane-base-index 1
# Renumber windows เมื่อปิด window
set -g renumber-windows on
# เพิ่ม history limit
set -g history-limit 50000
# Mouse support
set -g mouse on
# 256 colors + true color
set -g default-terminal "tmux-256color"
set -ga terminal-overrides ", xterm-256color:Tc"
# ลด escape time (สำหรับ vim)
set -sg escape-time 0
# Split panes ด้วย | และ -
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
unbind '"'
unbind %
# สลับ panes ด้วย Alt+arrow (ไม่ต้อง prefix)
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D
# Reload config
bind r source-file ~/.tmux.conf \; display "Config reloaded!"
# Copy mode ด้วย vi keys
setw -g mode-keys vi
bind -T copy-mode-vi v send-keys -X begin-selection
bind -T copy-mode-vi y send-keys -X copy-selection-and-cancel
# Status bar
set -g status-position bottom
set -g status-style "bg=#1a1a2e, fg=#C9A94E"
set -g status-left "#[fg=#0A0A0C, bg=#C9A94E, bold] #S "
set -g status-right "#[fg=#9A9690] %Y-%m-%d %H:%M #[fg=#0A0A0C, bg=#C9A94E, bold] #H "
set -g status-left-length 30
set -g status-right-length 50
setw -g window-status-format " #I:#W "
setw -g window-status-current-format "#[fg=#0A0A0C, bg=#1BB91F, bold] #I:#W "
ติดตั้ง TPM (Tmux Plugin Manager)
# ติดตั้ง TPM
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
# เพิ่มใน ~/.tmux.conf
# List of plugins
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'
set -g @plugin 'tmux-plugins/tmux-yank'
# tmux-resurrect — save/restore sessions หลัง reboot
set -g @resurrect-capture-pane-contents 'on'
set -g @resurrect-strategy-nvim 'session'
# tmux-continuum — auto-save sessions ทุก 15 นาที
set -g @continuum-restore 'on'
set -g @continuum-save-interval '15'
# Initialize TMUX plugin manager (ต้องอยู่ท้ายสุดของ tmux.conf)
run '~/.tmux/plugins/tpm/tpm'
# ติดตั้ง plugins: Ctrl+a แล้วกด I (ตัวใหญ่)
# Update plugins: Ctrl+a แล้วกด U
สร้าง Development Environment อัตโนมัติ
#!/bin/bash
# ~/bin/tmux-dev.sh — สร้าง tmux layout สำหรับทำงาน
SESSION="dev"
# ตรวจสอบว่ามี session อยู่แล้วหรือไม่
tmux has-session -t $SESSION 2>/dev/null
if [ $? -eq 0 ]; then
tmux attach -t $SESSION
exit 0
fi
# สร้าง session ใหม่
tmux new-session -d -s $SESSION -n "editor"
# Window 1: Editor
tmux send-keys -t $SESSION:1 "cd ~/projects && vim" C-m
# Window 2: Servers (split 3 panes)
tmux new-window -t $SESSION -n "servers"
tmux send-keys -t $SESSION:2 "ssh web-1" C-m
tmux split-window -h -t $SESSION:2
tmux send-keys "ssh web-2" C-m
tmux split-window -v -t $SESSION:2.2
tmux send-keys "ssh db-1" C-m
# Window 3: Monitoring
tmux new-window -t $SESSION -n "monitor"
tmux send-keys -t $SESSION:3 "htop" C-m
tmux split-window -h -t $SESSION:3
tmux send-keys "watch -n 5 'df -h'" C-m
# Window 4: Logs
tmux new-window -t $SESSION -n "logs"
tmux send-keys -t $SESSION:4 "tail -f /var/log/nginx/access.log" C-m
tmux split-window -v -t $SESSION:4
tmux send-keys "tail -f /var/log/nginx/error.log" C-m
# กลับไป window 1 แล้ว attach
tmux select-window -t $SESSION:1
tmux attach -t $SESSION
tmux สำหรับ Server Monitoring
#!/bin/bash
# ~/bin/tmux-monitor.sh — monitoring dashboard
SESSION="monitor"
tmux new-session -d -s $SESSION -n "dashboard"
# Pane 1: htop (CPU/Memory)
tmux send-keys "htop" C-m
# Pane 2: disk usage
tmux split-window -h
tmux send-keys "watch -n 60 'df -h && echo --- && iostat -x 1 1'" C-m
# Pane 3: network
tmux split-window -v -t 0
tmux send-keys "iftop -n -P" C-m
# Pane 4: logs
tmux split-window -v -t 2
tmux send-keys "journalctl -f --no-hostname -p err" C-m
tmux attach -t $SESSION
tmux กับ screen อันไหนดีกว่า?
tmux ดีกว่า screen ในเกือบทุกด้านมี pane splitting ที่ดีกว่า scripting API ที่ดีกว่า plugin system status bar ที่ customize ได้มากกว่าและ development ที่ active กว่า screen ยังใช้ได้แต่ถ้าเริ่มใหม่ให้ใช้ tmux เลยสิ่งเดียวที่ screen ดีกว่าคือมันติดตั้งมาในหลาย distros โดยอัตโนมัติ
ทำยังไงให้ tmux เปิดอัตโนมัติเมื่อ SSH?
เพิ่มใน ~/.bashrc หรือ ~/.zshrc ของ server:
# Auto-attach tmux เมื่อ SSH
if [ -n "$SSH_CONNECTION" ] && [ -z "$TMUX" ]; then
tmux attach -t default 2>/dev/null || tmux new -s default
fi
Copy/Paste ใน tmux ทำยังไง?
กด Ctrl+b [ เข้า copy mode ใช้ลูกศรหรือ vi keys เลื่อนกด Space เริ่ม select กด Enter copy แล้ว Ctrl+b ] paste ถ้าติดตั้ง tmux-yank plugin ก็ copy ไป system clipboard ได้เลยสำหรับ mouse mode ถ้าเปิด set -g mouse on ก็ลาก select แล้วกด y copy ได้
tmux session หายหลัง server reboot ทำยังไง?
ติดตั้ง tmux-resurrect + tmux-continuum plugins จะ auto-save session ทุก 15 นาทีและ auto-restore เมื่อเปิด tmux ใหม่หลัง reboot มันจะ restore windows, panes, working directories แม้แต่ programs ที่กำลังรันอยู่เป็น plugins ที่ผมแนะนำสำหรับทุกู้คืนร่วมกับการ SSH อย่างปลอดภัยผ่าน WireGuard VPN
สรุป
tmux เป็นเครื่องมือที่ SysAdmin ทุกู้คืนต้องใช้มันช่วยให้ SSH sessions ไม่หายแบ่งหน้าจอทำงานหลายอย่างพร้อมกัน scripting สร้าง workspace อัตโนมัติและ plugins อย่าง resurrect/continuum ทำให้ sessions อยู่รอดแม้ server reboot
เริ่มจากคำสั่งพื้นฐาน sessions, windows, panes แล้วค่อยๆ customize ~/.tmux.conf ตามสไตล์ของตัวเองผมใช้ tmux ทุกวันมา 15 ปีไม่มี SysAdmin คนไหนที่ใช้แล้วเลิกใช้ครับสำหรับ Python automation scripts ที่รัน long-running jobs tmux เป็นคู่หูที่ขาดไม่ได้
