Skip to content
Porter 的流水账
Go back

tmux 入门教程

从 Session、Window、Pane 到日常工作流

记得刚工作的时候就有坐在我旁边的老同事向我介绍过 tmux,我用 CLI 不算少,但是跟整天用 FreeBSD 和 Linux 的人还是不能比,也就一直没有真正学过。最近 CLI 因为 coding agent 的缘故用得又多起来了,有时候也要远程,所以想趁这个机会把这门手艺再捡起来。在请教 AI 答疑解惑的过程中问了一些问题,这个过程结束之后就让 AI 结合我的提问来写一篇 tmux 新手笔记,这就是这篇东西的来历。

tmuxterminal multiplexer 的缩写,中文通常译为“终端复用器”。

它主要解决两个问题:

  1. 一个终端连接里同时运行多个终端任务
  2. SSH 连接断开后,让远程主机上的程序继续运行

如果你经常通过 SSH 管理服务器、运行长时间脚本、查看日志、启动开发服务,tmux 是非常实用的基础工具。


一、tmux 到底解决了什么问题?

假设你从主机 A 连接到服务器 B:

主机 A

   │ SSH

服务器 B
   └── python long_task.py

如果你直接在普通 SSH shell 中运行程序,那么当 SSH 连接断开时:

tmux 在服务器 B 上创建了一个独立于 SSH 连接的后台终端环境:

服务器 B

├── tmux server
│   └── tmux session
│       └── shell
│           └── python long_task.py

└── SSH 连接只是临时显示这个 session

SSH 断开时,消失的只是 tmux 的显示端,也就是 client。

服务器 B 上的:

仍然继续运行。

你之后可以从主机 A,甚至另一台主机 C,再次 SSH 到 B,然后重新连接原来的 tmux session。


二、理解 tmux 的四层结构

tmux 最容易让初学者困惑的地方,是以下几个概念:

它们的层级关系是:

tmux Server

├── Session: project-a
│   ├── Window: editor
│   │   ├── Pane: vim
│   │   └── Pane: shell
│   │
│   └── Window: server
│       ├── Pane: application
│       └── Pane: logs

└── Session: production
    ├── Window: web
    └── Window: database

1. Server:tmux 的后台进程

当你第一次启动 tmux 时,tmux 会在当前主机上启动一个后台 server。

这个 server 负责管理:

通常,一个 Linux 或 macOS 用户只运行一个默认的 tmux server。

你平时很少需要直接操作 server,只需要知道:

Session、Window 和 Pane 都由 tmux server 管理。


2. Session:完整工作空间

Session 是 tmux 的最高层工作空间。

例如:

Session: foo
Session: production
Session: experiment

每个 Session 可以包含多个 Window。

Session 适合用来隔离:

例如:

Session: foo
├── Window: code
├── Window: backend
├── Window: logs
└── Window: database

Session: production
├── Window: web-server
├── Window: worker
└── Window: monitoring

你可以 detach 一个 Session,再 attach 另一个 Session。

不同 Session 中的程序互不影响。


3. Window:Session 里的标签页

Window 可以理解成一个 Session 内的“终端标签页”。

例如:

Session: work

Window 0: editor
Window 1: server
Window 2: logs
Window 3: database

每次屏幕上主要显示一个 Window。

你可以快速在这些 Window 之间切换,就像在浏览器标签页之间切换。

Window 与 Session 看起来有一定重叠,因为它们都能用来组织终端任务。但它们的使用层级不同:

一个简单判断标准是:

对于轻度用户,一个 Session 加几个 Window 通常就够了。


4. Pane:Window 中的分屏

Pane 是 Window 内部的一个终端区域。

例如:

Window: development

┌─────────────────────────────┐
│ 编辑器                       │
├──────────────┬──────────────┤
│ 开发服务器     │ 实时日志      │
└──────────────┴──────────────┘

这个 Window 中有三个 Pane。

每个 Pane 通常运行一个:


三、安装 tmux

macOS

macOS 默认通常不包含 tmux,可以通过 Homebrew 安装:

brew install tmux

确认安装:

tmux -V

Debian 或 Ubuntu

sudo apt install tmux

Fedora

sudo dnf install tmux

Arch Linux

sudo pacman -S tmux

四、第一次启动 tmux

直接执行:

tmux

tmux 会:

  1. 启动 tmux server;
  2. 创建一个新的 Session;
  3. 在 Session 中创建一个 Window;
  4. 在 Window 中创建一个 Pane;
  5. 在 Pane 中启动 shell。

