有一次处于某些原因我在 Mac 上使用 SSH 远程登陆了 Windows,然后在 Windows 上使用 SSH 登陆 localhost,惊讶地发现登不进去!SSH 提示公钥验证失败。可是我的 Windows 使用的私钥和 Mac 是一样的,并且以前在 Windows 上也一直可以登陆 localhost,为什么今天突然不行了呢?

$ ssh localhost
myuser@localhost: Permission denied (publickey).

我关闭了密码登录,因此没有提示我输密码就直接拒绝访问

抱着百思不得姐的心情我开始了 debug。

我首先使用 ssh -v 命令查看连接过程中的日志:

$ ssh localhost -v
OpenSSH_for_Windows_8.6p1, LibreSSL 3.4.3
debug1: Reading configuration data C:\\Users\\MyUser/.ssh/config
debug1: Authenticator provider $SSH_SK_PROVIDER did not resolve; disabling
debug1: Connecting to localhost [::1] port 22.
debug1: Connection established.
debug1: identity file C:\\Users\\MyUser/.ssh/id_rsa type 0
debug1: identity file C:\\Users\\MyUser/.ssh/id_rsa-cert type -1
debug1: identity file C:\\Users\\MyUser/.ssh/id_dsa type -1
debug1: identity file C:\\Users\\MyUser/.ssh/id_dsa-cert type -1
debug1: identity file C:\\Users\\MyUser/.ssh/id_ecdsa type -1
debug1: identity file C:\\Users\\MyUser/.ssh/id_ecdsa-cert type -1
debug1: identity file C:\\Users\\MyUser/.ssh/id_ecdsa_sk type -1
debug1: identity file C:\\Users\\MyUser/.ssh/id_ecdsa_sk-cert type -1
debug1: identity file C:\\Users\\MyUser/.ssh/id_ed25519 type -1
debug1: identity file C:\\Users\\MyUser/.ssh/id_ed25519-cert type -1
debug1: identity file C:\\Users\\MyUser/.ssh/id_ed25519_sk type -1
debug1: identity file C:\\Users\\MyUser/.ssh/id_ed25519_sk-cert type -1
debug1: identity file C:\\Users\\MyUser/.ssh/id_xmss type -1
debug1: identity file C:\\Users\\MyUser/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_for_Windows_8.6
debug1: Remote protocol version 2.0, remote software version OpenSSH_8.9p1 Ubuntu-3ubuntu0.7
debug1: compat_banner: match: OpenSSH_8.9p1 Ubuntu-3ubuntu0.7 pat OpenSSH* compat 0x04000000
debug1: Authenticating to localhost:22 as 'windows11'
debug1: load_hostkeys: fopen C:\\Users\\MyUser/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen __PROGRAMDATA__\\ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen __PROGRAMDATA__\\ssh/ssh_known_hosts2: No such file or directory
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ssh-ed25519
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: SSH2_MSG_KEX_ECDH_REPLY received
debug1: Server host key: ssh-ed25519 SHA256:SWsZo54/ljQNSNSieY+AyQ27sbR31qWnyCbv7hzkarg
debug1: load_hostkeys: fopen C:\\Users\\MyUser/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen __PROGRAMDATA__\\ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen __PROGRAMDATA__\\ssh/ssh_known_hosts2: No such file or directory
debug1: Host 'localhost' is known and matches the ED25519 host key.
debug1: Found key in C:\\Users\\MyUser/.ssh/known_hosts:1
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: pubkey_prepare: ssh_get_authentication_socket: No such file or directory
debug1: Will attempt key: C:\\Users\\MyUser/.ssh/id_rsa RSA SHA256:Mqzkt00T7hxTv3hB2xbvKm7fron2ScNtlaNRbtuntvk
debug1: Will attempt key: C:\\Users\\MyUser/.ssh/id_dsa
debug1: Will attempt key: C:\\Users\\MyUser/.ssh/id_ecdsa
debug1: Will attempt key: C:\\Users\\MyUser/.ssh/id_ecdsa_sk
debug1: Will attempt key: C:\\Users\\MyUser/.ssh/id_ed25519
debug1: Will attempt key: C:\\Users\\MyUser/.ssh/id_ed25519_sk
debug1: Will attempt key: C:\\Users\\MyUser/.ssh/id_xmss
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,sk-ssh-ed25519@openssh.com,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,sk-ecdsa-sha2-nistp256@openssh.com,webauthn-sk-ecdsa-sha2-nistp256@openssh.com>
debug1: kex_input_ext_info: publickey-hostbound@openssh.com (unrecognised)
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: C:\\Users\\MyUser/.ssh/id_rsa RSA SHA256:Mqzkt00T7hxTv3hB2xbvKm7fron2ScNtlaNRbtuntvk
debug1: Authentications that can continue: publickey
debug1: Trying private key: C:\\Users\\MyUser/.ssh/id_dsa
debug1: Trying private key: C:\\Users\\MyUser/.ssh/id_ecdsa
debug1: Trying private key: C:\\Users\\MyUser/.ssh/id_ecdsa_sk
debug1: Trying private key: C:\\Users\\MyUser/.ssh/id_ed25519
debug1: Trying private key: C:\\Users\\MyUser/.ssh/id_ed25519_sk
debug1: Trying private key: C:\\Users\\MyUser/.ssh/id_xmss
debug1: No more authentication methods to try.
MyUser@localhost: Permission denied (publickey).

