無法使用 ProxyJump 進行 ssh,但可以與 ssh -J 搭配使用

無法使用 ProxyJump 進行 ssh,但可以與 ssh -J 搭配使用

我的問題是:如何使用 ubuntu 實例在 AWS 上設定 ssh 堡壘主機?

我可以成功地執行以下操作:

root@e183d80cdabc# ssh -J [email protected] [email protected]
Last login: Sat Sep  4 13:14:17 2021 from 10.240.0.30
==> SUCCESS! ==> ubuntu@ip-10-240-0-20:~$

但是當我嘗試 ~/.ssh/config 檔案方法時它失敗了。使用的命令:

# ssh 10.240.0.20
# ssh [email protected]
# ssh -i ~/.ssh/id_rsa [email protected]

ssh: connect to host 10.240.0.20 port 22: Connection refused

我的 ~/.ssh/config 看起來像這樣:

root@e183d80cdabc# cat $HOME/.ssh/config
Host bastion
  HostName 54.170.186.144
Host remote
  HostName 10.240.0.20
  ProxyJump bastion

我在 AWS 上運行 ubuntu,如下所示:

ubuntu@ip-10-240-0-30:~$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.2 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.2 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

我嘗試添加該User ubuntu字段,但這沒有幫助。

/etc/ssh/ssh_config在伺服器上的樣子是這樣的:

Host *
    ForwardX11Trusted yes
    IdentityFile ~/.ssh/id_rsa
    Port 22
    SendEnv LANG LC_*
    HashKnownHosts yes
    GSSAPIAuthentication yes

更新 我現在使用詳細選項,即

root@e183d80cdabc# ssh -vvv 10.240.0.20
OpenSSH_8.2p1 Ubuntu-4ubuntu0.3, OpenSSL 1.1.1f  31 Mar 2020
debug1: Reading configuration data /root/.ssh/config
debug1: /root/.ssh/config line 2: Applying options for *
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug2: resolve_canonicalize: hostname 10.240.0.20 is address
debug2: ssh_connect_direct
debug1: Connecting to 10.240.0.20 [10.240.0.20] port 22.
debug1: connect to address 10.240.0.20 port 22: Connection refused
ssh: connect to host 10.240.0.20 port 22: Connection refused

它似乎沒有使用任何跳轉主機(即它跳過堡壘)並且直接前進,並且失敗了。

任何想法都非常感謝!謝謝

==================================================== = =======

更新:2021-09-04-15-44 - 附解決方案 謝謝大家,我已在下面標記為答案。

正確的配置不使用 HostName,因為匹配是在主持人。我還能夠在 IP 位址上包含通配符,這正是我真正想要的。

ssh配置

root@e183d80cdabc# cat $HOME/.ssh/config
Host bastion
  HostName 63.33.206.201
  User ubuntu
Host 10.240.0.*
  ProxyJump bastion
  User ubuntu

瞧!

# ssh 10.240.0.20
...
ubuntu@ip-10-240-0-20:~$

答案1

配對是在Host節上完成的,而不是在 上完成的HostName

嘗試:

ssh ubuntu@remote

答案2

命令列之間的區別

ssh -J [email protected] [email protected]

我建議你做什麼來參考你的配置

ssh remote

是後者既沒有IP也沒有在兩台機器上登入的用戶在命令列中 - 您必須編輯 ssh 配置以包含全部您不再在命令列上傳遞的訊息:

# $HOME/.ssh/config
### The Bastion Host
Host bastion
  HostName 54.170.186.144
  User ubuntu

### The Remote Host
Host remote
  HostName 10.240.0.20
  User ubuntu
  ProxyJump bastion

更新:雖然是的,理論上你我建議配置您的Host節以匹配 IP 位址反對這樣做。

如果某個主機無法透過某個 IP 位址直接到達,則應透過姓名- 想像一下當您將相同的(私有)IP 空間分配給多個主機時會發生什麼,您不可能為每個主機分配正確的 ProxyJump 配置。

不宜使用位址來引用主機的另一個原因是主機可透過多個位址系列存取:如果主機可透過IPv4 和IPv6 訪問,您可能希望ssh 連線保持與協定無關,並且僅在您真正想要限制時才添加標誌(自動)選擇。

相關內容