屏幕底部通常会出现状态栏,例如:

[0] 0:bash*

其中可能表示:

不过更推荐给 Session 起一个有意义的名字:

tmux new-session -s work

可以简写为:

tmux new -s work

五、tmux 快捷键的基本规则

tmux 的默认快捷键前缀是:

Ctrl+b

大多数 tmux 快捷键不是同时按三个键,而是分两步:

  1. 按住 Ctrl,按一下 b
  2. 松开;
  3. 再按具体命令键。

例如 detach:

Ctrl+b
松开
d

通常写作:

Ctrl+b d

它的意思不是同时按住 Ctrl+b+d

后文中的所有快捷键都遵循这个规则。


六、Session 的常用操作

1. 创建命名 Session

tmux new -s work

创建之后会立即进入这个 Session。


2. 主动离开 Session,但保持程序运行

在 tmux 中按:

Ctrl+b d

就是 detach

你会看到类似:

[detached (from session work)]

此时:

接下来可以安全地执行:

exit

断开 SSH。


3. 查看现有 Session

tmux ls

也可以写成:

tmux list-sessions

示例:

work: 3 windows
production: 2 windows

4. 重新进入 Session

tmux attach -t work

可以简写为:

tmux a -t work

5. 创建或进入 Session

日常最实用的写法之一是:

tmux new -A -s work

含义是:

这可以作为固定登录命令。

例如:

ssh server
tmux new -A -s work

6. 在 Session 之间切换

在 tmux 内按:

Ctrl+b s

会显示 Session 列表,可以选择另一个 Session。

也可以按:

Ctrl+b (

切换到上一个 Session。

Ctrl+b )

切换到下一个 Session。


7. 重命名当前 Session

Ctrl+b $

或者在 shell 中执行:

tmux rename-session -t old-name new-name

8. 关闭一个 Session

从 tmux 外部执行:

tmux kill-session -t work

这会关闭 Session 中的所有:

因此应谨慎使用。

关闭所有 tmux Session 和整个 server:

tmux kill-server

这会结束当前用户的全部 tmux 工作空间。


七、Window 的常用操作

1. 创建新 Window

Ctrl+b c

c 可以理解为 create。


2. 查看和选择 Window

Ctrl+b w

会打开 Window 列表。


3. 切换到下一个 Window

Ctrl+b n

n 表示 next。


4. 切换到上一个 Window

Ctrl+b p

p 表示 previous。


5. 切换到最近使用的 Window

Ctrl+b l

l 表示 last。

这类似在两个标签页之间来回切换。


6. 按编号进入 Window

Ctrl+b 0
Ctrl+b 1
Ctrl+b 2

默认 Window 编号通常从 0 开始。


7. 重命名当前 Window

Ctrl+b ,

建议为 Window 使用有意义的名字,例如:

这样底部状态栏会更容易理解。


8. 关闭当前 Window

最自然的方式是在 Window 的最后一个 Pane 中退出 shell:

exit

也可以使用:

Ctrl+b &

tmux 会要求确认是否关闭 Window。

这会终止该 Window 中的所有 Pane 和程序,应谨慎使用。


八、Pane 的常用操作

1. 左右分屏

Ctrl+b %

结果类似:

┌──────────────┬──────────────┐
│ Pane 1       │ Pane 2       │
└──────────────┴──────────────┘

2. 上下分屏

Ctrl+b "

结果类似:

┌─────────────────────────────┐
│ Pane 1                      │
├─────────────────────────────┤
│ Pane 2                      │
└─────────────────────────────┘

注意这里使用的是双引号键。


3. 在 Pane 之间移动

Ctrl+b 方向键

例如:

Ctrl+b ←
Ctrl+b →
Ctrl+b ↑
Ctrl+b ↓

4. 显示 Pane 编号

Ctrl+b q

屏幕上会短暂显示每个 Pane 的编号。

在编号仍显示时按数字,可以跳到对应 Pane。


5. 关闭当前 Pane

在 Pane 中退出当前 shell:

exit

也可以按:

Ctrl+b x

tmux 会要求确认。


6. 放大当前 Pane

Ctrl+b z

当前 Pane 会临时占满整个 Window。

再次按:

Ctrl+b z

恢复原来的分屏布局。

这在查看日志、阅读代码或操作全屏程序时很方便。


7. 调整 Pane 大小

按前缀后按住 Ctrl 加方向键:

