大网数据广告活动
服务器运维问答

Can't login with SSH and SSH host key is different when using DNS name instead of IP address

· 52CE 我爱测

I just restarted my Ubuntu 16.04 server and started getting the error "WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!" when trying to ssh in using the DNS name ssh [email omitted] . I initially just removed the entries from kno…

问题原文

原帖链接 · silverscania · CC BY-SA 3.0

I just restarted my Ubuntu 16.04 server and started getting the error "WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!" when trying to ssh in using the DNS name ssh [email omitted]. I initially just removed the entries from known_hosts and tried again, but I realised it actually wasn't accepting my password at all.

Doing ssh [email omitted] using the IP address still works, and even my passwordless login still works.

This is the output of nslookup myserver.lan, which shows the DNS is returning the correct address:

Server:     192.168.1.2
Address:    192.168.1.2#53

Name:   myserver.lan
Address: 192.168.1.2

This is the verbose output of `ssh [email omitted]', which succeeds:

OpenSSH_6.9p1, LibreSSL 2.1.8
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: /etc/ssh/ssh_config line 56: Applying options for *
debug1: Connecting to 192.168.1.2 [192.168.1.2] port 22.
debug1: Connection established.
debug1: identity file /Users/jimbo/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/jimbo/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/jimbo/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/jimbo/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/jimbo/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/jimbo/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/jimbo/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/jimbo/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.9
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.2p2 Ubuntu-4ubuntu2.1
debug1: match: OpenSSH_7.2p2 Ubuntu-4ubuntu2.1 pat OpenSSH* compat 0x04000000
debug1: Authenticating to 192.168.1.2:22 as 'me'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client [email omitted] <implicit> none
debug1: kex: client->server [email omitted] <implicit> none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:ZOO3YXwox2kHpLokBRXzPvuUFPOrposhAgnpQCaZ5+I
The authenticity of host '192.168.1.2 (192.168.1.2)' can't be established.
ECDSA key fingerprint is SHA256:ZOO3YXwox2kHpLokBRXzPvuUFPOrposhAgnpQCaZ5+I.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.2' (ECDSA) to the list of known hosts.
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /Users/jimbo/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 279
debug1: Authentication succeeded (publickey).
Authenticated to 192.168.1.2 ([192.168.1.2]:22).
debug1: channel 0: new [client-session]
debug1: Requesting [email omitted]
debug1: Entering interactive session.
debug1: client_input_global_request: rtype [email omitted] want_reply 0
debug1: Sending environment.
debug1: Sending env LANG = en_GB.UTF-8
Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.4.0-78-generic x86_64)

And this is the verbose output of ssh [email omitted], which fails, and doesn't recognise my password:

OpenSSH_6.9p1, LibreSSL 2.1.8
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: /etc/ssh/ssh_config line 56: Applying options for *
debug1: Connecting to myserver.lan [::1] port 22.
debug1: Connection established.
debug1: identity file /Users/jimbo/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/jimbo/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/jimbo/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/jimbo/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/jimbo/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/jimbo/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/jimbo/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/jimbo/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.9
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.9
debug1: match: OpenSSH_6.9 pat OpenSSH* compat 0x04000000
debug1: Authenticating to myserver.lan:22 as 'me'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client [email omitted] <implicit> none
debug1: kex: client->server [email omitted] <implicit> none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:WDp/1JiHcWk6Lpcgfrl+/pDbSmdFxVTZFDfVhTEqBCg
debug1: Host 'myserver.lan' is known and matches the ECDSA host key.
debug1: Found key in /Users/jimbo/.ssh/known_hosts:5
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /Users/jimbo/.ssh/id_rsa
debug1: Authentications that can continue: publickey,keyboard-interactive
debug1: Trying private key: /Users/jimbo/.ssh/id_dsa
debug1: Trying private key: /Users/jimbo/.ssh/id_ecdsa
debug1: Trying private key: /Users/jimbo/.ssh/id_ed25519
debug1: Next authentication method: keyboard-interactive
Password:

As you can see the returned host keys are different. Is this correct to expect different keys when using the host name vs the IP address. What are the next steps I can take?

It looks like ssh is resolving the host name to localhost, as I get the same host key when sshing to localhost.

My hosts file is basically empty:

127.0.0.1   localhost
255.255.255.255 broadcasthost
::1             localhost 

But host myserver.lan returns this. Is this something to do with IPv6 configuration?

host myserver.lan
myserver.lan has address 192.168.1.2
myserver.lan has IPv6 address ::1

This is the output of dig @192.168.1.2 myserver.lan

; <<>> DiG 9.8.3-P1 <<>> @192.168.1.2 myserver.lan
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 32577
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 1

;; QUESTION SECTION:
;myserver.lan.          IN  A

;; ANSWER SECTION:
myserver.lan.       604800  IN  A   192.168.1.2

;; AUTHORITY SECTION:
myserver.lan.       604800  IN  NS  ns.myserver.lan.

;; ADDITIONAL SECTION:
ns.myserver.lan.    604800  IN  A   192.168.1.2

;; Query time: 21 msec
;; SERVER: 192.168.1.2#53(192.168.1.2)
;; WHEN: Sun May 21 13:34:41 2017
;; MSG SIZE  rcvd: 79

And the bind config file on the server:

;
; BIND data file for myserver

$TTL    604800
@       IN      SOA     myserver.lan. root.myserver.lan. (
                              3         ; Serial
                         604800         ; Refresh
                          86400         ; Retry
                        2419200         ; Expire
                         604800 )       ; Negative Cache TTL
        IN      A       192.168.1.2
;
@       IN      NS      ns.myserver.lan.
@       IN      A       192.168.1.2
@       IN      AAAA    ::1
ns      IN      A       192.168.1.2

P.S. I have a windows laptop with cygwin which is still able to log into the server using the host name. Only my Mac seems to be affected.

已采纳解答

原帖链接 · AlexD · CC BY-SA 3.0

The problem is caused by incorrect DNS configuration. ssh client uses IPv6 and 'myserver.lan' is being resolved to [::1] . You need to remove AAAA ::1 record from your zone configuration file and restart named.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-05-21)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。