尽管有 ssh-keys 身份验证本地主机失败

问题描述

我正在 CI 中设置我的虚拟机,以便我可以测试使用 ssh 的脚本。

我正在设置运行 Ubuntu 20.04.2 LTS 的虚拟机,如下所示:

sudo apt-get install openssh-server
ssh-keygen -t rsa -q -f "$HOME/.ssh/id_rsa" -N ""
cat "$HOME/.ssh/id_rsa.pub" >> "$HOME/.ssh/authorized_keys"

(根据文档,ssh-server 会自动启动,我使用 sudo systemctl status ssh 验证确实是这种情况)

为了调试我跑了

ssh -v localhost "uname -r"

给予:

OpenSSH_8.2p1 Ubuntu-4ubuntu0.1,OpenSSL 1.1.1f  31 Mar 2020
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: Connecting to localhost [::1] port 22.
debug1: Connection established.
debug1: identity file /home/runner/.ssh/id_rsa type 0
debug1: identity file /home/runner/.ssh/id_rsa-cert type -1
debug1: identity file /home/runner/.ssh/id_dsa type -1
debug1: identity file /home/runner/.ssh/id_dsa-cert type -1
debug1: identity file /home/runner/.ssh/id_ecdsa type -1
debug1: identity file /home/runner/.ssh/id_ecdsa-cert type -1
debug1: identity file /home/runner/.ssh/id_ecdsa_sk type -1
debug1: identity file /home/runner/.ssh/id_ecdsa_sk-cert type -1
debug1: identity file /home/runner/.ssh/id_ed25519 type -1
debug1: identity file /home/runner/.ssh/id_ed25519-cert type -1
debug1: identity file /home/runner/.ssh/id_ed25519_sk type -1
debug1: identity file /home/runner/.ssh/id_ed25519_sk-cert type -1
debug1: identity file /home/runner/.ssh/id_xmss type -1
debug1: identity file /home/runner/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.1
debug1: Remote protocol version 2.0,remote software version OpenSSH_8.2p1 Ubuntu-4ubuntu0.1
debug1: match: OpenSSH_8.2p1 Ubuntu-4ubuntu0.1 pat OpenSSH* compat 0x04000000
debug1: Authenticating to localhost:22 as 'runner'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
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: Server host key: ecdsa-sha2-nistp256 SHA256:NFnWPH2boToKCX0yq1K33LY0K3EwK810f599LpwoXJI
debug1: read_passphrase: can't open /dev/tty: No such device or address
Host key verification failed.

为了完整性

cat /var/log/auth.log

给出:

Mar 10 06:13:08 fv-az189-848 php8.0: DIGEST-MD5 common mech free
Mar 10 06:13:09 fv-az189-848 php8.0: DIGEST-MD5 common mech free
Mar 10 06:13:09 fv-az189-848 php8.0: DIGEST-MD5 common mech free
Mar 10 06:13:52 fv-az189-848 sudo:   runner : TTY=unknown ; *** ; USER=root ; COMMAND=/usr/bin/apt-get install openssh-server
Mar 10 06:13:52 fv-az189-848 sudo: pam_unix(sudo:session): session opened for user root by (uid=0)
Mar 10 06:13:55 fv-az189-848 sudo: pam_unix(sudo:session): session closed for user root
Mar 10 06:13:55 fv-az189-848 sudo:   runner : TTY=unknown ; *** ; USER=root ; COMMAND=/usr/bin/systemctl status ssh
Mar 10 06:13:55 fv-az189-848 sudo: pam_unix(sudo:session): session opened for user root by (uid=0)
Mar 10 06:13:55 fv-az189-848 sudo: pam_unix(sudo:session): session closed for user root
Mar 10 06:13:55 fv-az189-848 sshd[1847]: Connection closed by ::1 port 54242 [preauth]

在我看来,ssh 正在找到密钥,但正在尝试读取我在这里使用 -N "" 选项设置为空的密码:

ssh-keygen -t rsa -q -f "$HOME/.ssh/id_rsa" -N ""

因此,我不知道如何解决此问题。我可以创建 /dev/tty 但在我看来 ssh 甚至不应该寻找它。

解决方法

我检查了你的设置,它对我有用。

但我的调试输出在 Server host key

之后看起来不同
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:NFnWPH2boToKCX0yq1K33LY0K3EwK810f599LpwoXJI
debug1: Host 'localhost' is known and matches the ECDSA host key.
debug1: Found key in /home/pi/.ssh/known_hosts:15

我假设您的 localhost 文件中不存在 .ssh/known_host,因此 ssh 想请您确认。

你可以尝试不检查就开始

ssh -v localhost "uname -r" -o StrictHostKeyChecking=no

但潜在的问题是您缺少 /dev/tty,您当前的 tty 似乎在某些方面配置错误

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...