Ctrl+b Ctrl+←
Ctrl+b Ctrl+→
Ctrl+b Ctrl+↑
Ctrl+b Ctrl+↓

不同终端或配置中的行为可能略有不同。

也可以进入 tmux 命令模式后精确调整:

Ctrl+b :

然后输入:

resize-pane -L 5
resize-pane -R 5
resize-pane -U 3
resize-pane -D 3

8. 切换 Pane 布局

Ctrl+b Space

tmux 会在几种预设布局之间切换,例如:


九、exit 到底会关闭什么?

这是初学者最需要理解的行为之一。

exit 本质上退出的是当前 Pane 中的 shell。

tmux 会根据层级自动清理空容器。

情况一:Window 中还有其他 Pane

Window
├── Pane 1
└── Pane 2  ← 在这里执行 exit

结果:


情况二:这是 Window 的最后一个 Pane

Session
├── Window 1
└── Window 2
    └── 唯一 Pane ← 执行 exit

结果:


情况三:这是 Session 的最后一个 Window

Session: work
└── 唯一 Window
    └── 唯一 Pane ← 执行 exit

结果:

如果 tmux server 里还有其他 Session,它们不受影响。


情况四:这是最后一个 Session

如果 tmux server 只剩:

tmux Server
└── 唯一 Session
    └── 唯一 Window
        └── 唯一 Pane

在其中执行 exit 后:

所以:

exit 不会直接杀掉整个 tmux;它只是退出当前 shell。只有当这个 shell 恰好是最后一个 Pane、最后一个 Window、最后一个 Session 时,整个 tmux server 才会结束。


十、Detach、Exit 和 SSH 断线的区别

Detach

Ctrl+b d

含义:

离开 tmux 的显示界面,但保留 Session 和其中的程序。

这是你准备退出 SSH 时最推荐的操作。


Exit

exit

含义:

退出当前 Pane 中的 shell。

它可能进一步导致:

所以不要把 exit 当成 detach 使用。


直接关闭 SSH

如果你已经在 tmux 中,即使:

tmux 通常也会自动把该 client 视为 detached。

Session 和其中的程序仍然继续运行。

主动离开时仍建议使用:

Ctrl+b d

这样过程更明确,也可以确认 detach 已经成功。


十一、机器重启后会发生什么?

tmux 可以跨 SSH 断线存在,但不能跨主机重启保持进程。

如果服务器 B 发生:

sudo reboot

或者断电,那么:

重启后执行:

tmux ls

通常会看到:

no server running

原因很简单:

tmux 本身只是运行在操作系统中的普通进程。操作系统重启时,进程表会被清空。


tmux-resurrect 能恢复什么?

tmux-resurrect 等插件可以保存并恢复:

但它不能完整保存普通进程的内存状态。

例如一个 Python 任务已经运行了 18 小时,机器重启后,插件最多重新执行:

python task.py

不能保证从第 18 小时的位置继续。

真正需要跨重启继续的计算任务,应由程序自己实现:


十二、tmux 和 systemd、launchd 的分工

tmux 适合管理交互式工作环境,例如:

tmux 不应该代替正式的服务管理器。

长期服务应该使用:

例如,下面这些稳定服务通常更适合交给 systemd:

systemd 可以提供:

tmux 可以作为操作工作台,用来查看:

journalctl -fu my-service

因此可以这样理解:

tmux 管理“人的交互式终端工作空间”,systemd 管理“机器上的长期服务生命周期”。


十三、最常见的 tmux 工作流

工作流一:运行一个长时间任务

SSH 到服务器:

ssh user@server

创建 Session:

tmux new -s training

运行任务:

python train.py

准备离开时:

Ctrl+b d

然后退出 SSH:

exit

下次回来:

ssh user@server
tmux attach -t training

工作流二:一个项目使用多个 Window

创建项目 Session:

tmux new -s project

将第一个 Window 重命名为:

Ctrl+b ,

输入:

editor

创建第二个 Window:

Ctrl+b c

重命名为:

server

运行:

uvicorn app:app --reload

创建第三个 Window:

Ctrl+b c

重命名为:

logs

运行:

tail -f app.log

现在结构是:

Session: project
├── Window: editor
├── Window: server
└── Window: logs

通过下面的快捷键切换:

Ctrl+b n
Ctrl+b p
Ctrl+b 0
Ctrl+b 1
Ctrl+b 2

工作流三:一个 Window 使用多个 Pane

