At my side this happens due to something which I consider an ssh
bug of newer (OpenSSH_7.9p1
and above) clients, when it tries to learn a more secure ecdsa
server key where there already is an older rsa
type key known. It then presents this misleading message!
I do not know a good fix for this, the only workaround I found is to remove all "good but old rsa
keys" such that the client can re-learn the "new more secure ecdsa
keys". So:
The first step is to remove all the good old RSA keys (Warning! This loses protection against MitM):
$ sed -i '/ ssh-rsa /d' ~/.ssh/known_hosts
The second step then is to re-learn all host keys, which must be done manually by connecting to each IP again using ssh
.
Here is what I observe:
$ sftp test@136.243.197.100
Connected to test@136.243.197.100
sftp>
$ sftp test@valentin.hilbig.de
Connected to test@valentin.hilbig.de.
sftp>
Now try to connect to a newly introduced alias of this same already known good server:
$ sftp test@gcopy.net
Warning: the ECDSA host key for 'gcopy.net' differs from the key for the IP address '136.243.197.100'
Offending key for IP in /home/test/.ssh/known_hosts:45
Matching host key in /home/test/.ssh/known_hosts:44
Are you sure you want to continue connecting (yes/no)?
Please have a look at the IP address. It's the same IP as above! So it looks like the (good) key of the (known) IP suddenly is offending itself (it isn't, as the ssh
client mixes two incompatible keys, see below).
Now we try to fix it:
$ ssh-keygen -R 136.243.197.100
# Host 136.243.197.100 found: line 45
/home/test/.ssh/known_hosts updated.
Original contents retained as /home/test/.ssh/known_hosts.old
Let's try again:
$ sftp test@gcopy.net
Warning: Permanently added the ECDSA host key for IP address '136.243.197.100' to the list of known hosts.
Connected to test@gcopy.net.
$ sftp test@valentin.hilbig.de
Warning: the RSA host key for 'valentin.hilbig.de' differs from the key for the IP address '136.243.197.100'
Offending key for IP in /home/test/.ssh/known_hosts:45
Matching host key in /home/test/.ssh/known_hosts:10
Are you sure you want to continue connecting (yes/no)?
WTF? What happened here?
The new fresh key learned from the server fails again?
And the problem even switched sides?!?
Nope, it's not the key, nor the server. Everything is correct!
It's the ssh
client which fails to verify the correct key!
Entry 45
in known_hosts
now carries a key of type ecdsa-sha2-nistp256
while the key, which was pulled from the server by the client, is of type rsa-sha2-512
(and therefor cannot match the other key!).
$ sftp -v test@valentin.hilbig.de
shows:
debug1: kex: host key algorithm: rsa-sha2-512
while
$ sftp -v test@gcopy.net
shows:
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
Apparently the ssh
client has a bug somewhere!
It cannot cope with a host key existing in more than one variant!
Or it falls into the trap to request an outdated variant of a key.
How to fix?
I really have no idea. This probably can only be fixed upstream.
But there is a manual but clumsy workaround:
You have to manually remove all traces of the old key of type rsa
. The key in question is shown in the output, but it is not directly marked as the problem:
Warning: the RSA host key for 'valentin.hilbig.de' differs from the key for the IP address '136.243.197.100'
Offending key for IP in /home/test/.ssh/known_hosts:45
Matching host key in /home/test/.ssh/known_hosts:10
check:
awk 'NR==45 { print $2 }' /home/test/.ssh/known_hosts
awk 'NR==10 { print $2 }' /home/test/.ssh/known_hosts
gives
ecdsa-sha2-nistp256
ssh-rsa
So here the matching host key is the offending one and the offending key is the right one which must be kept! So let's remove the wrong (matching) one:
ssh-keygen -R valentin.hilbig.de
# Host valentin.hilbig.de found: line 10
/home/test/.ssh/known_hosts updated.
Original contents retained as /home/test/.ssh/known_hosts.old
Now check again:
$ sftp test@valentin.hilbig.de
The authenticity of host 'valentin.hilbig.de (136.243.197.100)' can't be established.
ECDSA key fingerprint is SHA256:tf7lwe10C2p1lK2UG9p//m/4sUBCpX+i9k5Ub63c6Os.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'valentin.hilbig.de' (ECDSA) to the list of known hosts.
Connected to test@valentin.hilbig.de.
sftp>
$ sftp test@gcopy.net
Connected to test@gcopy.net.
sftp>
sftp test@136.243.197.100
Connected to test@136.243.197.100.
sftp>
YAY! Problem finally gone. But with several 100 entries in .ssh/known_hosts
, this "solution" really becomes a major PITA (and an Error Prone Security Nightmare on Elm Street. YMMV.)
in my case, I change the server(ip) bind with the domain, then the
The ECDSA host key for server has changed
. My way is remove the related cache string about domain in~/.ssh/known_hosts
. Then the ssh works. – Ninja – 2017-07-11T03:59:10.067