在日志中我发现了这样一行记录:

debug1: Remote protocol version 2.0, remote software version OpenSSH_8.9p1 Ubuntu-3ubuntu0.7

奇怪,我在 Windows 上连接 localhost,怎么连到一个 Ubuntu 版本的 OpenSSH 上了?

我立马想到了 WSL。

于是我使用 wsl --shutdown 命令关闭了所有 WSL 实例,再次尝试连接。

SSH 再次提示我远程主机的指纹变了,删除 known_hosts 后再次尝试,连接成功:

$ wsl --shutdown
$ ssh localhost
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ 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:DHrLaqaBwTvslmpHDDUMma3P62c9xQrqDNj+v/DFAww.
Please contact your system administrator.
Add correct host key in C:\\Users\\MyUser/.ssh/known_hosts to get rid of this message.
Offending ED25519 key in C:\\Users\\MyUser/.ssh/known_hosts:1
Host key for localhost has changed and you have requested strict checking.
Host key verification failed.
$ rm .\known_hosts
$ ssh localhost
The authenticity of host 'localhost (::1)' can't be established.
ED25519 key fingerprint is SHA256:DHrLaqaBwTvslmpHDDUMma3P62c9xQrqDNj+v/DFAww.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'localhost' (ED25519) to the list of known hosts.
>

确实是 WSL 在捣鬼。不过刚刚关闭 WSL 的时候 Docker 也自动退出了。之前我在 WSL 上并没有设置过有关网络镜像的配置,为什么会把 Windows 宿主机上的 localhost 映射到 WSL Ubuntu 虚拟机上呢?不过我的 WSL Ubuntu 共用宿主机的 Docker Desktop,会不会和 Docker Desktop 有关呢?

于是我打开了 WSL,但是关闭了 Docker Desktop,再次尝试连接 localhost,成功连接。

此时我再打开 Docker Desktop,然后尝试连接 localhost,于是发生了和开头相同的一幕:SSH 提示我远程主机指纹发生变化,删除 known_hosts 文件后再次尝试连接,提示公钥验证失败。

这时我想起 WSL Ubuntu 上设置的用户名和 Windows 不同,于是我在 ssh 命令中加上了 Ubuntu 用户名,再次尝试连接,成功登陆 WSL Ubuntu。

目前尚不清楚 Docker 为什么会影响 WSL 的网络,我已经把 Docker 运行的所有容器都删了,依然会影响 WSL 的网络。

