Fix GitLab Host Key Verification Failed
In this article
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.comor a self‑hosted instance). - Basic familiarity with the command line.
Step-by-Step
- Identify the exact hostname. Run
git remote -vinside your repository. Look for the part after@in the SSH URL (e.g.,git@gitlab.com:user/repo.git→ host isgitlab.com). - 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. - Remove the old host key. Run
ssh-keygen -R gitlab.com. This deletes the stale entry from your~/.ssh/known_hostsfile. You’ll see a confirmation message. - Test the SSH connection. Run
ssh -T git@gitlab.com(again, use your hostname). You’ll be prompted to accept the new host key. Typeyesand press Enter. - 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. - Confirm Git operations work. Run
git fetchorgit pullin your repository. The error should be gone. - If the error persists, check your SSH config. Open
~/.ssh/configand ensure there’s noHostblock 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@orssh://.
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 aHost gitlab.comblock withHostName gitlab.com,User git, andIdentityFile ~/.ssh/id_ed25519to 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/configwithUserKnownHostsFile. Runssh -vT git@gitlab.comto see which file is being used. - Verify the hostname is correct. Typos in the remote URL are common. Run
git remote -vand 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
StrictHostKeyCheckingis set toyesin your SSH config, you must accept the key manually. If set tono, 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.