SSH

Ghostty 的 `+ssh` CLI 动作是一个可直接替换 `ssh` 的 包装器,它会在 exec 真正的 `ssh` 二进制前为 Ghostty 准备好远程会话。

ghostty +ssh 是一个可直接替换的 ssh 包装器。它会提前 为 Ghostty 准备好远程会话,然后原样转发你的 参数,去 exec 真正的 ssh 二进制。这个命令修复了远程机器 需要 Ghostty terminfo 文件这一常见的 “鸡生蛋还是先有蛋”问题。

ghostty +ssh -- user@example.com

你可以把它用作别名,让你的 shell 里每次 ssh 调用 都经由 +ssh:

alias ssh='ghostty +ssh --'

或者,你也可以让 Ghostty 的 shell 集成 自动 包装 ssh。

完整的 flag 参考请运行 ghostty +ssh --help。

Note

+ssh 需要 Ghostty tip(这将是 Ghostty 1.4.0)。 Ghostty 1.3.x 中尚无此项。本页记载的 SSH shell 集成 功能已包含在 1.3.x 中。

功能

环境变量转发

--forward-env 参数(默认为 true)请求对 COLORTERM、TERM_PROGRAM 与 TERM_PROGRAM_VERSION 的 SendEnv 转发, 好让远程 shell 能检测到自己正运行在 Ghostty 之中。

为了让被转发的变量到达远程会话,远程 sshd 必须在其 AcceptEnv 指令里列出这些变量。参见下文 远程 sshd 配置。

通过 --forward-env=false 禁用。

Terminfo 安装

Ghostty 使用 xterm-ghostty 作为其 TERM 值,但多数远程 主机还没有随附 Ghostty 的 terminfo 条目。更多信息 见 Terminfo。

--terminfo 参数(默认 true)会在你首次连接远程主机 时,利用远程的 tic 在远程 安装 Ghostty 的 terminfo 条目。若安装 成功(或该主机已被缓存),该远程会话的 TERM 将被设为 xterm-ghostty。

如果安装失败(例如远程 tic 不可用),+ssh 会记录一条警告并回退到 TERM=xterm-256color,这是一个在远程上 保留颜色和大多数终端能力、被 广泛支持 的条目。

通过 --terminfo=false 禁用。

安装缓存

成功的 terminfo 安装会记录到本地 缓存,以 user@hostname 为键,这样后续连接同一主机时将 跳过安装步骤。该缓存由 +ssh-cache 动作管理。

向 +ssh 传入 --cache=false,则单次 调用绕过读取和写入,而不影响缓存。这对于 脚本化(“总是重新安装”)和调试 问题 很有用。

备用 ssh 可执行文件

默认时 +ssh 会 exec 你 PATH 中排在最前面的 ssh。 --ssh=PATH 参数则把它指向特定的 客户端:

ghostty +ssh --ssh=/opt/homebrew/bin/ssh -- user@example.com

在你想要 +ssh 使用其他 OpenSSH 编译版本或 包装客户端时,这很有用。路径必须指向 可执行文件;若不然,+ssh 将 失败,而不是默默回退 到 PATH 的 ssh。

+ssh-cache CLI 动作

ghostty +ssh-cache 管理 +ssh 使用的 terminfo 缓存:

# List every cached destination.
ghostty +ssh-cache

# Show a specific destination, or every cached user on a host.
ghostty +ssh-cache user@example.com
ghostty +ssh-cache example.com

# Manually mark a destination as cached (e.g. after installing
# terminfo by hand).
ghostty +ssh-cache --add=user@example.com

# Force the next connection to reinstall terminfo on a destination.
ghostty +ssh-cache --remove=user@example.com

# Remove cache entries older than a given age (s, m, h, d, w, y).
ghostty +ssh-cache --prune=30d

# Clear the entire cache.
ghostty +ssh-cache --clear

目标会按位置查询:user@hostname 显示 确切的该条目,而不带任何参数的 hostname 显示这台主机上所有缓存的 用户。若未找到匹配的查询,则 以非零状态结束。

--remove 是强迫在单个 目标重新安装的推荐方式:移除缓存 条目,即可让下一次成功连接重新填入。若想完全绕过 缓存,使用 +ssh --cache=false。

缓存条目始终作为 user@hostname 加密,其中 user 为 ssh 用户(默认为本地 $USER),而 hostname 是解析后的远程主机(HostName 和 ProxyJump 之后),而非 ~/.ssh/config 中的别名。

运行 ghostty +ssh-cache --help 查看完整 flag 参考。

Shell 集成

Ghostty 的 shell 集成 可在 你的交互 shell 中包装 ssh,使 ghostty +ssh 透明地被调用。默认禁用。可通过 shell-integration-features 启用:

shell-integration-features = ssh-env,ssh-terminfo
  • ssh-env 启用环境变量转发(把 --forward-env=true 传递给 +ssh)。
  • ssh-terminfo 启用远程 terminfo 安装(把 --terminfo=true 传递给 +ssh)。

该包装器一个适当的 ssh shell 函数,它 把上述功能开关翻译为正确的 +ssh命令行 选项并转发你的 参数。

限制

因为包装器是一个 shell 函数,它不会被子 进程继承。在下列情况下,包装器 不 会被使用:

  • 以 ./script.sh 或 sh script.sh 结尾运行的脚本。 每个脚本都在一个 新的非交互 shell 中运行,不会继承该函数。 应将脚本 source 到当前 shell (例如 source script.sh 或 . script.sh),使它在 定义该函数的同一 shell 进程中 运行。
  • 自行启动 ssh 的包装器工具,例如 aws ec2-instance-connect ssh、gcloud compute ssh、mosh 、ssh 之上的 rsync -e ssh、git、scp 与 sftp。这些会 直接调用ssh(或某个ssh 一类的二进制),从不 使用你的 shell 函数表。
  • 更为普遍的是,非交互式 shell 和子 shell, 包括 Makefile 配方、cron 作业以及 其他程序中的命令替换。

对于这些情况,请直接调用 ghostty +ssh,或按 下文说明配置 ~/.ssh/config。

手动 ~/.ssh/config 配置

对于 shell 包装器覆盖不到的情况,你可以在 ~/.ssh/config 中直接配置 SetEnv 与 SendEnv,这适用于每一次 ssh 调用,无论它是 如何启动的:

# ~/.ssh/config
Host example.com
  SetEnv TERM=xterm-256color
  SendEnv COLORTERM TERM_PROGRAM TERM_PROGRAM_VERSION

Note

SetEnv 需要 OpenSSH 8.7 或更新。有关 如何手动将 Ghostty 的 terminfo 条目复制到远程主机 的方法,请参见 Terminfo。

Note

如果远程主机已经安装了 xterm-ghostty terminfo 条目,SetEnv TERM=xterm-256color 段会 不必要地降级 TERM。请只在缺少 terminfo 条目的主机上使用 此项。

远程 sshd 配置

环境变量转发使用的是 SendEnv,并且只是一个 请求。远程 sshd 会静默丢弃 未列入其 AcceptEnv 指令的任何变量。如果想让 COLORTERM、TERM_PROGRAM 与 TERM_PROGRAM_VERSION 到达远程会话,远程 sshd_config 需要类似 以下内容:

# /etc/ssh/sshd_config on the remote host
AcceptEnv COLORTERM TERM_PROGRAM TERM_PROGRAM_VERSION

这是远程主机的属性而非 Ghostty 的,许多被管理或 加固的服务器将不会有此项配置。TERM 本身 始终由 ssh 转发, 不会受到 AcceptEnv 影响。