ใช้ tmux จัดการ Terminal Sessions บน VPS

ใช้ tmux จัดการ Terminal Sessions บน VPS

มิถุนายน 2026 · อ่าน 7 นาที

tmux (terminal multiplexer) คือ tool จำเป็นสำหรับการทำงานบน VPS ช่วยให้รันหลาย terminal พร้อมกันในหน้าต่างเดียว และที่สำคัญที่สุดคือ session ยังคงอยู่หลัง SSH disconnect ทำให้ process ที่กำลังรันอยู่ไม่ตายแม้หลุด connection เช่น backup, database migration หรือ compilation ที่ใช้เวลานาน

ติดตั้ง tmux

sudo apt install tmux -y

# ตรวจ
tmux -V

คำสั่ง Session พื้นฐาน

# สร้าง session ใหม่
tmux new -s mysession

# Detach (ออกโดยไม่ kill) — Ctrl+B แล้ว D
# หรือพิมพ์:
tmux detach

# List sessions ที่รันอยู่
tmux ls

# Attach กลับเข้า session
tmux attach -t mysession
# หรือ
tmux a -t mysession

# Kill session
tmux kill-session -t mysession

Shortcut สำคัญ (prefix = Ctrl+B)

# --- Session ---
Ctrl+B + d        → Detach ออกจาก session
Ctrl+B + $        → Rename session
Ctrl+B + s        → List + switch sessions

# --- Window ---
Ctrl+B + c        → สร้าง window ใหม่
Ctrl+B + n        → ไป window ถัดไป
Ctrl+B + p        → ไป window ก่อนหน้า
Ctrl+B + 0-9      → ไป window หมายเลขนั้น
Ctrl+B + ,        → Rename window

# --- Pane (แบ่งหน้าต่าง) ---
Ctrl+B + %        → แบ่ง Pane แนวตั้ง
Ctrl+B + "        → แบ่ง Pane แนวนอน
Ctrl+B + Arrow    → ย้ายระหว่าง pane
Ctrl+B + x        → ปิด pane ปัจจุบัน
Ctrl+B + z        → Zoom pane เต็มจอ

ตั้ง .tmux.conf (config ส่วนตัว)

nano ~/.tmux.conf
# เปลี่ยน prefix เป็น Ctrl+A (เหมือน screen)
set -g prefix C-a
unbind C-b
bind C-a send-prefix

# Mouse support
set -g mouse on

# เพิ่ม history limit
set -g history-limit 50000

# Window numbering เริ่มจาก 1
set -g base-index 1
setw -g pane-base-index 1

# Status bar
set -g status-right '%Y-%m-%d %H:%M'
set -g status-style bg=black,fg=white
# Apply config โดยไม่ restart
tmux source-file ~/.tmux.conf

Use Case จริงบน VPS

# รัน long-running process โดยไม่กลัว disconnect
tmux new -s backup
./backup-all-databases.sh

# Ctrl+B + D (detach) แล้วปิด SSH ได้เลย
# กลับมา attach ดูผลลัพธ์ทีหลัง

# Monitor หลายอย่างพร้อมกัน
tmux new -s monitor
Ctrl+B + %  # แบ่ง 2 pane
# pane ซ้าย: htop
# pane ขวา: tail -f /var/log/nginx/access.log
Tip: ใช้ tmux new -s deploy -d เพื่อสร้าง session แบบ detached ทันที แล้ว run command ใน session นั้นด้วย tmux send-keys -t deploy "command" Enter เหมาะสำหรับ deploy script อัตโนมัติ

ตรวจสอบว่า tmux ทำงานถูกต้อง

# ดู sessions ทั้งหมด
tmux ls

# ดู process ใน session
tmux list-panes -a -F "#{session_name}: #{pane_pid}"

# ตรวจว่า process ยังรันอยู่หลัง detach
ps aux | grep backup-all-databases

# ดูว่า tmux process เองรันอยู่
pgrep -l tmux
Tip: ตั้ง SSH ServerAliveInterval 60 ใน ~/.ssh/config ฝั่ง client เพื่อป้องกัน SSH timeout ขณะทำงานใน tmux ลดปัญหา connection หลุดบ่อย

แก้ปัญหาที่พบบ่อย

Session หายไปหลัง VPS reboot

tmux session ไม่ survive reboot ถ้าต้องการ process รัน persistent หลัง reboot ใช้ systemd service แทน:

sudo systemctl enable myservice
sudo systemctl start myservice
# ดูบทความ systemd สำหรับรายละเอียด

Copy-paste ไม่ทำงานใน tmux

ต้องใช้ keyboard copy mode แทน mouse เมื่อ mouse mode เปิดอยู่:

# เข้า copy mode
Ctrl+B + [
# ใช้ arrow หรือ vi keys เลือก text
# กด Space เริ่ม selection
# กด Enter copy
# Ctrl+B + ] paste

Pane display ผิดเพี้ยน (character แปลกๆ)

TERM variable ไม่ถูก ตั้ง TERM ใน SSH config หรือ .tmux.conf:

set -g default-terminal "screen-256color"
# หรือ
export TERM=xterm-256color

tmux ช้าเมื่อพิมพ์ (lag)

escape-time default 500ms ทำให้รู้สึกช้า ลดให้น้อยลง:

set -sg escape-time 10  # ใน ~/.tmux.conf

คำถามที่พบบ่อย

tmux กับ screen ต่างกันอย่างไร?

ทั้งคู่เป็น terminal multiplexer แต่ tmux พัฒนาต่อเนื่องกว่า รองรับ pane, modern scripting API และ config ยืดหยุ่นกว่า screen ถูก fork มานานและ develop น้อยลง ทุกวันนี้ tmux คือ choice หลักสำหรับ VPS sysadmin

ใช้ tmux ร่วมกับ Mosh ได้ไหม?

ได้ดีมาก Mosh แก้ปัญหา SSH timeout และ packet loss tmux แก้ปัญหา session persistence ใช้ร่วมกัน: mosh user@vps -- tmux attach ทำให้ทำงานบน VPS ได้ลื่นแม้ internet ไม่เสถียร

Shared terminal กับคนอื่นใน session เดียวกันได้ไหม?

ได้ ผ่าน tmux -S /tmp/shared new -s collab และให้ user อื่น attach ด้วย socket เดียวกัน เหมาะสำหรับ pair programming หรือ live debug กับเพื่อนร่วมทีม