점프 서버에서 제공하는 변수 호스트 이름을 사용하여 SSH 연결을 설정하는 방법

점프 서버에서 제공하는 변수 호스트 이름을 사용하여 SSH 연결을 설정하는 방법

로그인 노드(점프 서버)에 연결하고 명령을 실행하여 해당 노드에서 호스트 이름을 가져온 다음 동일한 사용자 이름과 자격 증명을 사용하여 해당 호스트 이름에 ssh를 연결해야 하는 경우를 위해 .sshconfig 파일을 만들고 싶습니다. 로그인 노드에 사용됩니다. 이것은 내가 끝내는 구성입니다.

Host jump-server
  HostName loginnode
  User user

Host remote
  User user
  IdentityFile C:\Users\user\.ssh\id_rsa
  ForwardX11 yes
  ForwardX11Trusted yes
  XAuthLocation /usr/bin/xauth
  ProxyCommand ssh -q jump-server "ssh user@`command_to_get_the_hostname` bash -l" 

다음 명령을 사용하여 ssh를 시도하고 있습니다. ssh.exe -v remote

그리고 나는 다음과 같은 결과를 얻었습니다.

OpenSSH_for_Windows_8.1p1, LibreSSL 3.0.2
debug1: Reading configuration data C:\\Users\\user/.ssh/config
debug1: C:\\Users\\user/.ssh/config line 14: Applying options for remote
debug1: Executing proxy command: exec ssh -q jump-server "ssh lemoni15@`command_to_get_the_hostname` bash -l"
debug1: identity file C:\\Users\\user\\.ssh\\id_rsa type 0
debug1: identity file C:\\Users\\user\\.ssh\\id_rsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_for_Windows_8.1
: command not foundSH_for_Windows_8.1

답변1

RemoteCommand

점프 호스트만이 원격 호스트의 호스트 이름을 안다고 하셨죠? 로컬 시스템에서 실행 되므로 점프 호스트에서는 ProxyCommand실행되지 않습니다 . command_to_get_the_hostname먼저 점프 호스트에 연결한 다음 거기에서 명령을 실행하려면 RemoteCommand대신 사용할 수 있습니다.

RemoteCommand

서버에 성공적으로 연결한 후 원격 시스템에서 실행할 명령을 지정합니다. 명령 문자열은 줄 끝까지 확장되며 사용자의 셸에서 실행됩니다. - -

예:

Host remote
  Hostname jump-host.example.com
  User user
  RemoteCommand ssh user@$(/full/path/to/command_to_get_the_hostname)

이제 를 사용하여 원격 호스트에 연결할 수 있습니다 ssh -t remote.-t 강제 의사 터미널 할당$(command)원격 측, 즉 점프 호스트에서 실행하기 위한 것입니다 .

부작용으로 IdentityFile C:\Users\user\.ssh\id_rsa원격 호스트에 로컬을 사용할 수 없지만 두 번째 호스트가 ssh이제 점프 호스트에서 실행되므로 점프 호스트에 키가 있어야 합니다.

ProxyJump및 스크립팅

ProxyJump점프 호스트를 ( -J) 호스트로 사용하고 로컬 IdentityFile( -i), ForwardX11( -X), ForwardX11Trusted( ) 등을 사용할 수 있으려면 -YSSH 구성 파일만 사용하는 대신 일부 스크립팅이 필요합니다. 먼저 점프 호스트에 연결하고 거기에서 원격 호스트 주소를 가져온 다음 다른 SSH 연결에서 이를 사용해야 합니다.

와 함께세게 때리다이것은 다음과 같습니다:

#!/bin/bash

remotehost=$(\
  ssh [email protected] \
  /full/path/to/command_to_get_the_hostname) || exit 1

ssh -X -Y -J [email protected] \
  -i ~/.ssh/id_rsa user@"$remotehost"

생각하다이것은 다음과 같이 바뀔 수 있습니다.파워셸Windows의 스크립트(테스트하지 않음):

$remoteHost = ssh.exe [email protected] `
  /full/path/to/command_to_get_the_hostname
if (-not $?) {throw "Failed to get the remote hostname"}

ssh.exe -X -Y -J [email protected] `
  -i "C:\Users\user\.ssh\id_rsa" "user@$remoteHost"

관련 정보