创建与重新连接 tmux 会话

在终端复用器中运行工作,SSH 断线、笔记本休眠、窗口关闭都不会终止正在运行的任务。

Linux终端会话tmux 终端复用低风险
执行 tmux new -s work 创建命名会话,按 Ctrl-b d 脱离,之后在任意终端执行 tmux attach -t work 重新接入。

01命令

命令
$ tmux new -s work        # create session 'work'
tmux attach -t work    # reattach later (even from another SSH login)
# inside tmux: Ctrl-b d  detach, Ctrl-b %  split pane, Ctrl-b d then exit

02作用说明

tmux 的会话由服务端进程维持,客户端断开后任务照常运行——通宵构建或 vim 会话可以熬过掉线的 VPN。所有命令经由前缀键 Ctrl-b 加字母触发:d 脱离,% 和 " 分别左右、上下分屏,tmux ls 可查看会话列表。tmux attach -t work 会从任何能访问该主机的机器恢复到离开时的现场。

03风险

风险低风险
使用条件tmux (sudo apt install tmux / sudo dnf install tmux)
!注意: 同名 tmux 会话不能共存——若提示 duplicate session,说明应改为 attach,或用 tmux kill-session -t work 清理失效会话。

04撤销

撤销
$ tmux kill-session -t work

05来源

  • tmux(1) — Linux man page
    Ubuntu Manpages · technical · 2026-09-17
    依据: tmux man page: 'tmux is a terminal multiplexer' documenting new-session, attach-session, and detach-client.
  • Arch Wiki: tmux
    Arch Linux · community · 2026-09-17
    依据: Practical guide to sessions, prefix key bindings (Ctrl-b), pane splitting, and reattaching after SSH disconnects.

最后验证: 2026-09-17 · 分类: 开发者

06相关方案