SSH连到服务器,跑个任务,终端一断任务就没了。
用tmux可以解决这个问题——会话在服务器上一直跑,你断开连接也没事,下次连上来还在。
而且tmux可以分屏、多窗口,一个终端顶好几个用。
安装
# Debian/Ubuntu
apt install tmux
# CentOS
yum install tmux
# macOS
brew install tmux
基本概念
tmux有三层结构:
Session(会话)
├── Window(窗口)
│ ├── Pane(面板)
│ └── Pane
└── Window
└── Pane
- Session:一个工作环境,可以有多个窗口
- Window:一个窗口,可以分割成多个面板
- Pane:实际的终端
快速上手
创建和管理会话
# 创建会话
tmux # 创建匿名会话
tmux new -s work # 创建名为work的会话
# 断开会话(会话继续在后台运行)
# 快捷键:Ctrl+b d
# 查看所有会话
tmux ls
# 重新连接会话
tmux attach # 连接最近的会话
tmux a -t work # 连接名为work的会话
# 杀掉会话
tmux kill-session -t work
常用快捷键
tmux的快捷键都是 Ctrl+b 开头,称为前缀键。
先按 Ctrl+b,松开,再按后面的键。
会话操作:
Ctrl+b d- 断开会话(detach)Ctrl+b s- 切换会话Ctrl+b $- 重命名会话
窗口操作:
Ctrl+b c- 新建窗口Ctrl+b n- 下一个窗口Ctrl+b p- 上一个窗口Ctrl+b 数字- 切换到指定窗口Ctrl+b ,- 重命名窗口Ctrl+b &- 关闭窗口
面板操作:
Ctrl+b %- 左右分屏Ctrl+b "- 上下分屏Ctrl+b 方向键- 切换面板Ctrl+b x- 关闭当前面板Ctrl+b z- 最大化/恢复当前面板Ctrl+b {- 向前移动面板Ctrl+b }- 向后移动面板Ctrl+b Ctrl+方向键- 调整面板大小
其他:
Ctrl+b ?- 显示所有快捷键Ctrl+b :- 进入命令模式Ctrl+b [- 进入复制模式(可以滚动查看历史)
实用场景
场景1:跑长任务
# 创建会话
tmux new -s deploy
# 执行部署脚本
./deploy.sh
# 断开(Ctrl+b d)
# 该干啥干啥去
# 回来看结果
tmux a -t deploy
即使网络断了,部署任务也在继续执行。
场景2:多任务并行
# 创建会话
tmux new -s dev
# 左右分屏(Ctrl+b %)
# 左边跑服务
npm run dev
# 切到右边(Ctrl+b →)
# 上下分屏(Ctrl+b ")
# 看日志
tail -f /var/log/app.log
# 切到下面(Ctrl+b ↓)
# 连数据库
mysql -u root -p
一个终端三个任务同时看。
场景3:分享终端
两个人可以同时连接同一个tmux会话,看到相同的内容,适合远程pair programming:
# A创建会话
tmux new -s pair
# B连接同一个会话
tmux a -t pair
两个人看到的完全同步。
配置优化
tmux默认配置不太好用,建议自定义。创建 ~/.tmux.conf:
# 设置前缀键为Ctrl+a(更顺手)
unbind C-b
set -g prefix C-a
bind C-a send-prefix
# 开启鼠标支持
set -g mouse on
# 设置历史记录长度
set -g history-limit 50000
# 窗口编号从1开始
set -g base-index 1
setw -g pane-base-index 1
# 窗口自动重新编号
set -g renumber-windows on
# 设置终端颜色
set -g default-terminal "screen-256color"
# 减少延迟
set -sg escape-time 0
# 状态栏
set -g status-style bg=black,fg=white
set -g status-left "[#S] "
set -g status-right "%Y-%m-%d %H:%M"
# 分屏快捷键更直观
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
# vim风格的面板切换
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
# 快速重载配置
bind r source-file ~/.tmux.conf \; display "配置已重载"
# 复制模式用vi键位
setw -g mode-keys vi
改完后重载:
tmux source-file ~/.tmux.conf
# 或者在tmux里按 Ctrl+b :source-file ~/.tmux.conf
复制粘贴
这是tmux里比较麻烦的地方。
方法1:鼠标复制(开启mouse后)
按住Shift,用鼠标选中,然后用系统的复制(Ctrl+Shift+C或Cmd+C)。
方法2:tmux复制模式
# 进入复制模式
Ctrl+b [
# 用方向键或vim键位移动
# 按空格开始选择
# 按回车复制
# 粘贴
Ctrl+b ]
方法3:和系统剪贴板同步
Linux(需要xclip):
# ~/.tmux.conf
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xclip -selection clipboard"
macOS:
# ~/.tmux.conf
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "pbcopy"
持久化会话
tmux的会话在服务器重启后会丢失。如果需要持久化,用tmux-resurrect插件。
安装TPM(插件管理器)
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
配置插件
# ~/.tmux.conf 末尾添加
# 插件
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'
# 自动保存和恢复
set -g @continuum-restore 'on'
set -g @continuum-save-interval '15'
# 初始化TPM
run '~/.tmux/plugins/tpm/tpm'
重载配置后按 Ctrl+b I 安装插件。
之后:
Ctrl+b Ctrl+s- 保存会话Ctrl+b Ctrl+r- 恢复会话
和screen的比较
另一个常用的终端复用工具是screen,对比一下:
| 特性 | tmux | screen |
|---|---|---|
| 分屏 | 方便 | 麻烦 |
| 配置 | 灵活 | 简单 |
| 状态栏 | 好看 | 简陋 |
| 脚本化 | 强 | 弱 |
| 默认安装 | 少 | 多 |
推荐用tmux,功能更强。但如果服务器只有screen,基本用法差不多:
# screen基本操作
screen # 创建
screen -S name # 创建命名会话
Ctrl+a d # 断开
screen -r name # 恢复
screen -ls # 列出会话
快速参考
# 会话
tmux new -s name # 新建
tmux ls # 列出
tmux a -t name # 连接
tmux kill-session -t name # 删除
# 在tmux内
Ctrl+b d # 断开
Ctrl+b s # 切换会话
Ctrl+b c # 新窗口
Ctrl+b n/p # 切换窗口
Ctrl+b % # 左右分屏
Ctrl+b " # 上下分屏
Ctrl+b 方向键 # 切换面板
Ctrl+b z # 最大化面板
Ctrl+b x # 关闭面板
Ctrl+b [ # 复制模式
Ctrl+b ? # 帮助
tmux用熟了会上瘾。SSH连服务器第一件事就是tmux a,断网也不怕任务挂。
核心就三个操作:创建会话、分屏、断开/恢复。其他的用到再学。