SSH 登陆 Windows 时踩过的坑的更多相关文章

  1. [问题解决]RedHat7更换CentOS7的yum源时踩过的坑

    更换yum源的流程 查看当前yum程序 $ rpm -qa|grep yum 这里推荐将其结果截屏或拷贝出来,以免后面报错修复. 删除原有yum源 $ rpm -aq | grep yum|xargs ...

  2. 使用Ajax中get请求发送Token时踩的那些坑

    在使用惯了各种牛X的插件以后,在使用原生组件写一些小东西的时候总是有踩不完的坑! 今天就来说一说我使用原生ajax请求时踩得坑: 下面是我的代码: var xmlhttp; if (window.XM ...

  3. 那些年我用awk时踩过的坑——awk使用注意事项

    由于项目经历原因,经常使用awk处理一些文本数据.甚至,我特意下载了一个windows上的awk:gawk.exe,这样在windows上也能享受awk处理数据的方便性,. 俗话说,"常在河 ...

  4. [转] 那些在使用webpack时踩过的坑

    用webpack的过程,就是一个不断入坑和出坑的过程.回望来时路,一路都是坑啊!现把曾经趟过的那些坑整理出来,各位看官有福了~ 文章末尾有我用到的依赖的版本信息,若你发现某个问题与我在本文中的描述不一 ...

  5. 记录初学者学习Hive时踩过的坑

    1. 缺少MySQL驱动包 1.1 问题描述 Caused by: org.datanucleus.store.rdbms.connectionpool.DatastoreDriverNotFound ...

  6. 曲演杂坛--使用CTE时踩的小坑:No Join Predicate

    在一次系统优化中,意外发现一个比较“坑”的SQL,拿出来供大家分享. 生成演示数据: --====================================== --检查测试表是否存在 IF(O ...

  7. 配置tomcat时踩过的坑

    conf/server.xml文件有2处地方容易踩坑: 1.<HOst>标签中的appBase名字,要看清,有些修改的版本,会把这个名字改成deploy,而eclipse默认部署的是web ...

  8. 前端开发工具Brackets介绍,安装及安装Emme插件时踩过的坑

    对于前端开发的园友来说有可能IDE工具有很多,层次不穷,还有每个人的喜好及习惯也不一样,因为我是一名后端开发的.Net程序员,但是大家都知道,现在都提倡什么全栈工程师,所以也得会点前端开发,所以我对于 ...

  9. 研究slatstack时踩过的坑,注意点及解决方案

    运行问题 1.直接物理性移除minion或者更换minion原先连接的master,会导致先前的master始终无法ping通minion [root@localhost salt]# salt '* ...

  10. 记录在vue中使用jsx时踩过的坑

    使用方法及细节就不一一说了. 1.给input或者textarea绑定value时,出现失效的问题.解决方法:https://github.com/vuejs/babel-plugin-transfo ...

随机推荐

  1. 开发板测试手册——USB 4G 模块、GPS 定位功能操作步骤详解(3)

    目录 4 USB 4G 模块测试 41 4.1 网络功能测试 42 4.2 短信功能测试 43 4.3 GPS 定位功能测试 44 4.4 通话功能测试 45 4.5 测试程序编译 46 5 USB ...

  2. (Java)常用类库

    Spring 常用工具类 Spring作为常用的开发框架,在Spring框架应用中,排在ApacheCommon.Guava.Huool等通用库后,第二优先级可以考虑使用Spring-core-xxx ...

  3. 典型性相关分析在SPSS中的实现

    典型性相关分析是研究两组变量(每组变量中都可能有多个指标)之间相关关系的一种多元统计方法.它能够揭示出两组变量之间的内在联系. 本文着重模型在spss中的应用,通过一道例题解释各个指标的意义.详细推导 ...

  4. vol2以及mimikatz插件安装教程

    volatility2安装 https://github.com/volatilityfoundation/volatility git clone https://github.com/volati ...

  5. Java int/int 保留2位小数

    @Test public void txfloat() { // TODO 自动生成的方法存根 int a=9; int b=7; DecimalFormat df=new DecimalFormat ...

  6. 探究kubernetes 探针参数periodSeconds和timeoutSeconds

    探究kubernetes 探针参数 periodSeconds和timeoutSeconds 问题起源 kubernetes probes的配置中有两个容易混淆的参数,periodSeconds和ti ...

  7. CF-957(D-E)

    CF-957 赛时A去写全排列--前三题我的写法都挺丑的,后面改进了再更-- Problem - D - Codeforces 虽然是很简单很经典的线性dp,但也是我第一次自己把这种题写出来ヾ(≧▽≦ ...

  8. 安装PHP拓展

    win环境下: php扩展下载地址:http://pecl.php.net/ 需要知道:  php版本,操作系统位数,线程是否安全.想要知道这3个,在php中输入.如下图所示:phpinfo();di ...

  9. Profinet转ModbusTCP网关模块连发那科机器人与DCS通讯

    一.现场要求:发那科机器人作为服务器端,DCS作为客户端向发那科机器人发送读写请求,发那科机器人应答后DCS接收发那科机器人的数据,实现数据的传递. 二.解决方案:在不增加编程任务的前提下只需在DCS ...

  10. 说说XXLJob分片任务实现原理?

    XXL Job 是一个开源的分布式任务调度平台,其核心设计目标是开发迅速.学习简单.轻量级.易扩展的分布式任务调度框架. 这两天咱们开发的 AI Cloud 项目中,也使用到了 XXL Job 来执行 ...