多個代理跳轉的 ssh 配置

多個代理跳轉的 ssh 配置

我有一個設置,需要進行 2 次代理跳轉才能連接到我的伺服器D。實際上,從A,我需要連接到B,從那裡我需要連接到C(只允許來自 的連接B),然後我可以從那裡連接到D(只接受來自 的連接C)。

在 上B,使用 ssh 配置,我D只需鍵入即可連接到ssh D,這就是為什麼我認為我可以使用此配置來A連接到D

Host B
    hostname 127.0.0.1
    user myUser
    Port 43099
    IdentityFile ~/.ssh/id_rsa

Host D
    hostname D
    ProxyJump B
  

然後連接 fromA來連接到Dvia ssh D

但是,這是我從中得到的錯誤:

debug1: Reading configuration data /home/MyUser/.ssh/config
debug1: /home/MyUser/.ssh/config line 17: Applying options for D
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Setting implicit ProxyCommand from ProxyJump: ssh -v -W '[%h]:%p' B
debug1: Authenticator provider $SSH_SK_PROVIDER did not resolve; disabling
debug1: Executing proxy command: exec ssh -v -W '[D]:22' B
debug1: Local version string SSH-2.0-OpenSSH_8.8
OpenSSH_8.8p1, OpenSSL 1.1.1m  14 Dec 2021
debug1: Reading configuration data /home/MyUser/.ssh/config
debug1: /home/MyUser/.ssh/config line 7: Applying options for B
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Authenticator provider $SSH_SK_PROVIDER did not resolve; disabling
debug1: Connecting to 127.0.0.1 [127.0.0.1] port 43023.
debug1: Connection established.
debug1: identity file /home/MyUser/.ssh/id_rsa type 0
debug1: identity file /home/MyUser/.ssh/id_rsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.8
debug1: Remote protocol version 2.0, remote software version OpenSSH_8.6
debug1: compat_banner: match: OpenSSH_8.6 pat OpenSSH* compat 0x04000000
debug1: Authenticating to 127.0.0.1:43023 as 'MyUser'
debug1: load_hostkeys: fopen /home/MyUser/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ssh-ed25519
debug1: kex: server->client cipher: [email protected] MAC: <implicit> compression: none
debug1: kex: client->server cipher: [email protected] MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: SSH2_MSG_KEX_ECDH_REPLY received
debug1: Server host key: ssh-ed25519 SHA256:to5+n56a6Zj+sbTnMghZGehX2OX5cn29HWbje55WJHU
debug1: load_hostkeys: fopen /home/MyUser/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: Host '[127.0.0.1]:43023' is known and matches the ED25519 host key.
debug1: Found key in /home/MyUser/.ssh/known_hosts:45
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: /home/MyUser/.ssh/id_rsa RSA SHA256:d6hsPtnc6TWfm9j5lOClBzD4ylE+ww/tg9BE7qP8DlE explicit
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,[email protected],ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,[email protected],[email protected]>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Offering public key: /home/MyUser/.ssh/id_rsa RSA SHA256:d6hsPtnc6TWfm9j5lOClBzD4ylE+ww/tg9BE7qP8DlE explicit
debug1: Server accepts key: /home/MyUser/.ssh/id_rsa RSA SHA256:d6hsPtnc6TWfm9j5lOClBzD4ylE+ww/tg9BE7qP8DlE explicit
Authenticated to 127.0.0.1 ([127.0.0.1]:43023) using "publickey".
debug1: channel_connect_stdio_fwd: D:22
debug1: channel 0: new [stdio-forward]
debug1: getpeername failed: Bad file descriptor
debug1: Requesting [email protected]
debug1: Entering interactive session.
debug1: pledge: filesystem full
debug1: client_input_global_request: rtype [email protected] want_reply 0
debug1: client_input_hostkeys: searching /home/MyUser/.ssh/known_hosts for [127.0.0.1]:43023 / (none)
debug1: client_input_hostkeys: searching /home/MyUser/.ssh/known_hosts2 for [127.0.0.1]:43023 / (none)
debug1: client_input_hostkeys: hostkeys file /home/MyUser/.ssh/known_hosts2 does not exist
debug1: client_input_hostkeys: host key found matching a different name/address, skipping UserKnownHostsFile update
debug1: Remote: /Users/Hendrik/.ssh/authorized_keys:3: key options: agent-forwarding port-forwarding pty user-rc x11-forwarding
debug1: Remote: /Users/Hendrik/.ssh/authorized_keys:3: key options: agent-forwarding port-forwarding pty user-rc x11-forwarding
channel 0: open failed: connect failed: nodename nor servname provided, or not known
stdio forwarding failed
kex_exchange_identification: Connection closed by remote host
Connection closed by UNKNOWN port 65535

B似乎根本沒有讀取ssh 配置。

我的 ssh 配置B如下所示:


Host C
  HostName C
  User MyUser
  ForwardX11Trusted yes
  ForwardX11 yes

Host D
  User MyUser
  HostName D
  ProxyCommand ssh %r@C -W %h:22
  RemoteForward 26512 github.com:22
  Compression yes
  ForwardX11Trusted yes
  ForwardX11 yes
  DynamicForward 10080
  ControlMaster auto
  ControlPath ~/.ssh/masters/%C

有沒有辦法編輯 ssh 配置,A以便我可以從AD進行連接ssh D

答案1

使用代理跳轉時,所有 ssh 連線均源自原始客戶端(本例為 A),中間節點只是隧道端點。因此,在您的情況下,所有配置(包括私鑰檔案)都需要位於 A 上,並且看起來像這樣(假設您對所有 3 個主機使用相同的身份檔案):

Host B
    hostname 127.0.0.1
    user myUser
    Port 43099
    IdentityFile ~/.ssh/id_rsa
Host C
    ProxyJump B
    IdentityFile ~/.ssh/id_rsa
    # Any additional options for connecting to C
Host D
    ProxyJump C
    IdentityFile ~/.ssh/id_rsa
    # Any additional options for connecting to D

當您執行以下操作時ssh D,A 上的 ssh 用戶端將進行以下一系列 ssh 會話:

  1. A -> B(直接透過網路)
  2. A -> C(透過 A->B 連接建立隧道,然後直接透過網路從 B 到 C)
  3. A -> D(透過 A->C 連接(透過 A->B 建立隧道),然後直接透過網路從 C 到 D)

相關內容