尽管有 ssh 密钥,但本地主机身份验证失败

Authentication localhost failed, in spite of 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 似乎在某些方面配置错误