Your deploy job, git pull or scp stops with one line:
Host key verification failed.
Sometimes it comes with a wall of @ signs above it, sometimes it is completely alone. Either way, ssh refused to continue because it could not confirm that the server on the other end is the one you meant to reach. No password or key was sent. The connection stopped before authentication.
That makes this error different from most ssh failures. It is not about your key, your account or the firewall. It is about known_hosts, the file where your client remembers each server's public host key. The fix depends on which of two situations you are in, and one of them should make you stop and check before you fix anything.
We reproduced every case below with OpenSSH 9.6 on Ubuntu 24.04 against a lab server, and the output in this guide is copied from those runs.
Quick Summary: What You'll Learn
- What "Host key verification failed" means and why no credential was sent
- How to tell the two cases apart: an unknown host versus a changed host key
- Why it fails with no prompt in CI, cron, Git GUIs, Jenkins and Ansible
- The safe fix: check the fingerprint out of band, then add or replace the key
- Why
sshworks in your terminal whilegit,scporsudofail - Fixes for GitHub, GitHub Actions, Docker builds and Ansible, without turning checking off
What does "Host key verification failed" mean?
Every SSH server has a host key pair. During the handshake the server proves it holds the private half, and your client compares the public half with what it has stored for that host name in ~/.ssh/known_hosts (plus the system-wide /etc/ssh/ssh_known_hosts). "Host key verification failed" is the client's final line when that comparison could not end in trust.
There are only two ways to get there:
- The host is unknown. There is no stored key for this exact name, and ssh was not allowed to ask you whether to trust the new one.
- The host key changed. There is a stored key, and the server presented a different one.
The first is a missing entry. The second is either a rebuilt server or someone in the middle, and ssh cannot tell which. Our explainer on SSH host key verification covers the threat model in depth. This guide is about reading the error and getting unblocked without weakening it.
Which case are you in: unknown host or changed key?
Look at what is printed above the last line.
Unknown host, BatchMode=yes (common in scripts and CI). In our lab this printed nothing else at all:
$ ssh -o BatchMode=yes deploy@127.0.0.1 -p 2299 true
Host key verification failed.
Unknown host, StrictHostKeyChecking=yes. Here ssh tells you why:
No ED25519 host key is known for [127.0.0.1]:2299 and you have requested strict checking.
Host key verification failed.
Changed key. This is the loud one:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ED25519 key sent by the remote host is
SHA256:P+OD0MrTwzsf5Ov4PxOlLBmWQor3T1W6EyDCTK+avoM.
Please contact your system administrator.
Add correct host key in /home/you/.ssh/known_hosts to get rid of this message.
Offending ED25519 key in /home/you/.ssh/known_hosts:1
remove with:
ssh-keygen -f '/home/you/.ssh/known_hosts' -R '[127.0.0.1]:2299'
Host key for [127.0.0.1]:2299 has changed and you have requested strict checking.
Host key verification failed.
So a bare "Host key verification failed." with nothing above it almost always means "unknown host, no way to ask". The banner means "changed key". If you only have a CI log with the bare line, rerun the same command with -o StrictHostKeyChecking=yes or -v to see which key type and name ssh was checking.
You can also ask known_hosts directly whether it has an entry for a name:
ssh-keygen -F server.example.com
# non-standard port: the entry is stored as [host]:port
ssh-keygen -F '[server.example.com]:2222'
No output means no entry, which points to the unknown-host case.
Why does it fail with no prompt in CI, cron, Git GUIs and Ansible?
On your laptop, connecting to a new server shows the familiar Are you sure you want to continue connecting (yes/no/[fingerprint])? question. That prompt needs someone to answer it. When it can't be shown, ssh fails closed instead of guessing.
That happens in three common ways:
-
BatchMode=yes. Thessh_configman page says it disables "password prompts and host key confirmation requests". Many deploy scripts and CI templates set it on purpose so a job never hangs waiting for input. -
No terminal. Cron jobs, systemd services, CI runners and many GUI Git clients run ssh without a controlling terminal. In our lab, a plain
sshwith the defaultStrictHostKeyChecking=ask, started withsetsidso it had no terminal, printed onlyHost key verification failed.and exited with status 255. -
A fresh or different
known_hosts. A new CI container, a Docker build stage or a Jenkins agent starts with an emptyknown_hosts, even if your laptop has trusted that server for years.
The fix is not to make the prompt appear. It is to put the right key into the known_hosts that this job actually reads, before the job connects. The next section shows how to get the right key.
How do you fix it safely?
The goal is to add the server's real public key, and "real" means you checked its fingerprint through a channel the network path can't tamper with.
Step 1: get the fingerprint from the server itself. Use the cloud console's serial or web console, an existing session you already trust, or your provisioning output. On the server:
ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub
256 SHA256:P+OD0MrTwzsf5Ov4PxOlLBmWQor3T1W6EyDCTK+avoM lab2 (ED25519)
Step 2: fetch the key over the network and compare. ssh-keyscan collects host keys without logging in:
ssh-keyscan -t ed25519 -p 2299 127.0.0.1 > /tmp/hostkey.txt
ssh-keygen -lf /tmp/hostkey.txt
256 SHA256:P+OD0MrTwzsf5Ov4PxOlLBmWQor3T1W6EyDCTK+avoM [127.0.0.1]:2299 (ED25519)
The two SHA256: strings match, so this is the key the server really holds. The ssh-keyscan man page is blunt about why step 1 matters: it "cannot verify the authenticity of the host keys it obtains", and its output "should be verified out of band". Piping ssh-keyscan straight into known_hosts without comparing is trust-on-first-use with extra steps.
Step 3: append it to the right file.
cat /tmp/hostkey.txt >> ~/.ssh/known_hosts
For GitHub, GitLab and other hosted Git, you don't need console access. GitHub publishes its fingerprints on its SSH key fingerprints page, and the same keys come from its meta API, which you fetch over HTTPS:
curl -s https://api.github.com/meta | jq -r '.ssh_keys[]' | sed 's/^/github.com /' >> ~/.ssh/known_hosts
Compare the result with ssh-keygen -lf ~/.ssh/known_hosts against the published fingerprints once, and you are done.
What if the host key really changed?
Stop first. A changed key has boring explanations and one dangerous one, and the warning looks the same for all of them. Boring explanations include:
- The server was rebuilt or reinstalled (a new cloud instance on the same IP, a reflashed Raspberry Pi SD card, a reinstalled VM).
- An admin rotated the host keys.
- An IP address or DNS name was moved to a different machine.
- The provider replaced its key. GitHub did exactly this for its RSA host key on March 24, 2023, after the private key was briefly exposed in a public repository, and users with the old key saw the banner above.
If you can confirm one of these, check the new fingerprint with the out-of-band method from the previous section. Only when it matches, remove the old entry and add the new one:
ssh-keygen -R server.example.com
# for a non-standard port
ssh-keygen -R '[server.example.com]:2222'
In our lab, ssh-keygen -R printed # Host [127.0.0.1]:2299 found: line 1, then Original contents retained as kh.old, so you keep a backup of the previous file. The error message itself prints the exact -R command with the right file and name, which is handy when the entry lives in a non-default file.
If you can't explain the change (no rebuild, no announcement, the fingerprint from the console doesn't match what the network gives you), don't connect. Disconnect from the network you are on and check from somewhere else.
Note that StrictHostKeyChecking=accept-new does not help here. In our test it still printed the full warning and refused, which matches the man page: accept-new adds new keys automatically "but will not permit connections to hosts with changed host keys".
Why does ssh work but git, scp or sudo fail?
When your own ssh server connects fine and another tool on the same machine fails, the other tool is almost always reading a different known_hosts or using a different name.
A different user. known_hosts is per user. sudo ssh, sudo git pull, a Jenkins job running as jenkins, a systemd service running as deploy or a container running as root all read that user's ~/.ssh/known_hosts, not yours. Add the key for the user that actually runs the command.
A different name for the same server. Entries are matched by the name you connect to. In our lab, a key stored for 127.0.0.1 did not count for localhost: connecting by the second name failed with Host key verification failed. even though it was the same server. The same thing happens with an IP versus a DNS name, a short alias versus a full name, or an ssh config alias whose HostName points somewhere else. If you use aliases, our guide to organizing your ssh config helps keep names consistent.
A different port. On any port other than 22, entries are stored as [host]:port. A key saved for server on port 22 does not cover server on port 2222.
Jump hosts. With ProxyJump, both the bastion and the target are verified, each against its own entry. Either one can be the host that failed. Run with -v to see which name was being checked. Our bastion and ProxyJump setup guide covers the config side.
How git and scp show it. Both run ssh underneath, so the ssh lines appear first and the tool adds its own line after. With a changed key, our scp test ended with:
Host key verification failed.
scp: Connection closed
and git ls-remote ended with:
Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
That last message sends many people off to debug their deploy key. Read the line above it first: if it says host key verification failed, the key in your agent was never used.
How do you fix it in GitHub Actions, Docker builds and Ansible?
The pattern is the same everywhere: pin the known key in the job, don't disable checking.
GitHub Actions or any CI runner, connecting to your own server. Store the server's known_hosts line (the verified output of ssh-keyscan from earlier) as a CI variable or secret, then write it before connecting:
mkdir -p ~/.ssh
chmod 700 ~/.ssh
printf '%s\n' "$DEPLOY_KNOWN_HOSTS" >> ~/.ssh/known_hosts
ssh -o BatchMode=yes deploy@server.example.com ./deploy.sh
Running ssh-keyscan inside the job on every run works, but it trusts whatever answers at that moment, which is the exact check you are trying to keep.
Cloning from GitHub in CI or a Docker build. Add GitHub's published Ed25519 line, copied from its fingerprints page:
mkdir -p ~/.ssh
echo "github.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl" >> ~/.ssh/known_hosts
Ansible. Ansible enables host key checking by default, and its docs explain that a reinstalled host or a new host missing from known_hosts will error or prompt, which is a problem under cron. The documented switch is host_key_checking = False in ansible.cfg or ANSIBLE_HOST_KEY_CHECKING=False. That turns the check off for every host in the run. A safer routine is to add each new host's verified key to the control node's known_hosts when you provision it, so the inventory and known_hosts grow together.
Should you just turn off StrictHostKeyChecking?
You will find -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null in many answers. It makes the error go away by making every server trusted, including one that is impersonating yours. For a throwaway VM on your own laptop that is a reasonable trade. For production deploys it removes the only check that protects the credentials you are about to send.
Middle grounds, in order of how much protection they keep:
-
Pinned
known_hostsin the job (above): full protection, a little upkeep when servers are rebuilt. -
StrictHostKeyChecking=accept-new: trusts a host the first time, still refuses a changed key. Fine for a long-lived workstation, weak for an ephemeral CI runner where every run is a "first time". -
SSH host certificates: each server's key is signed by your CA and clients trust the CA with one
@cert-authorityline, so a rebuilt server with a freshly signed host certificate doesn't trigger this error. We compare the approach in SSH certificate authority vs public key auth for teams.
UpdateHostKeys also helps with planned rotations. OpenSSH enables it by default unless you changed UserKnownHostsFile or turned on VerifyHostKeyDNS, so a server can announce additional host keys after you authenticate, and your client learns them before the old ones are removed.
Where does termique fit?
termique checks host keys the same way OpenSSH does. The first time you connect to a host it saves the key the server presents, and every later connection must present the same key. If the key changes, termique blocks the connection before it sends any credential and shows Host key changed - connection blocked with the new key's type and its SHA256: fingerprint, in the same format ssh-keygen -lf prints. Trusting the new key is a deliberate step: you compare the fingerprint with the server, confirm that it matches and enter your master password. termique also blocks the connection when the address a host's credential was first sent to changes.
Two honest limits. termique supports plain host keys only and refuses a host that presents an SSH host certificate, so if your fleet runs on host certificates, the OpenSSH client is the better fit for those hosts today. And termique stores saved host keys on each computer, so a new device makes its own first-connection check, the same way a fresh known_hosts would.
termique is a free, native ssh manager we build, with end-to-end encrypted credentials: your passwords and private keys sync across devices only as ciphertext, while hosts, groups and snippets sync as plaintext metadata. The security docs describe the host key check step by step.
FAQ
Is "Host key verification failed" a problem with my SSH key?
No. Host key verification happens before authentication, so your private key, password and agent were not used. It is about the server's key and your known_hosts file. If git also prints "Could not read from remote repository", that line is a consequence, not the cause.
How do I fix "Host key verification failed" for github.com?
If you see the changed-key banner, remove the old entry with ssh-keygen -R github.com and add the current keys from GitHub's published list or its meta API. If you see the bare line in CI, the runner has no entry for github.com: add GitHub's published Ed25519 line to ~/.ssh/known_hosts before cloning.
Is it safe to delete my whole known_hosts file?
It works, and it throws away every key you already verified. The next connection to each server becomes a first connection again, where you would have to re-check fingerprints you checked before. Remove the single entry with ssh-keygen -R <host> instead.
Why does it happen after reinstalling my Raspberry Pi or server?
A fresh install generates new host keys, while your laptop still has the old key stored for the same name or IP. Confirm the new fingerprint on the device itself, then replace the entry with ssh-keygen -R and reconnect.
Where is known_hosts on Windows?
The built-in OpenSSH client on Windows uses C:\Users\<you>\.ssh\known_hosts, and ssh-keygen -R <host> works there too. If ssh works but git fails, check whether git is using a different ssh client (git config core.sshCommand or the GIT_SSH_COMMAND variable).
Fix the check, keep the check
"Host key verification failed" is ssh doing its job. Read what is above the last line, decide whether the host is unknown or changed, verify the fingerprint out of band and put the right key where the failing tool looks for it. If you manage many servers and want that check, with a clear fingerprint and an explicit trust step, on desktop and mobile, you can download termique and try it on the free plan.
Top comments (0)