tmux使用教程:SSH断开后依然运行的会话

· 0x.cloud团队

tmux使用教程:SSH断开后依然运行的会话

tmux让终端会话在服务器上持续运行,使用方法很简单:运行tmux new -s work,启动耗时任务,按Ctrl+b再按d,然后就可以合上笔记本。 之后重新SSH登录,运行tmux attach -t work,一切都和离开时一模一样。本文介绍在服务器上如何使用tmux:会话、窗口、分屏、滚动翻页、配置文件和命令大全。

最典型的场景:Claude Code这样的AI智能体,或者耗时很长的构建,在Wi-Fi断开或您去睡觉时也必须继续运行。

安装tmux

在Ubuntu 24.04和Debian 12上:

sudo apt update && sudo apt install -y tmux

# check the version
tmux -V

在0x.cloud上,“AI智能体专用”镜像已预装tmux,以及Node、Python、Git和Docker。

前缀键

tmux的所有快捷键都以前缀Ctrl+b开头:按下它,松开,再按下一个键。本文把这种操作写作Ctrl+b d。

tmux会话的使用方法

# create a session named work
tmux new -s work

# list sessions
tmux ls

# attach to the work session
tmux attach -t work

# attach to the most recent session
tmux attach

# kill the work session and everything in it
tmux kill-session -t work
快捷键 作用
Ctrl+b d 分离(detach),会话继续运行
Ctrl+b s 列出并切换会话
Ctrl+b $ 重命名会话

窗口:一个会话里的多个终端

快捷键 作用
Ctrl+b c 新建窗口
Ctrl+b n / Ctrl+b p 下一个 / 上一个窗口
Ctrl+b 0…9 按编号跳转到窗口
Ctrl+b , 重命名窗口
Ctrl+b & 关闭窗口

窗格:分屏

快捷键 作用
Ctrl+b % 左右分屏
Ctrl+b " 上下分屏
Ctrl+b+方向键 切换到相邻窗格
Ctrl+b o 下一个窗格
Ctrl+b z 窗格全屏与还原
Ctrl+b x 关闭窗格

布局示例:左边运行智能体,右边用htop监控内存,下方用tail -f跟踪日志。

tmux鼠标滚轮翻页与复制

默认情况下,鼠标滚轮无法滚动tmux的历史输出。有两种解决方法:

  • 复制模式: Ctrl+b [,然后用方向键或PgUp/PgDn翻页;按q退出。
  • 开启鼠标支持: 在配置文件中加入set -g mouse on(见下文)。

~/.tmux.conf入门配置

nano ~/.tmux.conf
# scroll and select panes with the mouse
set -g mouse on

# longer scrollback
set -g history-limit 50000

# number windows from 1, easier to reach
set -g base-index 1

不重启即可生效:按Ctrl+b :,输入source-file ~/.tmux.conf。所有命令和选项见tmux官方wiki。

用tmux运行Claude Code

Claude Code和Codex可能连续运行数小时。在普通SSH会话中,连接一断,智能体就会停止。使用tmux:

# create a session for the agent and start it inside
tmux new -s agent
claude

# detach with Ctrl+b d; the agent keeps working
# come back in the morning
tmux attach -t agent

完整配置见服务器安装Claude Code,全天候运行的工作流见run Claude Code 24/7。Claude Code需要4GB以上内存,因此s2-4(2 vCPU、4GB)适合运行智能体,$15.99/月,按小时计费约$0.109520/小时。在Claude Code VPS页面可以获取预装tmux的服务器。

AI智能体提示词

连接到我的服务器,创建一个名为agent的tmux会话,在其中启动/root/app项目的构建,然后分离会话。把查看进度用的命令发给我。

tmux还是systemd?

任务 选择
交互式工作、需要盯着的智能体、耗时的构建 tmux
必须一直运行、重启后自动启动的机器人或服务 systemd

tmux无法在重启后保留。长期运行的服务,请配置systemd服务。

常见错误与解决方法

现象 原因 解决方法
sessions should be nested with care, unset $TMUX to force 您已经在tmux里了 用Ctrl+b d分离,或用Ctrl+b s切换
can't find session: work 会话已不存在(被关闭或服务器重启) 运行tmux ls,再tmux new -s work
no server running on /tmp/tmux-0/default 当前没有任何会话 新建一个:tmux new -s work
鼠标滚轮无法滚动 鼠标模式未开启 在~/.tmux.conf中加入set -g mouse on
快捷键没有反应 前缀键和后续按键同时按下 按Ctrl+b,松开,再按后续键
颜色异常 TERM设置不对 在~/.tmux.conf中:set -g default-terminal "tmux-256color"

tmux命令大全

任务 命令或快捷键
新建会话 tmux new -s name
列出会话 tmux ls
连接会话 tmux attach -t name
分离会话 Ctrl+b d
新建窗口 Ctrl+b c
左右 / 上下分屏 Ctrl+b % / Ctrl+b "
翻页 Ctrl+b [
tmux命令行 Ctrl+b :
结束会话 tmux kill-session -t name

常见问题:tmux使用

tmux是什么? tmux是终端复用器:它让终端会话在服务器上独立于SSH连接持续运行,还能把屏幕分成多个窗口和窗格。

如何退出tmux而不关闭会话? 按Ctrl+b,松开,再按d(detach,分离)。会话会继续在服务器上运行,用tmux attach即可回到会话。

如何彻底关闭tmux会话? 在每个shell中输入exit退出,或者运行tmux kill-session -t 会话名。会话中运行的所有程序都会被停止。

tmux和screen怎么选? 两者都能让会话在SSH断开后继续存在。tmux的分屏更好用,配置更简单,开发也更活跃,所以现在大多数人选择tmux。

服务器重启后tmux会话还在吗? 不在。tmux只在服务器运行期间存在。需要在重启后自动启动的程序,请使用systemd服务。