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。

執行 ghostty +ssh --help 以獲得完整的旗標參考。

Note

+ssh 需要 Ghostty tip(將成為 Ghostty 1.4.0)。Ghostty 1.3.x 中並無此功能。本頁介紹的 SSH shell 整合功能在 1.3.x 中已存在。

功能

環境變數轉發

--forward-env 旗標(預設 true)請求 SendEnv 轉發 COLORTERM、TERM_PROGRAM 和 TERM_PROGRAM_VERSION,使遠端 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

當你有一個替代的 OpenSSH 建置或包裹器用戶端,並希望 +ssh 使用它時,這很有用。該路徑必須解析到一個可執行檔;否則 +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 以獲得完整的旗標參考。

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、rsync -e ssh、ssh 之上的 git、scp、sftp。它們直接呼叫 ssh(或 ssh 一類的二進位檔),從不查詢你 shell 的函數表。
  • 更一般地,非互動式 shell 與子 shell,包括 Makefile 的 recipe、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 影響。