例如在 server Window 中:

Ctrl+b %

左右分屏。

左侧运行服务:

python app.py

右侧查看日志:

tail -f app.log

再对右侧执行上下分屏:

Ctrl+b "

在新 Pane 中查看资源:

htop

得到类似布局:

┌────────────────┬────────────────┐
│ application    │ logs           │
│                ├────────────────┤
│                │ htop           │
└────────────────┴────────────────┘

工作流四:同时管理多个项目

tmux new -s project-a

detach:

Ctrl+b d

再创建:

tmux new -s project-b

查看:

tmux ls

结果类似:

project-a: 3 windows
project-b: 2 windows

进入指定项目:

tmux attach -t project-a

或者在 tmux 中:

Ctrl+b s

选择 Session。


工作流五:一个命令自动创建或连接工作环境

tmux new -A -s work

这是日常使用非常方便的命令。

还可以在 shell 配置中设置 alias:

alias twork='tmux new -A -s work'

以后 SSH 到服务器后只需要:

twork

十四、复制和滚动历史输出

普通终端的鼠标滚动在 tmux 中有时会受到限制,因为 tmux 自己维护每个 Pane 的历史缓冲区。

进入复制模式

Ctrl+b [

进入后可以使用方向键或 Page Up、Page Down 滚动。

退出复制模式:

q

或者:

Esc

不同 tmux 配置中,复制键可能采用 Emacs 或 vi 风格。


启用鼠标支持

~/.tmux.conf 中加入:

set -g mouse on

重新加载配置后,可以使用鼠标:

对于初学者,启用鼠标通常会让 tmux 更容易使用。


十五、tmux 命令模式

按:

Ctrl+b :

会进入 tmux 命令模式。

可以输入 tmux 内部命令,例如:

new-window
split-window -h
rename-window logs
kill-pane
list-sessions

命令行中的:

tmux <command>

和 tmux 内部命令模式里的命令基本属于同一套命令系统。

例如:

tmux list-sessions

在 tmux 内可以写成:

Ctrl+b :
list-sessions

十六、推荐的基础配置

创建或编辑:

~/.tmux.conf

可以从下面这份简单配置开始:

# 启用鼠标
set -g mouse on

# 增加历史输出行数
set -g history-limit 100000

# Window 和 Pane 编号从 1 开始
set -g base-index 1
setw -g pane-base-index 1

# 关闭 Window 后重新整理编号
set -g renumber-windows on

# 缩短 Esc 等按键的延迟
set -sg escape-time 10

# 使用更现代的终端颜色
set -g default-terminal "tmux-256color"

# 更容易记忆的分屏键
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"

# 新 Pane 继承当前工作目录
bind % split-window -h -c "#{pane_current_path}"
bind '"' split-window -v -c "#{pane_current_path}"

# 重新加载配置
bind r source-file ~/.tmux.conf \; display-message "tmux.conf reloaded"

重新加载配置:

Ctrl+b r

或者在 shell 中执行:

tmux source-file ~/.tmux.conf

十七、判断当前是否在 tmux 中

执行:

echo "$TMUX"

如果有类似输出:

/tmp/tmux-1000/default,1234,0

说明当前 shell 位于 tmux 中。

如果输出为空,说明当前 shell 不在 tmux 中。

在脚本里可以这样判断:

if [ -n "$TMUX" ]; then
    echo "inside tmux"
else
    echo "outside tmux"
fi

十八、避免意外嵌套 tmux

如果你已经在一个 tmux Session 里,又执行:

tmux

tmux 通常会提示不建议嵌套,例如:

sessions should be nested with care

因为此时会出现:

外层 tmux
└── 内层 tmux

两个 tmux 都使用相同的默认前缀 Ctrl+b,快捷键很容易混乱。

通常应该:

而不是在现有 tmux 中再次启动 tmux。

查看其他 Session:

Ctrl+b s

十九、多台机器上的 tmux 是相互独立的

tmux Session 属于运行它的那台主机。

例如:

主机 A 上的 tmux
主机 B 上的 tmux
主机 C 上的 tmux

它们是三套完全独立的 tmux server 和 Session。

如果你从 A SSH 到 B,然后在 B 上启动 tmux,那么 Session 保存在 B 的内存中。

之后从 C SSH 到 B,也可以连接这个 Session:

ssh user@B
tmux attach -t work

但如果你只在 A 上执行 tmux ls,看不到 B 上的 Session。

必须先进入 B:

ssh user@B
tmux ls

二十、多个客户端连接同一个 Session

你可以从两台机器同时 attach 同一个 Session。

例如主机 A:

ssh user@B
tmux attach -t work

主机 C:

ssh user@B
tmux attach -t work

此时两个客户端可以同时看到同一个 tmux Session。

这可以用于:

但是窗口尺寸可能互相影响,而且双方都能输入命令,所以需要注意。


强制接管 Session

如果希望断开其他 client,再由当前 client 连接:

tmux attach -d -t work

其中 -d 会 detach 其他已连接的 client。


二十一、常见误区

误区一:Session 就是 Window

不是。

Session
└── Window
    └── Pane

Session 是工作空间,Window 是标签页,Pane 是分屏。


误区二:执行 exit 就是 detach

不是。


误区三:SSH 断开会终止 tmux 程序

一般不会。

SSH client 消失后,tmux Session 仍在远程主机运行。


误区四:tmux 可以跨服务器重启保存程序

不能。

服务器重启后,tmux 和其中的普通进程都会消失。


误区五:所有长期服务都应该放进 tmux

不推荐。

正式长期服务应由 systemd、launchd 或容器平台管理。

tmux 更适合交互式任务和开发工作流。


误区六:开很多 Pane 总比开 Window 好

不一定。

Pane 太多时,每个区域会变得很小。

通常:


二十二、推荐的组织原则

一个实用的经验是:

Session:按项目或环境划分

foo
production
experiments

Window:按任务类型划分

editor
server
logs
database
monitor

Pane:按需要同时观察的内容划分

例如 server Window:

application | logs
            | resource monitor

不要因为 tmux 能无限分屏,就把所有内容塞进一个 Window。


二十三、最值得记住的快捷键

只掌握下面这些,就已经能覆盖绝大多数日常场景:

Ctrl+b d       detach

Ctrl+b c       创建 Window
Ctrl+b n       下一个 Window
Ctrl+b p       上一个 Window
Ctrl+b w       Window 列表
Ctrl+b ,       重命名 Window

Ctrl+b %       左右分屏
Ctrl+b "       上下分屏
Ctrl+b 方向键  切换 Pane
Ctrl+b z       放大或还原 Pane
Ctrl+b x       关闭 Pane

Ctrl+b s       Session 列表
Ctrl+b [       进入滚动和复制模式
Ctrl+b :       命令模式
Ctrl+b ?       查看全部快捷键

常用 shell 命令:

tmux new -s work
tmux new -A -s work
tmux ls
tmux attach -t work
tmux kill-session -t work

二十四、一套适合初学者的完整实践

可以按下面的步骤练习一次。

1. 创建 Session

tmux new -s demo

2. 重命名第一个 Window

Ctrl+b ,

输入:

shell

3. 左右分屏

Ctrl+b %

4. 在 Pane 之间移动

Ctrl+b ←
Ctrl+b →

5. 创建第二个 Window

Ctrl+b c

重命名为:

logs

6. 在两个 Window 之间切换

Ctrl+b n
Ctrl+b p

7. Detach

Ctrl+b d

8. 查看 Session

tmux ls

9. 重新连接

tmux attach -t demo

10. 关闭其中一个 Window

在该 Window 唯一的 Pane 中:

exit

11. 关闭最后一个 Window 和 Session

在最后一个 Pane 中:

exit

此时会退出 tmux。

完成这套练习后,你就已经理解了 tmux 的核心使用模型。


二十五、最终心智模型

使用 tmux 时,始终记住下面这张结构图:

主机
└── tmux Server
    ├── Session:一个项目或工作空间
    │   ├── Window:工作空间中的标签页
    │   │   ├── Pane:标签页中的终端分屏
    │   │   └── Pane
    │   └── Window

    └── Session

以及三个最重要的边界:

  1. SSH 断开不会终止 tmux Session。
  2. 服务器重启会终止 tmux 和其中的普通进程。
  3. Ctrl+b d 是保留工作的 detach;exit 是退出当前 shell。

对于大多数用户,推荐从下面的方式开始:

一个项目一个 Session
一个任务类型一个 Window
只有需要同时查看时才使用多个 Pane

日常登录服务器时使用:

tmux new -A -s work

离开时使用:

Ctrl+b d

只要掌握这套模型和工作流,就已经能够处理 tmux 的绝大多数日常使用场景。


Share this post on:

Next Post
用 Whisper.cpp 做 ASR