Search code examples
sshgitlabssh-keysgitlab-runner

Gitlab CI/CD using ssh / knownhosts error


I'm trying to use gitlab CI/CD to auto deploy my code, after push on an specific branch (in my case 'staging' branch)

after push on 'staging' branch I see following error on jobs section in gitlab UI:

Running with gitlab-runner 15.0.0 (xxxxxx)
  on deploy xxxxxx
Preparing the "ssh" executor
00:36
Using SSH executor...
ERROR: Preparation failed: ssh command Connect() error: ssh Dial() error: ssh: handshake failed: knownhosts: key is unknown

I can see gitlab from my VM and gitlab-runner registered successfully before.

I've also created ssh key and add it to gitlab-runner installation steps.


Solution

  • You need to check what SSH URL is used in your case.

    Something like [email protected]:me/myProject would look for gitlab.com SSH host keys fingerprints in an ~/.ssh/known_hosts file.

    Make sure to add first in gitlab-runner server the following to ~/.ssh/known_hosts:

    gitlab.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAfuCHKVTjquxvt6CM6tdG4SLp1Btn/nOeHHE5UOzRdf
    gitlab.com ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCsj2bNKTBSpIYDEGk9KxsGh3mySTRgMtXL583qmBpzeQ+jqCMRgBqB98u3z++J1sKlXHWfM9dyhSevkMwSbhoR8XIq/U0tCNyokEi/ueaBMCvbcTHhO7FcwzY92WK4Yt0aGROY5qX2UKSeOvuP4D6TPqKF1onrSzH9bx9XUf2lEdWT/ia1NEKjunUqu1xOB/StKDHMoX4/OKyIzuS0q/T1zOATthvasJFoPrAjkohTyaDUz2LN5JoH839hViyEG82yB+MjcFV5MU3N1l1QL3cVUCh93xSaua1N85qivl+siMkPGbO5xR/En4iEY6K2XPASUEMaieWVNTRCtJ4S8H+9
    gitlab.com ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBFSMqzJeV9rUzU4kWitGjeR4PWSa29SPqJ1fVkhtj3Hw9xjLVXVYrU9QlYWrOLXBpQ6KWjbjTDTdDkoohFzgbEY=
    

    That will skip manual fingerprint confirmation in SSH.
    In other words, no more "knownhosts: key is unknown".


    Note that with GitLab 15.3 (August 2022), you will have an easier time finding those:

    New links to SSH fingerprints

    Your GitLab SSH fingerprints are now easier to find, thanks to new links on the SSH configuration page and in the documentation.

    Thank you Andreas Deicha for your contribution!

    https://about.gitlab.com/images/15_3/manage-ssh-fingerprint.png -- New links to SSH fingerprints

    See Documentation and Issue.