Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Fix GitLab Host Key Verification Failed

· 4 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide walks you through resolving the “Host key verification failed” error when connecting to GitLab over SSH. You’ll learn why the error appears, how to remove the old host key, and how to re‑establish a trusted connection so your Git operations work again.

What You'll Need

  • SSH access to the server where the error occurs (you’ll edit files in your home directory).
  • The GitLab hostname you’re connecting to (e.g., gitlab.com or a self‑hosted instance).
  • Basic familiarity with the command line.

Step-by-Step

  1. Identify the exact hostname. Run git remote -v inside your repository. Look for the part after @ in the SSH URL (e.g., git@gitlab.com:user/repo.git → host is gitlab.com).
  2. Check the current known_hosts entry. Run ssh-keygen -F gitlab.com (replace with your hostname). If it returns a line, the host key is present but mismatched. If it returns nothing, the key is missing entirely.
  3. Remove the old host key. Run ssh-keygen -R gitlab.com. This deletes the stale entry from your ~/.ssh/known_hosts file. You’ll see a confirmation message.
  4. Test the SSH connection. Run ssh -T git@gitlab.com (again, use your hostname). You’ll be prompted to accept the new host key. Type yes and press Enter.
  5. Verify the fingerprint (optional but recommended). Before accepting, compare the displayed fingerprint with the official GitLab SSH host keys. For gitlab.com, the current ED25519 fingerprint is SHA256:eUXGGm1YGsMAS7vkcx6JOJdOGHPem5gNp4MG2zK9Y1Y. If it matches, accept; if not, stop and investigate.
  6. Confirm Git operations work. Run git fetch or git pull in your repository. The error should be gone.
  7. If the error persists, check your SSH config. Open ~/.ssh/config and ensure there’s no Host block that points to a different hostname or port. Remove any conflicting entries.

Common Mistakes

  • Removing the wrong key. If you have multiple GitLab instances (e.g., gitlab.com and a self‑hosted one), make sure you remove the key for the exact hostname you’re connecting to. Using the wrong hostname will not fix the issue.
  • Typing “yes” without checking the fingerprint. Accepting a changed key blindly can expose you to a man‑in‑the‑middle attack. Always verify the fingerprint.
  • Editing known_hosts manually. Using a text editor to delete lines can introduce formatting errors. Stick to ssh-keygen -R.
  • Forgetting to update the remote URL. If your remote uses HTTP, SSH changes won’t help. Ensure the remote URL starts with git@ or ssh://.

Tips

  • Use the same key for all GitLab operations. If you have multiple SSH keys, ensure your SSH agent is using the correct one. Add it with ssh-add ~/.ssh/id_ed25519.
  • Set up a config alias. In ~/.ssh/config, you can add a Host gitlab.com block with HostName gitlab.com, User git, and IdentityFile ~/.ssh/id_ed25519 to simplify connections.
  • Automate known_hosts updates. For scripting, you can use ssh-keyscan gitlab.com >> ~/.ssh/known_hosts, but this bypasses fingerprint verification—use only in trusted environments.
  • Keep your SSH client updated. Older versions may have bugs with host key algorithms. Update OpenSSH to the latest version for your OS.

Troubleshooting

If the error persists after following the steps:

  • Check for multiple known_hosts files. SSH may be using a non‑default location if you have a custom ~/.ssh/config with UserKnownHostsFile. Run ssh -vT git@gitlab.com to see which file is being used.
  • Verify the hostname is correct. Typos in the remote URL are common. Run git remote -v and compare carefully.
  • Check if a proxy or firewall is interfering. Corporate networks sometimes intercept SSH traffic. Try connecting from a different network to isolate the issue.
  • Look for strict host key checking settings. If StrictHostKeyChecking is set to yes in your SSH config, you must accept the key manually. If set to no, SSH will auto‑accept, but that’s insecure—avoid it.
  • If you’re using a self‑hosted GitLab, the server’s SSH host key may have changed legitimately. Contact your administrator to confirm the new fingerprint.

Frequently Asked Questions

Why did the host key change?

GitLab regenerated its SSH host keys on May 24, 2023, for gitlab.com. If your known_hosts file still has the old key, you’ll see this error. For self‑hosted instances, the key changes if the server’s SSH configuration is reset.

Can I just delete the entire known_hosts file?

Yes, but that removes all trusted host keys, which is a security risk. Better to remove only the GitLab entry with ssh-keygen -R.

What if I’m using a non‑standard SSH port?

The error message will still appear. Use ssh-keygen -R followed by ssh -p PORT git@hostname to test. The known_hosts entry is stored with the port number, so removing it works the same way.

Is it safe to accept the new key?

Only if the fingerprint matches the official one published by GitLab. For gitlab.com, the ED25519 fingerprint is SHA256:eUXGGm1YGsMAS7vkcx6JOJdOGHPem5gNp4MG2zK9Y1Y. If it doesn’t match, stop.

Will this fix affect other SSH connections?

No, removing a specific host key only affects that hostname. Other entries in known_hosts remain intact.

Do I need to restart any services after fixing this?

No. SSH reads known_hosts on each connection, so the fix takes effect immediately.