"Host key verification failed" on Gitlab_ci although ssh_key is present
Asked Answered
H

2

13

"Host key verification failed." on Gitlab_ci when running "ssh name@server"

Here's a part of my gitlab_ci which is supposed to login to my server and run scripts from the repo:

script:
  - which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )
  - eval $(ssh-agent -s)
  - ssh-add <(echo -e "$SSH_PRIVATE_KEY")


  - echo "debug"
  - echo "$SSH_PRIVATE_KEY" # all is ok here, it'll be printed
  - echo "debug"


  # - here comes an error
  - (cat ./script/script1.sh) | ssh -t -t $SSH_USERNAME@$SSH_HOST

And the errror:

  Host key verification failed.
  ERROR: Job failed: exit code 1

Why the error? The variables are set.

The private key does work when I'm doing the same thing manually from my local computer and Gitlab.

Hoem answered 27/9, 2018 at 20:13 Comment(3)
Possible duplicate of Git error: "Host Key Verification Failed" when connecting to remote repositoryDelastre
stackoverflow.com/…Delastre
@Hoem did the answer solved your issue? because I'm facing a similar issueGap
S
13

In addition of Git error: “Host Key Verification Failed” when connecting to remote repository, check your .known_hosts file.

If that same private key is working locally, that might be because, locally, your ~/.ssh/known_hosts file does include the ssh_keyscan of the remote server you want to connect to.
Make sure your GitLab CI job does too.

Sigrid answered 28/9, 2018 at 4:47 Comment(0)
F
4

I hit the same wall today and resolved it.
My solution is adding a -oStrictHostKeyChecking=no option to the ssh.

I found out this problem has been caused by ssh asking you the question below and this isn't supposed to appear in logs.

The authenticity of host 'xxx' can't be established.
ESDSA key fingerprint is xxxxxxxxxxxxx.
Are you sure you want to continue connecting (yes/no)?

The option -oStrictHostKeyChecking=no will suppress this so your ssh might work well.

I hope it will help people who have the same problem.

Freud answered 29/10, 2021 at 12:8 Comment(1)
It would work, yes, but at what cost? Removing this security check is generally not recommended (security.stackexchange.com/a/40002).Sigrid

© 2022 - 2024 — McMap. All rights reserved.