使用 ProxyCommand 和 netcat 模式時讓 ssh 從設定解析主機名

使用 ProxyCommand 和 netcat 模式時讓 ssh 從設定解析主機名

我正在嘗試設定一些用於彈跳 ssh 連接的通用選項。這是我的~/.ssh/config文件,縮寫為:

Host *%via
  ProxyCommand ssh gateway -W $(echo %h | cut -d%% -f1) %p

Host gateway
  HostName gateway.example.com
  User username
  ForwardAgent yes
  IdentityFile keypathg

Host target
  User username
  HostName target.example.com
  IdentityFile keypatht

當我*%via使用Host別名時,我得到:

% ssh -vvv target%via
OpenSSH_5.9p1, OpenSSL 0.9.8y 5 Feb 2013
debug1: Reading configuration data /Users/myuser/.ssh/config
debug1: /Users/myuser/.ssh/config line 5: Applying options for *
debug1: /Users/myuser/.ssh/config line 12: Applying options for *%via
debug1: Reading configuration data /etc/ssh_config
debug1: /etc/ssh_config line 20: Applying options for *
debug1: auto-mux: Trying existing master
debug1: Control socket "/Users/myuser/.ssh/tmp/target%via_22_myuser" does not exist
debug2: ssh_connect: needpriv 0
debug1: Executing proxy command: exec ssh -A gateway -W $(echo target%via | cut -d% -f1):22
debug1: permanently_drop_suid: 501
debug1: identity file /Users/myuser/.ssh/id_rsa type -1
debug1: identity file /Users/myuser/.ssh/id_rsa-cert type -1
debug1: identity file /Users/myuser/.ssh/id_dsa type -1
debug1: identity file /Users/myuser/.ssh/id_dsa-cert type -1
ssh_exchange_identification: Connection closed by remote host

但是,如果我利用

% ssh target.example.com%via

我訪問了目標伺服器,但作為錯誤的使用者並且沒有 pubkey 身份驗證。

思考此時我的問題是,在使用時ForwardAgent,這種彈跳方法是否會通過我的 ssh 配置/環境整個,或者只是密鑰。如果只是鑰匙,前者可以用某種方式利用嗎?

我的ssh版本是5.9v1,閘道是5.9v1,目標是5.3p1。我相信-W是在 5.4 中引入的,但這對於最終的盒子來說應該不重要吧?使用舊學校nc似乎沒有什麼不同。

我已經驗證我可以手動 ssh 到線路中的每個盒子。這樣做表示主機名稱別名資訊未傳遞,因為在網關上時,我不能,ssh target但我可以ssh target.example.com。這適用於 pubkey auth。網關和目標偶然具有相同的用戶名,這就是為什麼在沒有推送配置的情況下它可以工作的原因。

如果ForwardAgent或類似的配置無法推送此訊息,那麼最明智的解決方法是什麼,在網關上保留包含此資訊的 .ssh/config ?

答案1

哇,謝謝你問這個問題。我發現很少有人充分利用 SSH,這個問題涉及到幾個領域。

這不是ProxyCommand問題。它ProxyCommand只是指示本地 ssh 客戶端在嘗試與遠端客戶端通訊之前做一些準備工作。是的,在我們的實例中,我們與另一個 ssh 會話進行通信,但該會話只是-W簡單地獲取我們的輸入並將其轉發到另一台機器。您可以認為準備 ssh 會話是完全獨立的。不可避免的汽車類比:無論您是否需要乘坐渡輪從 A 點到 B 點,您的汽車都是同一輛車。

這不是ForwardAgent問題。 ForwardAgent讓本機用戶端提供一種工具,使本機金鑰在遠端會話的環境中可用。您還沒有完成建立遠端會話的任務。

這是一個.ssh/config格式問題。請注意第二行和第三行 debug1。它們列出了從您的.ssh/config.您注意到這$ ssh target.example.com%via有效,但使用者名稱和金鑰錯誤。好吧, 的 節Host target沒有被讀取(這將提供正確的用戶名和密鑰檔案)。使用了哪些節? **%via

如何讓這些選項通過?嗯,很有趣,通配符匹配 0 長度的字串。 Host target*將匹配target, target%via,target.example.comtarget.example.com%via

所以你問這個問題,會在機器.ssh/config上設定一個gateway幫助嗎?不,不會的。它永遠不會被閱讀。一切都發生在我們的本地機器上。

我所解釋的只是回答為什麼$ ssh target.example.com%via不起作用。

你比較喜歡$ ssh target%via。確實如此,這樣更方便。縮寫形式失敗,因為作為主機名,target找不到;它沒有解決。為什麼 ssh 不噴出:ssh: Could not resolve hostname target: Name or service not known?因為ProxyCommand已經成功建立了。 ssh 連線的元素已經建立,但主機名稱故障發生在意想不到的地方,因此它會發出更通用的訊息。我會就此提交一份錯誤報告,以幫助確定調試資訊可以改進的地方。

最後評論:

我確實喜歡它的Host *%via語法。它很乾淨,但又靈活。我以前見過Host *+*,它使用 的第一部分和最後一部分來%h(ost)確定要去哪裡。但你需要付出更多的努力才能理解這一點。關聯:http://wiki.gentoo.org/wiki/SSH_jump_host

相關內容