tmate参数介绍及使用

tmate 是一个基于 tmux 的远程终端共享工具,可以让你快速通过 SSH 或 Web 方式将你的终端分享给别人,非常适合远程协助或教学场景。


基本命令格式

1
tmate [options]

tmate help

mvpbang@mvpbang ~ % tmate -h
Usage: tmate [options] [tmux-command [flags]]

Basic options:
 -n <name>    specify the session token instead of getting a random one
 -r <name>    same, but for the read-only token
 -k <key>     specify an api-key, necessary for using named sessions on tmate.io
 -F           set the foreground mode, useful for setting remote access
 -f <path>    set the config file path
 -S <path>    set the socket path, useful to issue commands to a running tmate instance
 -v           set verbosity (can be repeated)
 -V           print version

常用参数详解

参数 作用
-S <socket> 指定 tmate socket 文件路径(默认在 /tmp/tmate-xxxx.sock
-F 以前台模式运行,不 daemonize,适用于调试
-n 只运行本地 tmate,不连接服务器
-f <config> 指定配置文件路径(默认读取 ~/.tmate.conf
-c <command> 启动后运行指定的命令,比如 tmate -c "htop"
-v 显示版本号
-h--help 显示帮助信息

示例用法

1. 启动 tmate 并获取 ssh 分享地址

1
tmate

然后输入:

1
tmate show-messages

或者直接:

1
2
tmate display -p '#{tmate_ssh}'
tmate display -p '#{tmate_web}'

2. 前台运行(调试模式)

1
tmate -F

3. 指定 socket 文件

1
tmate -S /tmp/mysocket attach

4. 启动并执行一个命令

1
tmate -c "top"

获取 session 地址

1
2
3
4
tmate new-session -d      # 后台启动
tmate wait tmate-ready # 等待连接准备好
tmate display -p '#{tmate_ssh}'
tmate display -p '#{tmate_web}'

配置文件(~/.tmate.conf

1
2
3
set -g tmate-server-host "ssh.tmate.io"
set -g tmate-server-port 22
set -g tmate-identity "/path/to/private_key"