在使用 SSH 远程连接服务器后,在终端上运行长时间占用终端的程序(如 pip install torch==1.10.1+cu111 torchvision==0.11.2+cu111 torchaudio==0.10.1 -f https://download.pytorch.org/whl/cu113/torch_stable.html 等大文件下载)容易受到网络中断等情况导致的 SSH 连接断开的影响。这是因为 SSH 终端窗口和会话是绑定的,当终端窗口断开时,会话也关闭,会话中的进程也随之关闭。使用 tmux 可以将会话与终端窗口“解绑”。这样,终端窗口关闭或异常断开时,不影响当前会话中运行的程序。
tmux 是一个终端复用器(terminal multiplexer),可以将会话与窗口“解绑”,窗口关闭时,会话不会终止,而是继续在后台运行,当重新把会话绑定到其他窗口时,里面运行的进程还在。tmux 有如下特点:
- 运行在单个窗口中同时访问多个会话;
- 新窗口可以接入已经存在的会话;
- 运行一个会话同时连接到多个窗口,实现多用户实时共享会话;
- 支持窗口在垂直和水平方向上任意拆分。
之前介绍过 screen 命令,也是一个终端复用器,但是没有 tmux 强大。
安装
| 12
 
 | sudo apt updatesudo apt install tmux
 
 | 
常用会话操作
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 
 | tmux new -s remote
 
 
 pip install torch==1.10.1+cu111 torchvision==0.11.2+cu111 torchaudio==0.10.1 -f https://download.pytorch.org/whl/cu113/torch_stable.html
 
 
 
 
 
 tmux attach -t remote
 
 | 
tmux 窗口有很多快捷键。所有快捷键都是通过前缀键唤醒,默认前缀键是 ctrl + b,即当先按下 ctrl + b 时才启动快捷键。如 ctrl + b 后再按 d 表示退出当前窗口,会话以后台方式运行。ctrl + b 后再按 ? 表示唤醒帮助信息,退出帮助按 ESC 或 q 键。
新建会话
| 12
 3
 4
 5
 
 | tmux
 
 
 tmux new -s remote
 
 | 
解绑会话
当窗口上没有运行程序时,可以使用如下命令:
当窗口上运行程序时,使用快捷键:ctrl + b, d
查看会话
| 12
 3
 
 | tmux list-session
 tmux ls
 
 | 
绑定会话
| 12
 3
 4
 5
 6
 7
 
 | tmux attach -t remote
 
 tmux attach-session -t remote
 
 
 tmux attach -t 0
 
 | 
关闭会话
当当前窗口绑定到该会话时
当要退出的会话 remote 或 0 没有绑定到当前窗口时
| 12
 
 | tmux kill-session -t remotetmux kill-session -t 0
 
 | 
切换会话
| 12
 3
 4
 5
 
 | tmux switch -t 0
 
 
 tmux switch -t remote
 
 | 
会话改名
| 12
 
 | tmux rename-session -t 0 remote
 
 | 
常用快捷键
当打开某个会话时,可以使用如下快捷键:
- ctrl + b, d: 分离当前会话
- ctrl + b, s: 列出所有会话
- ctrl + b, $: 为当前会话改名
窗口管理
新建窗口
| 12
 3
 4
 
 | tmux new-window
 
 tmux new-window -n newwin
 
 | 
切换窗口
| 12
 3
 4
 5
 
 | tmux select-window -t 0
 
 
 tmux select-window -t newwin
 
 | 
窗口改名
| 12
 
 | tmux rename-window newwin2
 
 | 
窗口快捷键
ctrl + b, c:创建一个新窗口,状态栏会显示多个窗口的信息。
ctrl + b, p:切换到上一个窗口(按照状态栏上的顺序)。
ctrl + b, n:切换到下一个窗口。
ctrl + b, :切换到指定编号的窗口,其中的是状态栏上的窗口编号。
ctrl + b, w:从列表中选择窗口。
ctrl + b, ,:窗口重命名。
窗格操作
tmux 可以将当前窗口划分为多个窗格(pane),可以在每个窗格运行不同的命令。
以下操作都是在当前窗口绑定到某个 tmux 会话下操作的。
划分窗格
| 12
 3
 4
 5
 
 | tmux split-window
 
 
 tmux split-window -h
 
 | 
光标在窗格间切换
虽然一个窗口下可以分割出多个窗格,但是鼠标切换不能简单的通过左键完成,需要使用命令完成
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 
 | tmux select-pane -U
 
 
 tmux select-pane -D
 
 
 tmux select-pane -L
 
 
 tmux select-pane -R
 
 | 
交换窗格位置
| 12
 3
 4
 5
 
 | tmux swap-pane -U
 
 
 tmux swap-pane -D
 
 | 
更多窗格快捷键
ctrl + b, %:划分左右两个窗格。
ctrl + b, “:划分上下两个窗格。
ctrl + b, :光标切换到其他窗格。是指向要切换到的窗格的方向键,比如切换到下方窗格,就按方向键↓。
ctrl + b, ;:光标切换到上一个窗格。
ctrl + b, o:光标切换到下一个窗格。
ctrl + b, {:当前窗格与上一个窗格交换位置。
ctrl + b, }:当前窗格与下一个窗格交换位置。
ctrl + b, ctrl + o:所有窗格向前移动一个位置,第一个窗格变成最后一个窗格。
ctrl + b, alt + o:所有窗格向后移动一个位置,最后一个窗格变成第一个窗格。
ctrl + b, x:关闭当前窗格。
ctrl + b, !:将当前窗格拆分为一个独立窗口。
ctrl + b, z:当前窗格全屏显示,再使用一次会变回原来大小。
ctrl + b, ctrl + :按箭头方向调整窗格大小。
ctrl + b, q:显示窗格编号。
其他命令
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 
 | tmux list-keys
 
 
 tmux list-commands
 
 
 tmux info
 
 
 tmux source-file ~/.tmux.conf
 
 | 
TMUX 终端颜色
增加如下内容:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 
 | set -g default-terminal "screen-256color"
 
 
 
 setw -g window-status-fg cyan
 setw -g window-status-bg default
 
 
 
 setw -g window-status-current-fg white
 setw -g window-status-current-bg red
 setw -g window-status-current-attr bright
 
 
 set -g pane-border-fg green
 set -g pane-border-bg default
 
 
 set -g pane-active-border-fg yellow
 set -g pane-active-border-bg default
 
 set -g message-fg white
 set -g message-bg black
 set -g message-attr bright
 
 
 set -g mouse on
 
 | 
然后,关闭所有 tmux 终端,再打开即可使用新的颜色配置。或者重新加载当前配置
| 1
 | tmux source-file ~/.tmux.conf
 | 
参考文献
- Tmux 使用教程
- Tactical tmux: The 10 Most Important Commands
- Getting started with Tmux
- 终端复用神器tmux——tmux的自定义配置