GitLab CI : SSH 실패, 개인 키를 인증 할 수 없음

Aug 18 2020

이 링크 를 따라 Gitlab-CI에서 내 서버에 SSH를 시도했습니다. SSH 키의 경우 서버로 이동하여 공개 및 개인 키를 생성했습니다. 개인 키는 GitLab CI / CD 환경 변수로 추출됩니다.

YAML 템플릿은 아래와 같이 링크에서 대부분 복사됩니다.

    image: docker:19.03.8
      services:
        - docker:19.03.8-dind

    deployment:
      variables:
        ip: <ip-address>
      script:
        - apk add --update openssh-client sshpass
        - eval $(ssh-agent -s) - echo "$SSH_PRIVATE_KEY" | ssh-add - > /dev/null
        - mkdir -p ~/.ssh
        - chmod 700 ~/.ssh
        - export SSHPASS=$AWS_PASSWORD - sshpass -e ssh -o StrictHostKeyChecking=no -vvv ubuntu@$ip echo testing

그러나 개인 키에 액세스하려고 할 때 오류가 발생했습니다.

    debug1: Authentications that can continue: publickey,password
    debug1: Trying private key: /root/.ssh/id_rsa
    debug3: no such identity: /root/.ssh/id_rsa: No such file or directory
    debug1: Trying private key: /root/.ssh/id_dsa
    debug3: no such identity: /root/.ssh/id_dsa: No such file or directory
    debug1: Trying private key: /root/.ssh/id_ecdsa
    debug3: no such identity: /root/.ssh/id_ecdsa: No such file or directory
    debug1: Trying private key: /root/.ssh/id_ed25519
    debug3: no such identity: /root/.ssh/id_ed25519: No such file or directory
    debug1: Trying private key: /root/.ssh/id_xmss
    debug3: no such identity: /root/.ssh/id_xmss: No such file or directory
    debug2: we did not send a packet, disable method
    debug3: authmethod_lookup password
    debug3: remaining preferred: ,password
    debug3: authmethod_is_enabled password
    debug1: Next authentication method: password
    debug3: send packet: type 50
    debug2: we sent a password packet, wait for reply
    debug3: receive packet: type 51
    debug1: Authentications that can continue: publickey,password
    Permission denied, please try again.

도움이된다면 gitlab 공유 주자를 사용하고 있습니다.

[최신 정보]

내가 연결하고자하는 서버에서, 내가 생성 된 공개 키를 추가 한 추가 잊어 버렸 id_rsa.pubauthorized_keys파일을.

[편집 1]

제안 된대로 ssh-keyscan을 사용하여 알려진 호스트를 추가하여 출력을 $ SSH_KNOWN_HOSTS 변수로 복사했습니다. 업데이트 된 yaml 파일 아래. 그러나 동일한 오류가 발생했습니다.

    deployment:
      variables:
        ip: <ip-address>
      script:
        - apk add --update openssh-client sshpass
        - eval $(ssh-agent -s)
        - echo "$SSH_PRIVATE_KEY" | ssh-add - > /dev/null - mkdir -p ~/.ssh - chmod 700 ~/.ssh - touch ~/.ssh/known_hosts - echo "$SSH_KNOWN_HOSTS" >> ~/.ssh/known_hosts
        - chmod 644 ~/.ssh/known_hosts
        - export SSHPASS=$AWS_PASSWORD - sshpass -e ssh -o StrictHostKeyChecking=no -vvv ubuntu@$ip echo testing

답변

2 DV82XL Aug 18 2020 at 12:45

sshpass일반적으로 공개 / 개인 키를 사용하기 때문에 에 대해 잘 모르겠습니다 . 다음 은 원격 서버에서 SCP/ SSH명령 을 실행하도록 설정 한 작업의 예입니다 .

deploy:
  stage: deploy
variables:
  hostname: app-dev
before_script:
  # optional step if you decide to use a hostname instead of IP address
  - cp -f ./network/etc/hosts /etc/hosts
  # Setup SSH
  - which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )
  - eval $(ssh-agent -s) - ssh-add <(cat $SSH_PRIVATE_KEY)
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh
  - ssh-keyscan $HOSTNAME >> ~/.ssh/known_hosts - chmod 644 ~/.ssh/known_hosts script: # Copy files and execute commands - scp ./scripts/install_package.sh root@$HOSTNAME:/tmp/deploy
  - ssh root@$HOSTNAME "/tmp/deploy/install_package.sh && exit"

파이프 라인을 실행 하기 전에 다음을 수행해야합니다.

  1. 를 사용하여 SSH 키 쌍을 생성합니다 ssh-keygen. 암호를 사용하지 마십시오. 공개 키는로 끝나고 .pub개인 키에는 확장자가 없습니다.
  2. 원격 서버에 SSH 접속, 공개 키 내용 복사~/.ssh/authorized_keys
  3. GitLab의로 개인 키의 내용을 복사 파일 환경 변수 라고SSH_PRIVATE_KEY
  4. $HOSTNAME환경 변수 를 사용하는 경우 파이프 라인에서 변수를 정의하고 /etc/hosts파이프 라인 컨테이너 의 파일에 IP / 호스트 이름을 추가합니다 . 그렇지 않으면 대신 IP 주소를 사용하십시오.