ubuntu 解决scp ssh登录WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
使用SSH登录某台机器,有时因为server端的一些变动,会出现以下信息:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ 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 the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
50:e6:cb:58:bc:b7:a3:f6:e8:8f:46:a7:c1:5f:c2:df.
Please contact your system administrator.
Add correct host key in /home/cobyeah/.ssh/known_hosts to get rid of this message.
Offending key in /home/cobyeah/.ssh/known_hosts:7
RSA host key for 192.168.0.4 has changed and you have requested strict checking.
Host key verification failed.
(此处先不提及原理,只讲处理方法,需要了解原因的请留言或找其他资料)
这时候的处理方法,有3种:
1. 删除提示信息中,对应的行数,例如上例,需要删除/home/cobyeah/.ssh/known_hosts文件的第7行。
2. 删除整份/home/cobyeah/.ssh/known_hosts文件。
3. 修改/etc/ssh/ssh_config文件的配置,以后则不会再出现此问题
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
输入以下命令重启SSH服务:sudo /etc/init.d/ssh restart
root@blj-pc:/etc/ssh# ls moduli ssh_config.d sshd_config.d ssh_host_ecdsa_key.pub ssh_host_ed25519_key.pub ssh_host_rsa_key.pub ssh_config sshd_config ssh_host_ecdsa_key ssh_host_ed25519_key ssh_host_rsa_key ssh_import_id root@blj-pc:/etc/ssh# cd root@blj-pc:~# cd /root/.ssh/ root@blj-pc:~/.ssh# ls known_hosts known_hosts.old root@blj-pc:~/.ssh# rm known_hosts root@blj-pc:~/.ssh# ls known_hosts.old root@blj-pc:~/.ssh# rm -rf * root@blj-pc:~/.ssh# sudo /etc/init.d/ssh restart Restarting ssh (via systemctl): ssh.service. root@blj-pc:~/.ssh# cd /etc/ssh/ root@blj-pc:/etc/ssh# cat ssh_config # This is the ssh client system-wide configuration file. See # ssh_config(5) for more information. This file provides defaults for # users, and the values can be changed in per-user configuration files # or on the command line. # Configuration data is parsed as follows: # 1. command line options # 2. user-specific file # 3. system-wide file # Any configuration value is only changed the first time it is set. # Thus, host-specific definitions should be at the beginning of the # configuration file, and defaults at the end. # Site-wide defaults for some commonly used options. For a comprehensive # list of available options, their meanings and defaults, please see the # ssh_config(5) man page. Include /etc/ssh/ssh_config.d/*.conf Host * # ForwardAgent no # ForwardX11 no # ForwardX11Trusted yes # PasswordAuthentication yes # HostbasedAuthentication no # GSSAPIAuthentication no # GSSAPIDelegateCredentials no # GSSAPIKeyExchange no # GSSAPITrustDNS no # BatchMode no # CheckHostIP yes # AddressFamily any # ConnectTimeout 0 StrictHostKeyChecking no UserKnownHostsFile /dev/null # IdentityFile ~/.ssh/id_rsa # IdentityFile ~/.ssh/id_dsa # IdentityFile ~/.ssh/id_ecdsa # IdentityFile ~/.ssh/id_ed25519 # Port 22 # Ciphers aes128-ctr,aes192-ctr,aes256-ctr,aes128-cbc,3des-cbc # MACs hmac-md5,hmac-sha1,umac-64@openssh.com # EscapeChar ~ # Tunnel no # TunnelDevice any:any # PermitLocalCommand no # VisualHostKey no # ProxyCommand ssh -q -W %h:%p gateway.example.com # RekeyLimit 1G 1h SendEnv LANG LC_* HashKnownHosts yes GSSAPIAuthentication yes root@blj-pc:/etc/ssh#
下面简单讲一下这个问题的原理和比较长久的解决方案。
用OpenSSH的人都知ssh会把你每个你访问过计算机的公钥(public key)都记录在~/.ssh/known_hosts。当下次访问相同计算机时,OpenSSH会核对公钥。如果公钥不同,OpenSSH会发出警告, 避免你受到DNS Hijack之类的攻击。
SSH对主机的public_key的检查等级是根据StrictHostKeyChecking变量来配置的。默认情况下,
StrictHostKeyChecking=ask。简单所下它的三种配置值:
1.StrictHostKeyChecking=no
#最不安全的级别,当然也没有那么多烦人的提示了,相对安全的内网测试时建议使用。如果连接server的key在本地不存在,那么就自动添加到文件中(默认是known_hosts),并且给出一个警告。
2.StrictHostKeyChecking=ask #默认的级别,就是出现刚才的提示了。如果连接和key不匹配,给出提示,并拒绝登录。
3.StrictHostKeyChecking=yes #
最安全的级别,如果连接与key不匹配,就拒绝连接,不会提示详细信息。
对于我来说,在内网的进行的一些测试,为了方便,选择最低的安全级别。在.ssh/config(或者/etc/ssh/ssh_config)中配置:
StrictHostKeyChecking no UserKnownHostsFile /dev/null
(注:这里为了简便,将knownhostfile设为/dev/null,就不保存在known_hosts中了)
SSH 登录失败:Host key verification failed 的处理方法